Created
September 18, 2020 11:24
-
-
Save FantasyCheese/6d6805524e16e5d5e3ed3da29abb8f10 to your computer and use it in GitHub Desktop.
Simplest Provider with ChangeNotifier 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 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'package:provider/provider.dart'; | |
void main() { | |
runApp(ChangeNotifierProvider( | |
create: (_) => Counter(), | |
child: MyApp(), | |
)); | |
} | |
class Counter extends ChangeNotifier { | |
Counter() { | |
Timer.periodic(Duration(seconds: 1), (timer) { | |
_count++; | |
notifyListeners(); | |
}); | |
} | |
int _count = 42; | |
int get count => _count; | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Center( | |
child: RichText( | |
text: TextSpan(text: context.watch<Counter>().count.toString()), | |
textDirection: TextDirection.ltr, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment