Skip to content

Commit 2e34742

Browse files
committed
Riverpod functional
1 parent 5ef5017 commit 2e34742

8 files changed

+238
-5
lines changed

Diff for: .flutter-plugins-dependencies

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"shared_preferences_ios","path":"C:\\\\Users\\\\jeff\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences_ios-2.1.1\\\\","native_build":true,"dependencies":[]}],"android":[{"name":"shared_preferences_android","path":"C:\\\\Users\\\\jeff\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences_android-2.0.12\\\\","native_build":true,"dependencies":[]}],"macos":[{"name":"shared_preferences_macos","path":"C:\\\\Users\\\\jeff\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences_macos-2.0.4\\\\","native_build":true,"dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"C:\\\\Users\\\\jeff\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dartlang.org\\\\path_provider_linux-2.1.7\\\\","native_build":false,"dependencies":[]},{"name":"shared_preferences_linux","path":"C:\\\\Users\\\\jeff\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences_linux-2.1.1\\\\","native_build":false,"dependencies":["path_provider_linux"]}],"windows":[{"name":"path_provider_windows","path":"C:\\\\Users\\\\jeff\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dartlang.org\\\\path_provider_windows-2.1.2\\\\","native_build":false,"dependencies":[]},{"name":"shared_preferences_windows","path":"C:\\\\Users\\\\jeff\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences_windows-2.1.1\\\\","native_build":false,"dependencies":["path_provider_windows"]}],"web":[{"name":"shared_preferences_web","path":"C:\\\\Users\\\\jeff\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences_web-2.0.4\\\\","dependencies":[]}]},"dependencyGraph":[{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_windows","dependencies":[]},{"name":"shared_preferences","dependencies":["shared_preferences_android","shared_preferences_ios","shared_preferences_linux","shared_preferences_macos","shared_preferences_web","shared_preferences_windows"]},{"name":"shared_preferences_android","dependencies":[]},{"name":"shared_preferences_ios","dependencies":[]},{"name":"shared_preferences_linux","dependencies":["path_provider_linux"]},{"name":"shared_preferences_macos","dependencies":[]},{"name":"shared_preferences_web","dependencies":[]},{"name":"shared_preferences_windows","dependencies":["path_provider_windows"]}],"date_created":"2022-08-17 15:09:26.892900","version":"3.0.5"}
1+
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"shared_preferences_ios","path":"C:\\\\Users\\\\jeff\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences_ios-2.1.1\\\\","native_build":true,"dependencies":[]}],"android":[{"name":"shared_preferences_android","path":"C:\\\\Users\\\\jeff\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences_android-2.0.12\\\\","native_build":true,"dependencies":[]}],"macos":[{"name":"shared_preferences_macos","path":"C:\\\\Users\\\\jeff\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences_macos-2.0.4\\\\","native_build":true,"dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"C:\\\\Users\\\\jeff\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dartlang.org\\\\path_provider_linux-2.1.7\\\\","native_build":false,"dependencies":[]},{"name":"shared_preferences_linux","path":"C:\\\\Users\\\\jeff\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences_linux-2.1.1\\\\","native_build":false,"dependencies":["path_provider_linux"]}],"windows":[{"name":"path_provider_windows","path":"C:\\\\Users\\\\jeff\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dartlang.org\\\\path_provider_windows-2.1.2\\\\","native_build":false,"dependencies":[]},{"name":"shared_preferences_windows","path":"C:\\\\Users\\\\jeff\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences_windows-2.1.1\\\\","native_build":false,"dependencies":["path_provider_windows"]}],"web":[{"name":"shared_preferences_web","path":"C:\\\\Users\\\\jeff\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences_web-2.0.4\\\\","dependencies":[]}]},"dependencyGraph":[{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_windows","dependencies":[]},{"name":"shared_preferences","dependencies":["shared_preferences_android","shared_preferences_ios","shared_preferences_linux","shared_preferences_macos","shared_preferences_web","shared_preferences_windows"]},{"name":"shared_preferences_android","dependencies":[]},{"name":"shared_preferences_ios","dependencies":[]},{"name":"shared_preferences_linux","dependencies":["path_provider_linux"]},{"name":"shared_preferences_macos","dependencies":[]},{"name":"shared_preferences_web","dependencies":[]},{"name":"shared_preferences_windows","dependencies":["path_provider_windows"]}],"date_created":"2022-08-18 11:34:55.767186","version":"3.0.5"}

Diff for: lib/RiverpodExample/riverpod.page.dart

Whitespace-only changes.

Diff for: lib/RiverpodExample/riverpod_main.dart

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_riverpod/flutter_riverpod.dart';
3+
4+
import 'riverpod_page.dart';
5+
import 'riverpod_model.dart';
6+
7+
// riverpod provider declared globally
8+
final riverpodProvider = StateNotifierProvider<RiverpodNotifier, DiceState>(
9+
(ref) => RiverpodNotifier());
10+
11+
class RiverpodMain extends StatelessWidget {
12+
final String appName = 'Flutter Dice - Riverpod';
13+
14+
@override
15+
Widget build(BuildContext context) {
16+
return ProviderScope(
17+
child: RiverpodPage(appName),
18+
);
19+
}
20+
}
21+
22+
// /// Providers are declared globally and specify how to create a state
23+
// //final counterProvider = StateProvider((ref) => DiceState(roll: 1, sides: 6));
24+
// final counterProvider = StateProvider((ref) => RiverpodNotifier());
25+
26+
// class Home extends ConsumerWidget {
27+
// @override
28+
// Widget build(BuildContext context, WidgetRef ref) {
29+
// final dice = ref.watch(counterProvider.state).state;
30+
31+
// return Scaffold(
32+
// appBar: AppBar(title: const Text('Counter example')),
33+
// body: Center(
34+
// // Consumer is a widget that allows you reading providers.
35+
// child: Consumer(builder: (context, ref, _) {
36+
// final count = dice.roll;
37+
// return Text('$count');
38+
// }),
39+
// ),
40+
// floatingActionButton: FloatingActionButton(
41+
// // The read method is a utility to read a provider without listening to it
42+
// onPressed: () => {
43+
// //var xxx = ref.read(counterProvider.state).state;
44+
// dice.incrementDice()
45+
// // ref.read(counterProvider.state).state.incrementDice()
46+
// //DiceState(roll: dice.roll + 1, sides: 6)
47+
// },
48+
// child: const Icon(Icons.add),
49+
// ),
50+
// );
51+
// }
52+
// }
53+
54+
// class Home extends ConsumerWidget {
55+
// @override
56+
// Widget build(BuildContext context, WidgetRef ref) {
57+
// final dice = ref.watch(counterProvider.state).state;
58+
59+
// return Scaffold(
60+
// appBar: AppBar(title: const Text('Counter example')),
61+
// body: Center(
62+
// // Consumer is a widget that allows you reading providers.
63+
// child: Consumer(builder: (context, ref, _) {
64+
// final count = dice.roll;
65+
// return Text('$count');
66+
// }),
67+
// ),
68+
// floatingActionButton: FloatingActionButton(
69+
// // The read method is a utility to read a provider without listening to it
70+
// onPressed: () => {
71+
// //var xxx = ref.read(counterProvider.state).state;
72+
// ref.read(counterProvider.state).state =
73+
// DiceState(roll: dice.roll + 1, sides: 6)
74+
// },
75+
// child: const Icon(Icons.add),
76+
// ),
77+
// );
78+
// }
79+
// }

Diff for: lib/RiverpodExample/riverpod_model.dart

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import 'dart:async';
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter_dice/prefs_singleton.dart';
4+
import 'dart:math';
5+
6+
import 'package:flutter_riverpod/flutter_riverpod.dart';
7+
8+
class PreferenceNames {
9+
static const roll = "roll";
10+
static const sides = "sides";
11+
}
12+
13+
@immutable
14+
class DiceState {
15+
const DiceState({
16+
required this.roll,
17+
required this.sides,
18+
});
19+
20+
final int roll;
21+
final int sides;
22+
23+
DiceState copyWith({int? roll, int? sides}) =>
24+
DiceState(roll: roll ?? this.roll, sides: sides ?? this.sides);
25+
26+
// Persistence Functions
27+
static DiceState loadState() {
28+
int sides = (PrefsSingleton.prefs?.getInt(PreferenceNames.sides) ?? 6);
29+
int roll = (PrefsSingleton.prefs?.getInt(PreferenceNames.roll) ?? 1);
30+
return DiceState(roll: roll, sides: sides);
31+
}
32+
33+
Future saveState() async {
34+
await PrefsSingleton.prefs?.setInt(PreferenceNames.sides, sides);
35+
await PrefsSingleton.prefs?.setInt(PreferenceNames.roll, roll);
36+
}
37+
}
38+
39+
class RiverpodNotifier extends StateNotifier<DiceState> {
40+
RiverpodNotifier() : super(DiceState.loadState());
41+
42+
// Logic Functions
43+
Future changeSides(int value) async {
44+
state = state.copyWith(sides: value);
45+
await rollDice();
46+
}
47+
48+
Future rollDice() async {
49+
var sides = state.sides;
50+
_delayedRoll(sides, 100);
51+
_delayedRoll(sides, 100);
52+
_delayedRoll(sides, 300);
53+
_delayedRoll(sides, 400);
54+
_delayedRoll(sides, 500);
55+
}
56+
57+
void _delayedRoll(int sides, int delay) {
58+
Timer(Duration(milliseconds: delay), () {
59+
int value = Random().nextInt(sides) + 1;
60+
state = state.copyWith(roll: value);
61+
state.saveState();
62+
});
63+
}
64+
65+
void incrementDice() {
66+
int value = min(state.roll + 1, state.sides);
67+
state = state.copyWith(roll: value);
68+
state.saveState();
69+
}
70+
71+
void decrementDice() {
72+
int value = max(state.roll - 1, 1);
73+
state = state.copyWith(roll: value);
74+
state.saveState();
75+
}
76+
}

Diff for: lib/RiverpodExample/riverpod_page.dart

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_dice/widgets/action_buttons.dart';
3+
import 'package:flutter_dice/widgets/dice_display.dart';
4+
import 'package:flutter_dice/widgets/dice_buttons.dart';
5+
import 'package:flutter_riverpod/flutter_riverpod.dart';
6+
7+
import 'riverpod_main.dart';
8+
9+
class RiverpodPage extends ConsumerWidget {
10+
RiverpodPage(this.title);
11+
12+
final String title;
13+
14+
@override
15+
Widget build(BuildContext context, WidgetRef ref) {
16+
final dice = ref.watch(riverpodProvider);
17+
final diceNotifier = ref.read(riverpodProvider.notifier);
18+
19+
return Scaffold(
20+
appBar: AppBar(
21+
title: Text(title),
22+
),
23+
body: Container(
24+
padding: EdgeInsets.all(20.0),
25+
child: Stack(
26+
children: <Widget>[
27+
DiceDisplay(
28+
roll: dice.roll,
29+
sides: dice.sides,
30+
),
31+
DiceButtons(
32+
onDicePressed: diceNotifier.changeSides,
33+
selectedSides: dice.sides,
34+
),
35+
ActionButtons(
36+
onDecrementPressed: () => diceNotifier.decrementDice(),
37+
onIncrementPressed: () => diceNotifier.incrementDice(),
38+
onRollPressed: () => diceNotifier.rollDice(),
39+
),
40+
],
41+
),
42+
),
43+
);
44+
}
45+
}

Diff for: lib/main.dart

+15-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:shared_preferences/shared_preferences.dart';
33

44
import 'ProviderExample/provider_main.dart';
55
import 'BLOCExample/bloc_main.dart';
6+
import 'RiverpodExample/riverpod_main.dart';
67
import 'prefs_singleton.dart';
78

89
void main() async {
@@ -26,7 +27,7 @@ class DiceApp extends StatelessWidget {
2627
}
2728

2829
class DiceScaffold extends StatelessWidget {
29-
final String appName = 'Flutter Dice Examples';
30+
final String appName = 'Flutter State Examples';
3031

3132
@override
3233
Widget build(BuildContext context) {
@@ -40,6 +41,8 @@ class DiceScaffold extends StatelessWidget {
4041
}
4142

4243
class DiceExamplesMenu extends StatelessWidget {
44+
var buttonStyle = ElevatedButton.styleFrom(minimumSize: const Size(200, 40));
45+
4346
@override
4447
Widget build(BuildContext context) {
4548
return Center(
@@ -48,6 +51,7 @@ class DiceExamplesMenu extends StatelessWidget {
4851
children: <Widget>[
4952
ElevatedButton(
5053
child: Text("BLOC"),
54+
style: buttonStyle,
5155
onPressed: () => Navigator.push(
5256
context,
5357
MaterialPageRoute<void>(
@@ -56,15 +60,22 @@ class DiceExamplesMenu extends StatelessWidget {
5660
),
5761
ElevatedButton(
5862
child: Text("Provider"),
63+
style: buttonStyle,
5964
onPressed: () => Navigator.push(
6065
context,
6166
MaterialPageRoute<void>(
6267
builder: (context) => ProviderMain(), maintainState: false),
6368
),
6469
),
65-
// ElevatedButton(
66-
// child: Text("RiverPod"),
67-
// ),
70+
ElevatedButton(
71+
child: Text("Riverpod"),
72+
style: buttonStyle,
73+
onPressed: () => Navigator.push(
74+
context,
75+
MaterialPageRoute<void>(
76+
builder: (context) => RiverpodMain(), maintainState: false),
77+
),
78+
),
6879
],
6980
),
7081
);

Diff for: pubspec.lock

+21
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ packages:
104104
url: "https://pub.dartlang.org"
105105
source: hosted
106106
version: "0.7.5"
107+
flutter_riverpod:
108+
dependency: "direct main"
109+
description:
110+
name: flutter_riverpod
111+
url: "https://pub.dartlang.org"
112+
source: hosted
113+
version: "2.0.0-dev.9"
107114
flutter_test:
108115
dependency: "direct dev"
109116
description: flutter
@@ -219,6 +226,13 @@ packages:
219226
url: "https://pub.dartlang.org"
220227
source: hosted
221228
version: "6.0.2"
229+
riverpod:
230+
dependency: transitive
231+
description:
232+
name: riverpod
233+
url: "https://pub.dartlang.org"
234+
source: hosted
235+
version: "2.0.0-dev.9"
222236
rxdart:
223237
dependency: "direct main"
224238
description:
@@ -301,6 +315,13 @@ packages:
301315
url: "https://pub.dartlang.org"
302316
source: hosted
303317
version: "1.10.0"
318+
state_notifier:
319+
dependency: transitive
320+
description:
321+
name: state_notifier
322+
url: "https://pub.dartlang.org"
323+
source: hosted
324+
version: "0.7.2+1"
304325
stream_channel:
305326
dependency: transitive
306327
description:

Diff for: pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ dependencies:
88
rxdart: ^0.27.3
99
shared_preferences: ^2.0.15
1010
provider: ^6.0.2
11+
flutter_riverpod: ^2.0.0-dev.9
1112

1213
dev_dependencies:
1314
flutter_test:

0 commit comments

Comments
 (0)