mirror of
https://github.com/rrousselGit/riverpod.git
synced 2025-05-19 17:16:21 +08:00

<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Chores** - Updated all Dart SDK constraints to "^3.6.0", added a unified workspace resolution key, and removed obsolete local dependency override configurations. - **CI/CD Improvements** - Streamlined workflows and refined testing/coverage scripts by removing outdated steps. - **Tooling Enhancements** - Introduced a new build generation script to automate build-runner tasks. - **Refactor** - Applied minor code style refinements, including simplified constructor syntax and streamlined control flow. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
74 lines
2.0 KiB
Dart
74 lines
2.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
void main() {
|
|
runApp(
|
|
/// [MyApp] is wrapped in a [ProviderScope].
|
|
/// This widget is where the state of most of our providers will be stored.
|
|
/// This replaces `MultiProvider` if you've used `provider` before.
|
|
const ProviderScope(
|
|
child: MyApp(),
|
|
),
|
|
);
|
|
}
|
|
|
|
/// A provider that creates and listen to a [StateNotifier].
|
|
///
|
|
/// Providers are declared as global variables.
|
|
/// This does not hinder testability, as the state of a provider is instead
|
|
/// stored inside a [ProviderScope].
|
|
final counterProvider = StateNotifierProvider<Counter, int>((_) => Counter());
|
|
|
|
/// A simple [StateNotifier] that implements a counter.
|
|
///
|
|
/// It doesn't have to be a [StateNotifier], and could be anything else such as:
|
|
/// - [ChangeNotifier], with [ChangeNotifierProvider]
|
|
/// - [Stream], with [StreamProvider]
|
|
/// ...
|
|
class Counter extends StateNotifier<int> {
|
|
Counter() : super(0);
|
|
|
|
void increment() => state++;
|
|
}
|
|
|
|
class MyApp extends StatelessWidget {
|
|
const MyApp({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return const MaterialApp(
|
|
home: MyHomePage(),
|
|
);
|
|
}
|
|
}
|
|
|
|
class MyHomePage extends HookConsumerWidget {
|
|
const MyHomePage({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Riverpod counter example'),
|
|
),
|
|
body: Center(
|
|
// HookConsumer is a builder widget that allows you to read providers and utilise hooks.
|
|
child: HookConsumer(
|
|
builder: (context, ref, _) {
|
|
final count = ref.watch(counterProvider);
|
|
|
|
return Text(
|
|
'$count',
|
|
style: Theme.of(context).textTheme.headlineMedium,
|
|
);
|
|
},
|
|
),
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
onPressed: () => ref.read(counterProvider.notifier).increment(),
|
|
child: const Icon(Icons.add),
|
|
),
|
|
);
|
|
}
|
|
}
|