-
-
Notifications
You must be signed in to change notification settings - Fork 974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add simple example on how to listen other providers #3871
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,125 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
--- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
title: listen other providers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
version: 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
--- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { Link } from "/src/components/Link"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { AutoSnippet, When } from "/src/components/CodeSnippet"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
```dart | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import 'package:flutter/material.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void main() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
runApp(const ProviderScope(child: MyApp())); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
final minutesProvider = NotifierProvider<MinutesNotifer, int>(() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return MinutesNotifer(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
final secondProvider = NotifierProvider<SecondsNotifer, int>(() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return SecondsNotifer(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class MinutesNotifer extends Notifier<int> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
int build() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ref.listen(secondProvider, (prev, next) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (prev == 59 && next == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
increment(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void increment() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var nextState = state; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
nextState = nextState + 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (nextState == 60) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
nextState = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
state = nextState; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class SecondsNotifer extends Notifier<int> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
int build() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void increment() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var nextState = state; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
nextState = nextState + 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (nextState == 60) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
nextState = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
state = nextState; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
int value() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return state; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+46
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Remove unnecessary value() method and simplify increment logic
class SecondsNotifer extends Notifier<int> {
@override
int build() {
return 0;
}
void increment() {
- var nextState = state;
- nextState = nextState + 1;
- if (nextState == 60) {
- nextState = 0;
- }
- state = nextState;
+ state = (state + 1) % 60;
}
- int value() {
- return state;
- }
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class MyApp extends StatelessWidget { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const MyApp({super.key}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Widget build(BuildContext context) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return MaterialApp( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
title: 'Flutter Demo', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
theme: ThemeData( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
useMaterial3: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
home: const MyHomePage(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class MyHomePage extends ConsumerWidget { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const MyHomePage({super.key}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Widget build(BuildContext context, WidgetRef ref) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var secondsNotifer = ref.watch(secondProvider.notifier); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var nbMinutes = ref.watch(minutesProvider); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return Scaffold( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
appBar: AppBar( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
title: const Text('The manual Clock'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
body: Center( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
child: Column( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mainAxisAlignment: MainAxisAlignment.center, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
children: <Widget>[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const Text( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'press the button to add seconds to the clock:', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Text( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'${ref.watch(secondProvider)}', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
style: Theme.of(context).textTheme.headlineMedium, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (nbMinutes != 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const Text( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'number of minutes:', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (nbMinutes != 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Text( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'$nbMinutes', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
style: Theme.of(context).textTheme.headlineMedium, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
floatingActionButton: FloatingActionButton( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
onPressed: secondsNotifer.increment, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
tooltip: 'Increment', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
child: const Icon(Icons.add), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix typo in class names
There's a typo in the class names:
MinutesNotifer
andSecondsNotifer
should beMinutesNotifier
andSecondsNotifier
(missing 'i' in "Notifier").📝 Committable suggestion