Skip to content

Instantly share code, notes, and snippets.

@hongsw
Created May 23, 2023 15:53
Show Gist options
  • Save hongsw/31fbd2fc71641ba26e7d75a3e99e1bdf to your computer and use it in GitHub Desktop.
Save hongsw/31fbd2fc71641ba26e7d75a3e99e1bdf to your computer and use it in GitHub Desktop.
fascinating-kingdom-4671

fascinating-kingdom-4671

Created with <3 with dartpad.dev.

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