Last active
September 18, 2020 09:46
-
-
Save FantasyCheese/f50fac114dd3730cf085771563922c16 to your computer and use it in GitHub Desktop.
Simplest InheritedWidget Example
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'; | |
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