From 455d37b727c6b114614251551100e116cd453674 Mon Sep 17 00:00:00 2001 From: stonegate Date: Thu, 13 Feb 2020 13:09:22 +0800 Subject: [PATCH] modified: lib/home/appbar/addpodcast.dart modified: lib/home/appbar/popupmenu.dart modified: lib/home/audio_player.dart modified: lib/home/home.dart modified: test/widget_test.dart --- lib/home/appbar/addpodcast.dart | 103 +++++------ lib/home/appbar/popupmenu.dart | 2 +- lib/home/audio_player.dart | 293 ++++++++++++++++---------------- lib/home/home.dart | 42 ++--- test/widget_test.dart | 12 +- 5 files changed, 231 insertions(+), 221 deletions(-) diff --git a/lib/home/appbar/addpodcast.dart b/lib/home/appbar/addpodcast.dart index 5bd08b2..2aae50e 100644 --- a/lib/home/appbar/addpodcast.dart +++ b/lib/home/appbar/addpodcast.dart @@ -15,7 +15,7 @@ import 'package:tsacdop/class/searchpodcast.dart'; import 'package:tsacdop/class/podcastlocal.dart'; import 'package:tsacdop/class/sqflite_localpodcast.dart'; import 'package:tsacdop/home/home.dart'; -import 'popupmenu.dart'; +import 'package:tsacdop/home/appbar/popupmenu.dart'; import 'package:tsacdop/webfeed/webfeed.dart'; class MyHomePage extends StatefulWidget { @@ -29,28 +29,34 @@ class _MyHomePageState extends State { @override Widget build(BuildContext context) { - return Scaffold( - key: _scaffoldKey, - appBar: AppBar( - elevation: 0, - centerTitle: true, - backgroundColor: Colors.grey[100], - leading: IconButton( - tooltip: 'Add', - icon: const Icon(Icons.add_circle_outline), - onPressed: () async { - await showSearch( - context: context, - delegate: _delegate, - ); - }, + return ChangeNotifierProvider( + create: (context) => ImportOmpl(), + child: Scaffold( + key: _scaffoldKey, + appBar: AppBar( + elevation: 0, + centerTitle: true, + backgroundColor: Colors.grey[100], + leading: IconButton( + tooltip: 'Add', + icon: const Icon(Icons.add_circle_outline), + onPressed: () async { + await showSearch( + context: context, + delegate: _delegate, + ); + }, + ), + title: Image( + image: AssetImage('assets/text.png'), + height: 30, + ), + actions: [ + PopupMenu(), + ], ), - title: Image(image: AssetImage('assets/text.png'), height: 30,), - actions: [ - PopupMenu(), - ], + body: Home(), ), - body: Home(), ); } } @@ -207,8 +213,8 @@ class _SearchResultState extends State { @override Widget build(BuildContext context) { - final importOmpl = Provider.of(context); - + ImportOmpl importOmpl = Provider.of(context); + savePodcast(String rss) async { print(rss); if (mounted) setState(() => _adding = true); @@ -222,37 +228,36 @@ class _SearchResultState extends State { print(_p.title); var dir = await getApplicationDocumentsDirectory(); - try{ - Response> imageResponse = await Dio().get>( - _p.itunes.image.href, - options: Options(responseType: ResponseType.bytes)); + try { + Response> imageResponse = await Dio().get>( + _p.itunes.image.href, + options: Options(responseType: ResponseType.bytes)); - img.Image image = img.decodeImage(imageResponse.data); - img.Image thumbnail = img.copyResize(image, width: 300); - File("${dir.path}/${_p.title}.png") - ..writeAsBytesSync(img.encodePng(thumbnail)); + img.Image image = img.decodeImage(imageResponse.data); + img.Image thumbnail = img.copyResize(image, width: 300); + File("${dir.path}/${_p.title}.png") + ..writeAsBytesSync(img.encodePng(thumbnail)); - String _primaryColor = - await getColor(File("${dir.path}/${_p.title}.png")); - PodcastLocal podcastLocal = PodcastLocal( - _p.title, _p.itunes.image.href, rss, _primaryColor, _p.author); - podcastLocal.description = _p.description; - var dbHelper = DBHelper(); - await dbHelper.savePodcastLocal(podcastLocal); + String _primaryColor = + await getColor(File("${dir.path}/${_p.title}.png")); + PodcastLocal podcastLocal = PodcastLocal( + _p.title, _p.itunes.image.href, rss, _primaryColor, _p.author); + podcastLocal.description = _p.description; + var dbHelper = DBHelper(); + await dbHelper.savePodcastLocal(podcastLocal); - importOmpl.importState = ImportState.parse; + importOmpl.importState = ImportState.parse; - await dbHelper.savePodcastRss(_p); + await dbHelper.savePodcastRss(_p); - importOmpl.importState = ImportState.complete; - importOmpl.importState = ImportState.stop; - print('fatch data'); - } - catch(e){ - Fluttertoast.showToast( - msg: 'Network error, Subscribe failed', - gravity: ToastGravity.BOTTOM, - ); + importOmpl.importState = ImportState.complete; + importOmpl.importState = ImportState.stop; + print('fatch data'); + } catch (e) { + Fluttertoast.showToast( + msg: 'Network error, Subscribe failed', + gravity: ToastGravity.BOTTOM, + ); } } diff --git a/lib/home/appbar/popupmenu.dart b/lib/home/appbar/popupmenu.dart index a28096a..d95c4e2 100644 --- a/lib/home/appbar/popupmenu.dart +++ b/lib/home/appbar/popupmenu.dart @@ -42,7 +42,7 @@ class PopupMenu extends StatelessWidget { @override Widget build(BuildContext context) { - final importOmpl = Provider.of(context); + ImportOmpl importOmpl = Provider.of(context); _refreshAll() async { var dbHelper = DBHelper(); diff --git a/lib/home/audio_player.dart b/lib/home/audio_player.dart index 0c79cdf..0a02006 100644 --- a/lib/home/audio_player.dart +++ b/lib/home/audio_player.dart @@ -2,7 +2,6 @@ import 'dart:async'; import 'dart:io'; import 'dart:typed_data'; import 'package:provider/provider.dart'; -import 'package:network_image_to_byte/network_image_to_byte.dart'; import 'package:audiofileplayer/audiofileplayer.dart'; import 'package:audiofileplayer/audio_system.dart'; @@ -37,7 +36,6 @@ class _PlayerWidgetState extends State { String url; String _title; String _feedtitle; - String _imgurl; bool _isLoading; @override @@ -156,10 +154,10 @@ class _PlayerWidgetState extends State { } return 'NotDownload'; } - + ByteData getAudio(String path) { File audioFile = File(path); - Uint8List audio = audioFile.readAsBytesSync(); + Uint8List audio = audioFile.readAsBytesSync(); return ByteData.view(audio.buffer); } @@ -172,9 +170,8 @@ class _PlayerWidgetState extends State { this.url = url; _title = Provider.of(context).title; _feedtitle = Provider.of(context).feedtitle; - _imgurl = Provider.of(context).imageurl; - _backgroundAudioPlaying = true; - _isLoading = true; + _backgroundAudioPlaying = true; + _isLoading = true; _getFile(url).then((result) { result == 'NotDownload' ? _initbackgroundAudioPlayer(url) @@ -236,11 +233,6 @@ class _PlayerWidgetState extends State { } } - Future _networkImageToByte(String url) async { - Uint8List byteImage = await networkImageToByte(url); - return byteImage; - } - final _pauseButton = AndroidCustomMediaButton( 'pausenow', pausenowButtonId, 'ic_stat_pause_circle_filled'); final _replay10Button = AndroidCustomMediaButton( @@ -252,7 +244,8 @@ class _PlayerWidgetState extends State { Future _setNotification() async { var dir = await getApplicationDocumentsDirectory(); - final Uint8List imageBytes = File('${dir.path}/$_feedtitle.png').readAsBytesSync(); + final Uint8List imageBytes = + File('${dir.path}/$_feedtitle.png').readAsBytesSync(); AudioSystem.instance.setMetadata(AudioMetadata( title: _title, artist: _feedtitle, @@ -290,9 +283,9 @@ class _PlayerWidgetState extends State { Provider.of(context, listen: false).audioState = AudioState.play; }); - var dir = await getApplicationDocumentsDirectory(); - final Uint8List imageBytes = File('${dir.path}/$_feedtitle.png').readAsBytesSync(); - //final Uint8List imageBytes = await _networkImageToByte(_imgurl); + var dir = await getApplicationDocumentsDirectory(); + final Uint8List imageBytes = + File('${dir.path}/$_feedtitle.png').readAsBytesSync(); AudioSystem.instance.setMetadata(AudioMetadata( title: _title, artist: _feedtitle, @@ -367,141 +360,149 @@ class _PlayerWidgetState extends State { @override Widget build(BuildContext context) { - return !_isLoading - ? Center() - : Container( - padding: EdgeInsets.symmetric(horizontal: 10.0), - color: Colors.grey[100], - height: 120.0, - child: Column( - mainAxisAlignment: MainAxisAlignment.start, - mainAxisSize: MainAxisSize.min, - children: [ - Container( - alignment: Alignment.centerLeft, - child: _remoteErrorMessage != null - ? Text(_remoteErrorMessage, - style: - const TextStyle(color: const Color(0xFFFF0000))) - : Text( - _remoteAudioLoading ? 'Buffring...' : '', - style: TextStyle(color: Colors.blue), + return ChangeNotifierProvider( + create: (context) => Urlchange(), + child: !_isLoading + ? Center() + : Container( + padding: EdgeInsets.symmetric(horizontal: 10.0), + color: Colors.grey[100], + height: 120.0, + child: Column( + mainAxisAlignment: MainAxisAlignment.start, + mainAxisSize: MainAxisSize.min, + children: [ + Container( + alignment: Alignment.centerLeft, + child: _remoteErrorMessage != null + ? Text(_remoteErrorMessage, + style: const TextStyle( + color: const Color(0xFFFF0000))) + : Text( + _remoteAudioLoading ? 'Buffring...' : '', + style: TextStyle(color: Colors.blue), + ), + ), + SliderTheme( + data: SliderTheme.of(context).copyWith( + activeTrackColor: Colors.blue[100], + inactiveTrackColor: Colors.grey[300], + trackHeight: 2.0, + thumbColor: Colors.blue[400], + thumbShape: + RoundSliderThumbShape(enabledThumbRadius: 5.0), + overlayColor: Colors.blue.withAlpha(32), + overlayShape: + RoundSliderOverlayShape(overlayRadius: 14.0), + ), + child: Slider( + value: _seekSliderValue, + onChanged: (double val) { + setState(() => _seekSliderValue = val); + final double positionSeconds = + val * _backgroundAudioDurationSeconds; + _backgroundAudio.seek(positionSeconds); + AudioSystem.instance + .setPlaybackState(true, positionSeconds); + }), + ), + Container( + height: 20.0, + padding: EdgeInsets.symmetric(horizontal: 10.0), + child: Row( + children: [ + Text( + _stringForSeconds( + _backgroundAudioPositionSeconds) ?? + '', + style: TextStyle(fontSize: 10), ), - ), - SliderTheme( - data: SliderTheme.of(context).copyWith( - activeTrackColor: Colors.blue[100], - inactiveTrackColor: Colors.grey[300], - trackHeight: 2.0, - thumbColor: Colors.blue[400], - thumbShape: - RoundSliderThumbShape(enabledThumbRadius: 5.0), - overlayColor: Colors.blue.withAlpha(32), - overlayShape: - RoundSliderOverlayShape(overlayRadius: 14.0), + Expanded( + child: Container( + padding: EdgeInsets.symmetric(horizontal: 30), + alignment: Alignment.center, + child: (_title.length > 50) + ? Marquee( + text: _title, + style: + TextStyle(fontWeight: FontWeight.bold), + scrollAxis: Axis.horizontal, + crossAxisAlignment: + CrossAxisAlignment.start, + blankSpace: 30.0, + velocity: 50.0, + pauseAfterRound: Duration(seconds: 1), + startPadding: 30.0, + accelerationDuration: Duration(seconds: 1), + accelerationCurve: Curves.linear, + decelerationDuration: + Duration(milliseconds: 500), + decelerationCurve: Curves.easeOut, + ) + : Text( + _title, + style: + TextStyle(fontWeight: FontWeight.bold), + ), + )), + Text( + _stringForSeconds( + _backgroundAudioDurationSeconds) ?? + '', + style: TextStyle(fontSize: 10), + ), + ], + ), ), - child: Slider( - value: _seekSliderValue, - onChanged: (double val) { - setState(() => _seekSliderValue = val); - final double positionSeconds = - val * _backgroundAudioDurationSeconds; - _backgroundAudio.seek(positionSeconds); - AudioSystem.instance - .setPlaybackState(true, positionSeconds); - }), - ), - Container( - height: 20.0, - padding: EdgeInsets.symmetric(horizontal: 10.0), - child: Row( - children: [ - Text( - _stringForSeconds(_backgroundAudioPositionSeconds) ?? - '', - style: TextStyle(fontSize: 10), - ), - Expanded( - child: Container( - padding: EdgeInsets.symmetric(horizontal: 30), - alignment: Alignment.center, - child: (_title.length > 50) - ? Marquee( - text: _title, - style: TextStyle(fontWeight: FontWeight.bold), - scrollAxis: Axis.horizontal, - crossAxisAlignment: CrossAxisAlignment.start, - blankSpace: 30.0, - velocity: 50.0, - pauseAfterRound: Duration(seconds: 1), - startPadding: 30.0, - accelerationDuration: Duration(seconds: 1), - accelerationCurve: Curves.linear, - decelerationDuration: - Duration(milliseconds: 500), - decelerationCurve: Curves.easeOut, - ) - : Text( - _title, - style: TextStyle(fontWeight: FontWeight.bold), - ), - )), - Text( - _stringForSeconds(_backgroundAudioDurationSeconds) ?? - '', - style: TextStyle(fontSize: 10), - ), - ], - ), - ), - Row( - mainAxisSize: MainAxisSize.min, - children: [ - IconButton( - padding: EdgeInsets.symmetric(horizontal: 30.0), - onPressed: _backgroundAudioPlaying - ? () => _forwardBackgroundAudio(-10) - : null, - iconSize: 25.0, - icon: Icon(Icons.replay_10), - color: Colors.black), - _backgroundAudioPlaying - ? IconButton( - padding: EdgeInsets.symmetric(horizontal: 30.0), - onPressed: _backgroundAudioPlaying - ? () { - _pauseBackgroundAudio(); - } - : null, - iconSize: 32.0, - icon: Icon(Icons.pause_circle_filled), - color: Colors.black) - : IconButton( - padding: EdgeInsets.symmetric(horizontal: 30.0), - onPressed: _backgroundAudioPlaying - ? null - : () { - _resumeBackgroundAudio(); - }, - iconSize: 32.0, - icon: Icon(Icons.play_circle_filled), - color: Colors.black), - IconButton( - padding: EdgeInsets.symmetric(horizontal: 30.0), - onPressed: _backgroundAudioPlaying - ? () => _forwardBackgroundAudio(30) - : null, - iconSize: 25.0, - icon: Icon(Icons.forward_30), - color: Colors.black), - /*IconButton( + Row( + mainAxisSize: MainAxisSize.min, + children: [ + IconButton( + padding: EdgeInsets.symmetric(horizontal: 30.0), + onPressed: _backgroundAudioPlaying + ? () => _forwardBackgroundAudio(-10) + : null, + iconSize: 25.0, + icon: Icon(Icons.replay_10), + color: Colors.black), + _backgroundAudioPlaying + ? IconButton( + padding: EdgeInsets.symmetric(horizontal: 30.0), + onPressed: _backgroundAudioPlaying + ? () { + _pauseBackgroundAudio(); + } + : null, + iconSize: 32.0, + icon: Icon(Icons.pause_circle_filled), + color: Colors.black) + : IconButton( + padding: EdgeInsets.symmetric(horizontal: 30.0), + onPressed: _backgroundAudioPlaying + ? null + : () { + _resumeBackgroundAudio(); + }, + iconSize: 32.0, + icon: Icon(Icons.play_circle_filled), + color: Colors.black), + IconButton( + padding: EdgeInsets.symmetric(horizontal: 30.0), + onPressed: _backgroundAudioPlaying + ? () => _forwardBackgroundAudio(30) + : null, + iconSize: 25.0, + icon: Icon(Icons.forward_30), + color: Colors.black), + /*IconButton( onPressed: _isPlaying || _isPaused ? () => _stop() : null, iconSize: 32.0, icon: Icon(Icons.stop), color: Colors.black), */ - ], - ), - ]), - ); + ], + ), + ]), + ), + ); } } diff --git a/lib/home/home.dart b/lib/home/home.dart index b6c30b6..bf59517 100644 --- a/lib/home/home.dart +++ b/lib/home/home.dart @@ -1,10 +1,12 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import 'package:tsacdop/class/audiostate.dart'; +import 'package:provider/provider.dart'; -import '../podcasts/podcastlist.dart'; +import 'package:tsacdop/podcasts/podcastlist.dart'; import 'hometab.dart'; import 'package:tsacdop/home/appbar/importompl.dart'; -import 'audio_player.dart'; +import 'package:tsacdop/home/audio_player.dart'; import 'homescroll.dart'; import 'package:tsacdop/util/pageroute.dart'; @@ -16,15 +18,17 @@ class Home extends StatefulWidget { class _HomeState extends State { @override Widget build(BuildContext context) { - return Column( - mainAxisAlignment: MainAxisAlignment.start, - mainAxisSize: MainAxisSize.min, - children: [ - Import(), - Container( - height: 30, - padding: EdgeInsets.symmetric(horizontal: 15), - alignment: Alignment.bottomRight, + return ChangeNotifierProvider( + create: (context) => Urlchange(), + child: Column( + mainAxisAlignment: MainAxisAlignment.start, + mainAxisSize: MainAxisSize.min, + children: [ + Import(), + Container( + height: 30, + padding: EdgeInsets.symmetric(horizontal: 15), + alignment: Alignment.bottomRight, child: GestureDetector( onTap: () { Navigator.push( @@ -43,12 +47,12 @@ class _HomeState extends State { ), ), ), - Container(child: ScrollPodcasts()), - Expanded( - child: MainTab(), - ), - PlayerWidget(), - ], - ); + Container(child: ScrollPodcasts()), + Expanded( + child: MainTab(), + ), + PlayerWidget(), + ], + )); } -} \ No newline at end of file +} diff --git a/test/widget_test.dart b/test/widget_test.dart index c22e120..300ea78 100644 --- a/test/widget_test.dart +++ b/test/widget_test.dart @@ -16,15 +16,15 @@ void main() { await tester.pumpWidget(MyApp()); // Verify that our counter starts at 0. - expect(find.text('0'), findsOneWidget); - expect(find.text('1'), findsNothing); + expect(find.text('Favorites'), findsOneWidget); + expect(find.text('Next'), findsNothing); // Tap the '+' icon and trigger a frame. - await tester.tap(find.byIcon(Icons.add)); - await tester.pump(); + //await tester.tap(find.byIcon(Icons.add)); + //await tester.pump(); // Verify that our counter has incremented. - expect(find.text('0'), findsNothing); - expect(find.text('1'), findsOneWidget); + //expect(find.text('0'), findsNothing); + //expect(find.text('1'), findsOneWidget); }); }