Created with <3 with dartpad.dev.
Created
May 23, 2023 15:53
-
-
Save hongsw/31fbd2fc71641ba26e7d75a3e99e1bdf to your computer and use it in GitHub Desktop.
fascinating-kingdom-4671
This file contains hidden or 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'; | |
import 'package:provider/provider.dart'; | |
class CounterModel extends ChangeNotifier { | |
int _count = 0; | |
int get count => _count; | |
void increment() { | |
_count++; | |
notifyListeners(); | |
} | |
} | |
void main() { | |
runApp(ChangeNotifierProvider( | |
create: (context) => CounterModel(), | |
child: MyApp(), | |
)); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar(title: const Text('Counter')), | |
body: Center( | |
child: Text('${context.watch<CounterModel>().count}'), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () => context.read<CounterModel>().increment(), | |
child: const Icon(Icons.add), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment