1_ Video playback


1.1 integration into applications

pubspec.yaml

Chewie: ^ 0.12.2 video_player: ^ 1.0.1Copy the code
  assets:
   - videos/IntroVideo.mp4
Copy the code

android/app/src/main/AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
Copy the code
android:usesCleartextTraffic="true"
Copy the code

chewie_list_item.dart

import 'package:chewie/chewie.dart'; import 'package:flutter/material.dart'; import 'package:video_player/video_player.dart'; class ChewieListItem extends StatefulWidget { // This will contain the URL/asset path which we want to play final VideoPlayerController videoPlayerController; final bool looping; ChewieListItem({ @required this.videoPlayerController, this.looping, Key key, }) : super(key: key); @override _ChewieListItemState createState() => _ChewieListItemState(); } class _ChewieListItemState extends State<ChewieListItem> { ChewieController _chewieController; @override void initState() { super.initState(); // Wrapper on top of the videoPlayerController _chewieController = ChewieController( videoPlayerController: widget.videoPlayerController, aspectRatio: 16 / 9, // Prepare the video to be played and display the first frame autoInitialize: true, looping: widget.looping, // Errors can occur for example when trying to play a video // from a non-existent URL errorBuilder: (context, errorMessage) { return Center( child: Text( errorMessage, style: TextStyle(color: Colors.white), ), ); }); } override Widget build(BuildContext context) {return Padding(Padding: const EdgeInsets. All (8.0), child: Chewie( controller: _chewieController, ), ); } @override void dispose() { super.dispose(); // IMPORTANT to dispose of all the used resources widget.videoPlayerController.dispose(); _chewieController.dispose(); }}Copy the code

main.dart

import 'package:flutter_app/chewie_list_item.dart'; import 'package:flutter/material.dart'; import 'package:video_player/video_player.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(), ); } } class MyHomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Video Player'), ), body: ListView( children: <Widget>[ ChewieListItem( videoPlayerController: VideoPlayerController.asset( 'videos/IntroVideo.mp4', ), looping: true, ), ], ), ); }}Copy the code

The effect

2_ Copy and paste

import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, title: 'Kindacode.com', home: HomePage(), ); } } class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePageState(); } class _HomePageState extends State<HomePage> { TextEditingController _textController = TextEditingController(); // This function is triggered when the copy icon is pressed Future<void> _copyToClipboard() async { await Clipboard.setData(ClipboardData(text: _textController.text)); ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: Text('Copied to clipboard'), )); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Kindacode.com'), backgroundColor: Colors.deepOrange, ), body: Center( child: Padding( padding: const EdgeInsets.all(30), child: TextField( controller: _textController, decoration: InputDecoration( icon: IconButton( icon: Icon(Icons.copy), onPressed: _copyToClipboard, ), ), ), ))); }}Copy the code