Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:provider/provider.dart';
- void main() => runApp(MyApp());
- class CnCounter with ChangeNotifier {
- int _number = 0;
- int get number => _number;
- void increment() {
- _number++;
- notifyListeners();
- }
- }
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'Flutter Demo',
- theme: ThemeData(
- primarySwatch: Colors.blue,
- ),
- home: ChangeNotifierProvider<CnCounter>(
- builder: (_) => CnCounter(),
- child: myHomePage('Flutter Demo Home Page', context),
- ),
- );
- }
- }
- Widget myHomePage(String title, BuildContext context) {
- return Consumer<CnCounter>(
- builder: (_, counter, __) {
- return Scaffold(
- appBar: AppBar(
- title: Text(title),
- ),
- body: Center(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- Text(
- 'You have pushed the button this many times:',
- ),
- Text(
- '${counter.number}',
- style: Theme.of(context).textTheme.display1,
- ),
- ],
- ),
- ),
- floatingActionButton: FloatingActionButton(
- onPressed: counter.increment,
- tooltip: 'Increment',
- child: Icon(Icons.add),
- ),
- );
- },
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement