Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple startup app (counter app) demonstrating the InheritedWidget
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) => MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
State createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: BuildInheritedWidget(child: CounterPage()),
floatingActionButton: FloatingActionButton(
onPressed: () => BuildInheritedWidget.setState(() {}),
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
class CounterPage extends StatefulWidget {
const CounterPage({Key key}) : super(key: key);
@override
State createState() => _CounterPageState();
}
class _CounterPageState extends State<CounterPage> {
@override
Widget build(BuildContext context) {
var data = InheritedData.of(context).data;
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'You have pushed the button this many times:',
),
Text(
'$data',
style: Theme.of(context).textTheme.headline4,
),
],
),
);
}
}
/// Builds a [InheritedWidget].
///
/// It's instantiated in a standalone widget
/// so its setState() call will **only** rebuild
/// [InheritedWidget] and consequently any of its dependents,
/// instead of rebuilding the app's entire widget tree.
class BuildInheritedWidget extends StatefulWidget {
BuildInheritedWidget({Key key, this.child}) : super(key: key);
final Widget child;
static final _BuildInheritedWidgetState state = _BuildInheritedWidgetState();
@override
State createState() => state;
/// Allow a 'rebuild' by the Widget and not the State.
static void setState(VoidCallback fn) => state.setState(fn);
}
class _BuildInheritedWidgetState extends State<BuildInheritedWidget> {
@override
Widget build(BuildContext context) => InheritedData(child: widget.child);
/// Override again merely to prevent the warning message.
@override
void setState(VoidCallback fn) => super.setState(fn);
}
class InheritedData extends InheritedWidget {
///
InheritedData({Key key, Widget child})
: object = DataField(),
super(key: key, child: child);
///
final DataField object;
dynamic get data => object.data;
/// Included temporarily for demonstration purposes.
@override
InheritedElement createElement() => InheritedElement(this);
///
@override
bool updateShouldNotify(InheritedData oldWidget) {
object.data = oldWidget.object.data + 1;
return true;
return false;
}
///
static InheritedData of(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<InheritedData>();
}
class DataField {
int data = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment