Created
April 6, 2022 08:51
-
-
Save nesimtunc/bfed1de47d46245f252051c4343c9e94 to your computer and use it in GitHub Desktop.
Reduce Flutter Widget Rebuilding while using Provider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:letswatch/video_store.dart'; | |
import 'package:provider/provider.dart'; | |
class Video { | |
String url; | |
Status status = Status.notReady; | |
Video(this.url, {this.status = Status.notReady}); | |
void changeStatus(Status status) { | |
print( | |
'change status from ${this.status.toString()} to ${status.toString()}'); | |
this.status = status; | |
} | |
} | |
enum Status { notReady, loading, ready, playing, paused, error } | |
class VideoStore with ChangeNotifier { | |
Video? _video; | |
Video? get video => _video; | |
Future<void> playMedia(Video video) async { | |
_video = video; | |
_video?.changeStatus(Status.loading); | |
notifyListeners(); | |
Future.delayed(const Duration(seconds: 2), () { | |
_video?.changeStatus(Status.error); | |
notifyListeners(); | |
}); | |
} | |
} | |
void main() { | |
runApp( | |
ChangeNotifierProvider( | |
create: (context) => VideoStore(), | |
child: const MyApp(), | |
), | |
); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({Key? key, required this.title}) : super(key: key); | |
final String title; | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
Video? _video; | |
@override | |
void initState() { | |
_video = Video('https://video.example.com'); | |
super.initState(); | |
} | |
void _playMedia() { | |
Provider.of<VideoStore>(context, listen: false).playMedia(_video!); | |
} | |
bool useConsumer = false; | |
@override | |
Widget build(BuildContext context) { | |
if (useConsumer) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
'Consumer Way:', | |
style: Theme.of(context).textTheme.headline3, | |
), | |
Consumer<VideoStore>(builder: (context, value, child) { | |
return Text( | |
value.video == null | |
? "Unknown" | |
: value.video!.status.toString(), | |
style: Theme.of(context).textTheme.headline4, | |
); | |
}), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _playMedia, | |
tooltip: 'Play', | |
child: const Icon(Icons.play_arrow), | |
), | |
); | |
} else { | |
final video = Provider.of<VideoStore>(context).video; | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
'Other way', | |
style: Theme.of(context).textTheme.headline3, | |
), | |
VideoStatus(video: video), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _playMedia, | |
tooltip: 'Play', | |
child: const Icon(Icons.play_arrow), | |
), | |
); | |
} | |
} | |
} | |
class VideoStatus extends StatelessWidget { | |
const VideoStatus({ | |
Key? key, | |
required this.video, | |
}) : super(key: key); | |
final Video? video; | |
@override | |
Widget build(BuildContext context) { | |
return Text( | |
video == null ? "Unknown" : video!.status.toString(), | |
style: Theme.of(context).textTheme.headline4, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment