Created
November 24, 2023 01:36
-
-
Save Likilee/e191d9df4c6eaaed2ff455c699ed6a30 to your computer and use it in GitHub Desktop.
Gist 테스트 :)
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
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file | |
// for details. All rights reserved. Use of this source code is governed by a | |
// BSD-style license that can be found in the LICENSE file. | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_riverpod/flutter_riverpod.dart'; | |
// This is a reimplementation of the default Flutter application | |
// using riverpod. | |
void main() { | |
runApp( | |
// Adding ProviderScope enables Riverpod for the entire project | |
const ProviderScope(child: MyApp()), | |
); | |
} | |
/// Providers are declared globally and specify how to create a state | |
final counterProvider = StateProvider((ref) => 0); | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData( | |
colorSchemeSeed: Colors.blue, | |
useMaterial3: true, | |
), | |
home: const MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends ConsumerWidget { | |
const MyHomePage({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context, WidgetRef ref) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Riverpod example'), | |
), | |
body: Center( | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
const Text('You have pushed the button this many times:'), | |
Consumer( | |
builder: (context, ref, _) { | |
final count = ref.watch(counterProvider); | |
return Text( | |
'$count', | |
key: const Key('counterState'), | |
style: Theme.of(context).textTheme.headlineMedium, | |
); | |
}, | |
), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
key: const Key('increment_floatingActionButton'), | |
// The read method is a utility to read a provider without listening to it | |
onPressed: () => ref.read(counterProvider.notifier).state++, | |
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