Slide 15
Slide 15 text
class Parent extends StatefulWidget { … }
class _ParentState extends State {
int count = 0;
@override
Widget build(BuildContext context) {
return CountInherited(
count: count,
child: Scaffold(
body: const Center(
child: Child(),
),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() {
count++;
}),
),
),
);
}
}
15
InheritedWidget
class CountInherited extends InheritedWidget {
const CountInherited({
super.key,
required this.count,
required super.child,
});
final int count;
static CountInherited of(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType()!;
@override
bool updateShouldNotify(CountInherited oldWidget) =>
oldWidget.count != count;
}