Last active
July 2, 2024 17:55
-
-
Save rodydavis/1b2f58d30c33ee2ee5c5a159b8867861 to your computer and use it in GitHub Desktop.
Flutter Counter with Signals
This file contains 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:signals/signals_flutter.dart'; | |
void main() { | |
runApp(const App()); | |
} | |
class App extends StatelessWidget { | |
const App({super.key}); | |
ThemeData createTheme(BuildContext context, Brightness brightness) { | |
return ThemeData( | |
colorScheme: ColorScheme.fromSeed( | |
seedColor: Colors.blue, | |
brightness: brightness, | |
), | |
brightness: brightness, | |
useMaterial3: true, | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: createTheme(context, Brightness.light), | |
darkTheme: createTheme(context, Brightness.dark), | |
themeMode: ThemeMode.system, | |
home: const CounterExample(), | |
); | |
} | |
} | |
class CounterExample extends StatefulWidget { | |
const CounterExample({super.key}); | |
@override | |
State<CounterExample> createState() => _CounterExampleState(); | |
} | |
class _CounterExampleState extends State<CounterExample> { | |
late final Signal<int> counter = createSignal(context, 0); | |
void _incrementCounter() { | |
counter.value++; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Flutter Counter'), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
const Text( | |
'You have pushed the button this many times:', | |
), | |
Text( | |
'$counter', | |
style: Theme.of(context).textTheme.headlineMedium, | |
), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _incrementCounter, | |
tooltip: 'Increment', | |
child: const Icon(Icons.add), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment