Created
May 26, 2019 00:46
-
-
Save landon-sf/23d809015de7abd82da1ef91113eeae2 to your computer and use it in GitHub Desktop.
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: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