Skip to content

Instantly share code, notes, and snippets.

@meet30997
Created March 11, 2022 03:06
Show Gist options
  • Save meet30997/a815fba14a7f8731e59a4a2b4164e8fe to your computer and use it in GitHub Desktop.
Save meet30997/a815fba14a7f8731e59a4a2b4164e8fe to your computer and use it in GitHub Desktop.
DartChildUpdateFromParent
import 'package:flutter/material.dart';
const 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: ParentWid(),
),
),
);
}
}
class ParentWid extends StatefulWidget {
@override
State<ParentWid> createState() => _ParentWidState();
}
class _ParentWidState extends State<ParentWid> {
int counter = 0;
@override
Widget build(BuildContext context) {
return Column(children:[
IconButton(
onPressed: () {
setState(() {
counter++;
});
},
icon: const Icon(Icons.add),
),
ChildWid(counter)
]);
}
}
class ChildWid extends StatefulWidget {
final int? counter;
const ChildWid(this.counter);
@override
State<ChildWid> createState() => _ChildWidState();
}
class _ChildWidState extends State<ChildWid> {
@override
Widget build(BuildContext context) {
return Text(
'Current Counter ${widget.counter ?? 0}',
style: Theme.of(context).textTheme.headline4,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment