如何处置chewie()视频播放器小部件

时间:2018-08-24 13:27:57

标签: dart flutter

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

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

1 个答案:

答案 0 :(得分:0)

您必须使用 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() ),
            ),
          ),
        ),
      );
    }
  }