Skip to content

Instantly share code, notes, and snippets.

@micedreams
Created December 7, 2023 18:39
Show Gist options
  • Save micedreams/6df271a17e3ebb48bdd4962544359ff6 to your computer and use it in GitHub Desktop.
Save micedreams/6df271a17e3ebb48bdd4962544359ff6 to your computer and use it in GitHub Desktop.
Note: don't use '++'? context: line 69
import 'package:flutter/material.dart';
void main() {
runApp(const Main());
}
class Main extends StatelessWidget {
const Main({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const App(),
);
}
}
class App extends StatefulWidget {
const App({
super.key,
});
@override
State createState() => _AppState();
}
class _AppState extends State<App> {
final countNotifier = ValueNotifier(0);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text("Counter with value listner"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
ValueListenableBuilder(
valueListenable: countNotifier,
builder: (context, count, _) {
return Text(
'$count',
style: Theme.of(context).textTheme.headlineMedium,
);
},
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
void _incrementCounter() => countNotifier.value = countNotifier.value + 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment