Created
May 2, 2021 05:07
-
-
Save namphho/9915f1faea8342c5cfdbf155852b403b to your computer and use it in GitHub Desktop.
didDependencyChanges
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:provider/provider.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
visualDensity: VisualDensity.adaptivePlatformDensity, | |
), | |
home: ParentWidget(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class ParentWidget extends StatefulWidget { | |
ParentWidget({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_ParentWidgetState createState() => _ParentWidgetState(); | |
} | |
class _ParentWidgetState extends State<ParentWidget> { | |
int _counter = 0; | |
void _incrementCounter() { | |
setState(() { | |
_counter++; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Provider.value( // Provide the value here. | |
value: _counter, | |
updateShouldNotify: (oldValue, newValue) => newValue % 2 == 0, | |
child: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
'You have pushed the button this many times:', | |
), | |
ChildWidget() | |
], | |
), | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _incrementCounter, | |
tooltip: 'Increment', | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
} | |
class ChildWidget extends StatefulWidget { | |
@override | |
_ChildWidgetState createState() => _ChildWidgetState(); | |
} | |
class _ChildWidgetState extends State<ChildWidget> { | |
int _counter = 0; | |
@override | |
void initState() { | |
// // When we don't care the new value from the parent, just set `listen = false`. | |
// _counter = Provider.of<int>(context, listen: false); | |
debugPrint('Child widget: initState(), counter = $_counter'); | |
super.initState(); | |
} | |
@override | |
void didChangeDependencies() { | |
// When parent widget `updateShouldNotify: true`, | |
// child widget can obtain new value when setting `listen: true`. | |
_counter = Provider.of<int>(context, listen: true); | |
debugPrint('Child widget: didChangeDependencies(), counter = $_counter'); | |
super.didChangeDependencies(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
print("ChildWidget build"); | |
return Text( | |
'$_counter', | |
style: Theme.of(context).textTheme.display1, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment