Last active
April 7, 2025 16:21
-
-
Save loic-sharma/c5b829021425e3a90798e88c7f0bc065 to your computer and use it in GitHub Desktop.
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'; | |
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'), | |
), | |
]); | |
} | |
} |
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'; | |
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'), | |
), | |
]); | |
} | |
} |
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'; | |
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