Skip to content
Snippets Groups Projects
Commit 18c3596a authored by Adrien BONNIN's avatar Adrien BONNIN
Browse files

:sparkles: Ajout des examples pour la présentation

parent b3f83953
No related branches found
No related tags found
No related merge requests found
Pipeline #78764 passed
...@@ -3,6 +3,7 @@ import 'package:article_flutter_riverpod/article_02/consumer_example.dart'; ...@@ -3,6 +3,7 @@ import 'package:article_flutter_riverpod/article_02/consumer_example.dart';
import 'package:article_flutter_riverpod/article_02/parent_example.dart'; import 'package:article_flutter_riverpod/article_02/parent_example.dart';
import 'package:article_flutter_riverpod/article_02/stateful_example.dart'; import 'package:article_flutter_riverpod/article_02/stateful_example.dart';
import 'package:article_flutter_riverpod/article_02/stateless_example.dart'; import 'package:article_flutter_riverpod/article_02/stateless_example.dart';
import 'package:article_flutter_riverpod/presentation/smaller_stateful_example.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
...@@ -25,6 +26,12 @@ final examples = [ ...@@ -25,6 +26,12 @@ final examples = [
path: 'stateful', path: 'stateful',
builder: (_) => const StatefulExample(), builder: (_) => const StatefulExample(),
), ),
Example(
title: 'Stateful (smaller)',
icon: Icons.add_circle_outline,
path: 'smaller-stateful',
builder: (_) => const SmallerStatefulExample(),
),
Example( Example(
title: 'Parent ProviderScope', title: 'Parent ProviderScope',
icon: Icons.child_care, icon: Icons.child_care,
......
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
final countServiceProvider = StateNotifierProvider<CountService, int>((ref) => CountService(42)); // <1>
class CountService extends StateNotifier<int> {
CountService(int firstValue) : super(firstValue);
void increment() {
state++;
}
}
class SmallerStatefulExample extends StatelessWidget {
const SmallerStatefulExample({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const ProviderScope(
child: Counter(),
);
}
}
class Counter extends ConsumerStatefulWidget { // <2>
const Counter({Key? key}) : super(key: key);
@override
ConsumerState<Counter> createState() => _CounterState(); // <3>
}
class _CounterState extends ConsumerState<Counter> { // <4>
late int firstValue;
@override
void initState() {
super.initState();
firstValue = ref.read(countServiceProvider); // <5>
}
@override
Widget build(BuildContext context) { // <6>
final currentValue = ref.watch(countServiceProvider); // <7>
return Column(
mainAxisSize: MainAxisSize.min,
children: [
ElevatedButton( // <8>
onPressed: () => ref.read(countServiceProvider.notifier).increment(), // <9>
child: Text("Increment $firstValue -> $currentValue"),
),
],
);
}
}
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import '../utils.dart';
final intProvider = StateProvider<int>((_) => 13); // <1>
void main() {
test('doit écouter un état', () {
const defaultValue = -1;
// given:
final container = createContainer();
var intValue = defaultValue; // <2>
container.listen<int>(intProvider, (_, next) => intValue = next);
// expect:
expect(intValue, equals(defaultValue)); // <3>
// when:
container.read(intProvider.notifier).state = 42; // <4>
// then:
expect(intValue, equals(42)); // <5>
});
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment