Skip to content

Instantly share code, notes, and snippets.

@wickstjo
Created October 6, 2022 08:45
Show Gist options
  • Save wickstjo/9dc216cd1079d5b0840abf1a890133ca to your computer and use it in GitHub Desktop.
Save wickstjo/9dc216cd1079d5b0840abf1a890133ca to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
final score_state = StateProvider<int>((ref) => 0);
main() {
runApp(
ProviderScope(child: MaterialApp.router(
debugShowCheckedModeBanner: false,
theme: ThemeData.dark(),
routerConfig: GoRouter(routes: [
GoRoute(
path: '/',
builder: (context, state) => Foo(),
),
GoRoute(
path: '/bar',
builder: (context, state) => Bar()
)
])
))
);
}
class Foo extends ConsumerWidget {
@override Widget build(BuildContext context, WidgetRef ref) {
// FETCH SCORE STATE
final score = ref.watch(score_state);
return Column(children: [
Text('$score'),
ElevatedButton(
onPressed: () => context.go('/bar'),
child: Text('GOTO UPDATER')
)
]);
}
}
class Bar extends ConsumerWidget {
void foo(WidgetRef ref) {
ref.watch(score_state.notifier).update((state) => state + 1);
}
@override Widget build(BuildContext context, WidgetRef ref) {
return Column(children: [
ElevatedButton(
onPressed: () => foo(ref),
child: Text('UPDATE')
),
ElevatedButton(
onPressed: () => context.go('/'),
child: Text('GOTO SUBSCRIBER')
)
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment