Created
April 20, 2021 15:22
-
-
Save matuella/3a1baf3fc1a684cf88d1eb64790748ef to your computer and use it in GitHub Desktop.
Stateful Children are updated by argument as well
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'; | |
final Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue), | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: ParentWidget(), | |
), | |
), | |
); | |
} | |
} | |
class ParentWidget extends StatefulWidget { | |
@override | |
_ParentWidgetState createState() => _ParentWidgetState(); | |
} | |
class _ParentWidgetState extends State<ParentWidget> { | |
String arg = 'arg'; | |
@override | |
Widget build(BuildContext context) { | |
return Column( | |
children: [ | |
ElevatedButton( | |
onPressed: () { | |
setState(() { | |
arg += arg; | |
}); | |
}, | |
child: Text('add arg'), | |
), | |
OtherWidget(arg) | |
], | |
); | |
} | |
} | |
class OtherWidget extends StatefulWidget { | |
OtherWidget(this.arg); | |
final String arg; | |
@override | |
_OtherWidgetState createState() => _OtherWidgetState(); | |
} | |
class _OtherWidgetState extends State<OtherWidget> { | |
@override | |
void initState() { | |
print('started state'); | |
super.initState(); | |
} | |
@override | |
void didChangeDependencies() { | |
print('changed deps'); | |
super.didChangeDependencies(); | |
} | |
@override | |
void didUpdateWidget(covariant OtherWidget oldWidget) { | |
print('updt widget'); | |
super.didUpdateWidget(oldWidget); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
child: Text(widget.arg), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment