paging_page.dart 3.4 KB

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