Last active
November 30, 2023 20:35
-
-
Save rodydavis/717f338b265c400b1693bd0938c67b5c to your computer and use it in GitHub Desktop.
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 MyApp()); | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) => const MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: MyHomePage(), | |
); | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({super.key}); | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
final name = signal('Jane'); | |
final surname = signal('Doe'); | |
late final fullName = computed(() => '${name.value} ${surname.value}'); | |
@override | |
Widget build(BuildContext context) => Scaffold( | |
appBar: AppBar( | |
title: const Text('Signals Example'), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
fullName.watch(context), | |
style: Theme.of(context).textTheme.headlineMedium, | |
), | |
ElevatedButton( | |
onPressed: () => | |
name.value = name.value == 'Jane' ? 'John' : 'Jane', | |
child: const Text('Change Name'), | |
), | |
ElevatedButton( | |
onPressed: () => | |
surname.value = surname.value == 'Doe' ? 'Dop' : 'Doe', | |
child: const Text('Change Surname'), | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment