paging_page.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_ijkplayer/flutter_ijkplayer.dart';
  3. import 'package:ijkplayer_example/i18n/i18n.dart';
  4. import 'package:oktoast/oktoast.dart';
  5. import 'package:photo/photo.dart';
  6. class PagingPickPage extends StatelessWidget {
  7. @override
  8. Widget build(BuildContext context) {
  9. return Scaffold(
  10. appBar: AppBar(
  11. title: Text(currentI18n.pick),
  12. ),
  13. body: ListView(
  14. children: <Widget>[
  15. FlatButton(
  16. child: Text(currentI18n.pick),
  17. onPressed: () => pickVideo(context),
  18. ),
  19. ],
  20. ),
  21. );
  22. }
  23. pickVideo(BuildContext context) async {
  24. var photos = await PhotoPicker.pickAsset(
  25. context: context,
  26. maxSelected: 8,
  27. pickType: PickType.onlyVideo,
  28. );
  29. if (photos == null || photos.isEmpty) {
  30. showToast(currentI18n.noPickTip);
  31. return;
  32. }
  33. showDialog(
  34. context: context,
  35. builder: (_) => buildLoadingWidget(),
  36. );
  37. List<DataSource> dataSourceList = [];
  38. for (var photo in photos) {
  39. var file = await photo.file;
  40. dataSourceList.add(DataSource.file(file));
  41. }
  42. Navigator.pop(context);
  43. Navigator.push(
  44. context,
  45. MaterialPageRoute(
  46. builder: (_) => PagingPage(
  47. dataSourceList: dataSourceList,
  48. ),
  49. ),
  50. );
  51. }
  52. }
  53. Widget buildLoadingWidget() {
  54. return Center(
  55. child: Container(
  56. decoration: BoxDecoration(
  57. borderRadius: BorderRadius.circular(8),
  58. color: Colors.white,
  59. ),
  60. width: 80,
  61. height: 80,
  62. padding: EdgeInsets.all(22),
  63. child: CircularProgressIndicator(),
  64. ),
  65. );
  66. }
  67. class PagingPage extends StatefulWidget {
  68. final List<DataSource> dataSourceList;
  69. const PagingPage({
  70. Key key,
  71. this.dataSourceList,
  72. }) : super(key: key);
  73. @override
  74. _PagingPageState createState() => _PagingPageState();
  75. }
  76. class _PagingPageState extends State<PagingPage> {
  77. Map<DataSource, IjkMediaController> map = {};
  78. @override
  79. void initState() {
  80. super.initState();
  81. assert(widget.dataSourceList != null);
  82. assert(widget.dataSourceList.isNotEmpty);
  83. initFirst();
  84. }
  85. void initFirst() async {
  86. await Future.delayed(Duration(seconds: 1));
  87. getControllerWithSrc(widget.dataSourceList[0])?.play();
  88. }
  89. @override
  90. void dispose() {
  91. _disposeAllCtl();
  92. super.dispose();
  93. }
  94. _disposeAllCtl() {
  95. for (var ctl in map.values) {
  96. ctl?.dispose();
  97. }
  98. }
  99. @override
  100. Widget build(BuildContext context) {
  101. assert(widget.dataSourceList != null);
  102. return PageView.builder(
  103. scrollDirection: Axis.vertical,
  104. itemBuilder: _buildItem,
  105. itemCount: widget.dataSourceList.length,
  106. onPageChanged: (current) {
  107. print("current page = $current");
  108. var src = widget.dataSourceList[current];
  109. var ctl = getControllerWithSrc(src);
  110. ctl?.pauseOtherController();
  111. ctl?.seekTo(0);
  112. ctl?.play();
  113. },
  114. );
  115. }
  116. // Hold up to three controllers.
  117. // IjkMediaController initControllers(int current) {
  118. // var src = widget.dataSourceList[current];
  119. // var ctl = getControllerWithSrc(src);
  120. // var next = current + 1;
  121. // if (next < widget.dataSourceList.length) {
  122. // var datasource = widget.dataSourceList[next];
  123. // var nextCtl = getControllerWithSrc(datasource);
  124. // }
  125. // return ctl;
  126. // }
  127. // void disposeOther(int current) {
  128. // var last = current - 1;
  129. // var next = current + 1;
  130. // }
  131. Widget _buildItem(BuildContext context, int index) {
  132. var src = widget.dataSourceList[index];
  133. var ctl = getControllerWithSrc(src);
  134. return IjkPlayer(
  135. mediaController: ctl,
  136. controllerWidgetBuilder: (_) => Container(),
  137. );
  138. }
  139. IjkMediaController getControllerWithSrc(DataSource src) {
  140. var ctl = map[src];
  141. if (ctl == null) {
  142. ctl = IjkMediaController();
  143. map[src] = ctl;
  144. ctl.setDataSource(src, autoPlay: false);
  145. }
  146. return ctl;
  147. }
  148. }