【问题标题】:how do I dispose chewie() video player widget我如何处理chewie() 视频播放器小部件
【发布时间】:2019-01-30 23:57:37
【问题描述】:

我如何处理chewie 小部件。我有这个,但不知道如何在我离开页面时停止播放视频

   Chewie(
   VideoPlayerController.file(videoFile),
   autoInitialize: true,
   cupertinoProgressColors: ChewieProgressColors(),
   ),

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    您必须使用 WillPopScope 小部件。使用该小部件,您可以在离开屏幕之前处理任何事情,在您的情况下,您可以停止视频。下面的例子可以帮助你。

    import 'package:flutter/material.dart';
    
      int counter = 0 ;
    
      void main() {
        runApp(MaterialApp(
          title: 'Demo',
          initialRoute: '/',
          routes: {
            '/': (context) => FirstScreen(),
            '/second': (context) => SecondScreen(),
          },
        ));
      }
    
      class FirstScreen extends StatelessWidget {
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            appBar: AppBar(
              title: Text('Vounter Value '),
            ),
            body: Center(
              child: RaisedButton(
                child: Text('Launch Second screen'),
                onPressed: () {
                  Navigator.pushNamed(context, '/second');
                },
              ),
            ),
          );
        }
      }
    
      class SecondScreen extends StatefulWidget {
        @override
        _SecondScreenState createState() => _SecondScreenState();
      }
    
      class _SecondScreenState extends State<SecondScreen> with SingleTickerProviderStateMixin {
    
    
        _willPopScope(){
          counter = counter + 1;
        }
    
    
        @override
        Widget build(BuildContext context) {
          return WillPopScope(
            onWillPop: _willPopScope() ,
            child: Scaffold(
              appBar: AppBar(
                title: Text("Second Screen"),
              ),
              body: Center(
                child: RaisedButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: Text('Go back!' + counter.toString() ),
                ),
              ),
            ),
          );
        }
      }
    

    【讨论】:

      猜你喜欢
      • 2020-12-08
      • 2020-06-04
      • 2019-02-17
      • 1970-01-01
      • 2014-11-23
      • 2019-08-13
      • 1970-01-01
      • 1970-01-01
      • 2020-08-21
      相关资源
      最近更新 更多