Skip to content

Instantly share code, notes, and snippets.

@FantasyCheese
Last active September 18, 2020 09:46
Show Gist options
  • Save FantasyCheese/f50fac114dd3730cf085771563922c16 to your computer and use it in GitHub Desktop.
Save FantasyCheese/f50fac114dd3730cf085771563922c16 to your computer and use it in GitHub Desktop.
Simplest InheritedWidget Example
import 'package:flutter/material.dart';
void main() {
runApp(CountProvider(
count: 42,
child: MyApp(),
));
}
class CountProvider extends InheritedWidget {
final Widget child;
final int count;
CountProvider({Key key, this.child, this.count}) : super(key: key, child: child);
static CountProvider of(BuildContext context) => context.dependOnInheritedWidgetOfExactType(aspect: CountProvider);
@override
bool updateShouldNotify(CountProvider oldWidget) => true;
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: RichText(
text: TextSpan(text: CountProvider.of(context).count.toString()),
textDirection: TextDirection.ltr,
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment