Skip to content

Instantly share code, notes, and snippets.

@loic-sharma
Last active April 7, 2025 16:21
Show Gist options
  • Save loic-sharma/c5b829021425e3a90798e88c7f0bc065 to your computer and use it in GitHub Desktop.
Save loic-sharma/c5b829021425e3a90798e88c7f0bc065 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
body: CounterPage(),
),
),
);
}
class CounterPage extends StatefulWidget {
@override
State<CounterPage> createState() => _CounterPageState();
}
class _CounterPageState extends State<CounterPage> {
int _counter = 0;
@override
Widget build(BuildContext context) {
return Column(children: [
Text('Count: $_counter'),
ElevatedButton(
onPressed: () {
setState(() {
_counter++;
});
},
child: Text('Increment'),
),
]);
}
}
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
body: CounterPage(),
),
),
);
}
@observable
class Counter({int count = 0});
final counter = Counter();
class CounterPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
context.watchListenable(counter);
return Column(children: [
Text('Count: ${counter.count}'),
ElevatedButton(
onPressed: () {
counter.count++;
},
child: Text('Increment'),
),
]);
}
}
import 'package:flutter/material.dart';
void main() {
final counter = Counter();
runApp(
MaterialApp(
home: Scaffold(
body: CounterPage(counter: counter),
),
),
);
}
@observable
class Counter({int count = 0});
class CounterPage extends StatelessWidget({
Counter counter,
}) {
@override
Widget build(BuildContext context) {
context.watchListenable(counter);
return Column(children: [
Text('Count: ${counter.count}'),
ElevatedButton(
onPressed: () {
counter.count++;
},
child: Text('Increment'),
),
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment