-
-
Save dadagov125/ddcdffa96e65a3ccc3ae3027250532cc to your computer and use it in GitHub Desktop.
Provider.shared
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:go_router/go_router.dart'; | |
import 'package:provider/provider.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
final _router = GoRouter( | |
routes: [ | |
GoRoute( | |
path: '/', | |
builder: (context, state) => MyHomePage(title: 'Home'), | |
), | |
GoRoute( | |
path: '/a', | |
builder: (context, state) => ScreenA(), | |
), | |
GoRoute( | |
path: '/b', | |
builder: (context, state) => ScreenB(), | |
), | |
], | |
); | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp.router( | |
routerConfig: _router, | |
title: 'Home', | |
theme: ThemeData( | |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), | |
useMaterial3: true, | |
), | |
); | |
} | |
} | |
class MyHomePage extends StatelessWidget { | |
const MyHomePage({super.key, required this.title}); | |
final String title; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
backgroundColor: Theme.of(context).colorScheme.inversePrimary, | |
title: Text(title), | |
), | |
body: Placeholder(), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
context.push('/a'); | |
}, | |
tooltip: 'to Screeb A', | |
child: Text('to A'), | |
) // This trailing comma makes auto-formatting nicer for build methods. | |
); | |
} | |
} | |
class ScreenA extends StatelessWidget { | |
const ScreenA({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('ScreenA'), | |
), | |
body: Center( | |
child: Provider.shared( | |
create: (_) => 'This text was created on screen [A]', | |
dispose: (context, value) { | |
print('dispose on screen A:\nshared value => ${value}'); | |
}, | |
child: Consumer<String>(builder: (context, value, child) { | |
return Text(value); | |
}), | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
context.push('/b'); | |
}, | |
tooltip: 'to Screen B', | |
child: Text('to B'), | |
// child: const Icon(Icons.add), | |
)); | |
} | |
} | |
class ScreenB extends StatelessWidget { | |
const ScreenB({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('ScreenB'), | |
), | |
body: Center( | |
child: Provider.shared( | |
create: (_) => 'This text was created on screen [B]', | |
dispose: (context, value) { | |
print('dispose on screen B:\nshared value => ${value}'); | |
}, | |
child: Consumer<String>(builder: (context, value, child) { | |
return Text(value); | |
}), | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
context.go('/'); | |
}, | |
tooltip: 'to Screen A', | |
child: Text('to Home'), | |
// child: const Icon(Icons.add), | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment