Skip to content

Instantly share code, notes, and snippets.

@letsar
Created July 16, 2019 06:59
Show Gist options
  • Save letsar/3663013295a3253a1dc1da5d8cd8cce8 to your computer and use it in GitHub Desktop.
Save letsar/3663013295a3253a1dc1da5d8cd8cce8 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
builder: (_) => ValueNotifier<String>(null),
child: MaterialApp(
title: 'Example',
home: A(),
),
);
}
}
class A extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('A')),
body: Center(
child: RaisedButton(
child: Text('Page B'),
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (_) => B()));
},
),
),
);
}
}
class B extends StatelessWidget {
@override
Widget build(BuildContext context) {
final notifier = Provider.of<ValueNotifier<String>>(
context,
);
return Scaffold(
appBar: AppBar(title: Text('B')),
body: Center(
child: Column(
children: <Widget>[
DropdownButton<String>(
value: notifier.value,
items: ['a', 'b', 'c']
.map(
(s) => DropdownMenuItem<String>(
child: Text(s),
value: s,
),
)
.toList(),
onChanged: (value) {
notifier.value = value;
},
),
RaisedButton(
child: Text('Page C'),
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (_) => C()));
},
),
],
),
),
);
}
}
class C extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('C')),
body: Center(
child: Text(Provider.of<ValueNotifier<String>>(context).value),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment