Skip to content

Instantly share code, notes, and snippets.

@landon-sf
Created May 26, 2019 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save landon-sf/23d809015de7abd82da1ef91113eeae2 to your computer and use it in GitHub Desktop.
Save landon-sf/23d809015de7abd82da1ef91113eeae2 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 {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Home(),
);
}
}
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
builder: (context) => SomeModel(),
child: Scaffold(
appBar: AppBar(
title: Text('Provider Poc'),
),
body: Consumer<SomeModel>(
builder: (context, someModel, _) => RaisedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => Provider.value(
value: someModel,
child: OtherWidget(),
),
));
},
child:
Text('Go to Other widget', style: TextStyle(fontSize: 20)),
),
),
),
);
}
}
class OtherWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
var sm = Provider.of<SomeModel>(context);
return Scaffold(
appBar: AppBar(
title: Text('Provider POC'),
),
floatingActionButton: FloatingActionButton(onPressed: () {
sm.somethingEnabled = !sm.somethingEnabled;
}),
body: Padding(
padding: EdgeInsets.all(20),
child: Row(
children: <Widget>[
Checkbox(
value: sm.somethingEnabled,
onChanged: (bool value) {},
),
],
),
),
);
}
}
class SomeModel extends ChangeNotifier {
var _somethingEnabled = false;
get somethingEnabled => _somethingEnabled;
set somethingEnabled(bool v) {
_somethingEnabled = v;
notifyListeners();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment