Last active
February 15, 2023 09:54
-
-
Save chooyan-eng/f6d0042cfaba5f08c78bacda43d32875 to your computer and use it in GitHub Desktop.
Sample for Flutter Tokyo
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'; | |
import 'package:flutter_riverpod/flutter_riverpod.dart'; | |
void main() => runApp( | |
const ProviderScope( | |
child: MyApp(), | |
), | |
); | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData( | |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), | |
useMaterial3: true, | |
), | |
home: const MyHomePage(title: 'Flutter Meetup'), | |
); | |
} | |
} | |
final countProvider = StateProvider((ref) { | |
return 0; | |
}); | |
class MyHomePage extends ConsumerWidget { | |
const MyHomePage({super.key, required this.title}); | |
final String title; | |
@override | |
Widget build(BuildContext context, WidgetRef ref) { | |
return Scaffold( | |
appBar: AppBar( | |
backgroundColor: Theme.of(context).colorScheme.inversePrimary, | |
title: Text(title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: const [ | |
Text( | |
'You have pushed the button this many times:', | |
), | |
_CounterText(), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
ref.read(countProvider.notifier).state += 1; | |
}, | |
tooltip: 'Increment', | |
child: const Icon(Icons.add), | |
), | |
); | |
} | |
} | |
class _CounterText extends ConsumerWidget { | |
const _CounterText(); | |
@override | |
Widget build(BuildContext context, WidgetRef ref) { | |
final count = ref.watch(countProvider); | |
return Text( | |
'$count', | |
style: Theme.of(context).textTheme.headlineMedium, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment