Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:provider/provider.dart';
  3.  
  4. void main() => runApp(MyApp());
  5.  
  6. class CnCounter with ChangeNotifier {
  7. int _number = 0;
  8.  
  9. int get number => _number;
  10.  
  11. void increment() {
  12. _number++;
  13. notifyListeners();
  14. }
  15. }
  16.  
  17. class MyApp extends StatelessWidget {
  18. @override
  19. Widget build(BuildContext context) {
  20. return MaterialApp(
  21. title: 'Flutter Demo',
  22. theme: ThemeData(
  23. primarySwatch: Colors.blue,
  24. ),
  25. home: ChangeNotifierProvider<CnCounter>(
  26. builder: (_) => CnCounter(),
  27. child: myHomePage('Flutter Demo Home Page', context),
  28. ),
  29. );
  30. }
  31. }
  32.  
  33. Widget myHomePage(String title, BuildContext context) {
  34. return Consumer<CnCounter>(
  35. builder: (_, counter, __) {
  36. return Scaffold(
  37. appBar: AppBar(
  38. title: Text(title),
  39. ),
  40. body: Center(
  41. child: Column(
  42. mainAxisAlignment: MainAxisAlignment.center,
  43. children: <Widget>[
  44. Text(
  45. 'You have pushed the button this many times:',
  46. ),
  47. Text(
  48. '${counter.number}',
  49. style: Theme.of(context).textTheme.display1,
  50. ),
  51. ],
  52. ),
  53. ),
  54. floatingActionButton: FloatingActionButton(
  55. onPressed: counter.increment,
  56. tooltip: 'Increment',
  57. child: Icon(Icons.add),
  58. ),
  59. );
  60. },
  61. );
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement