Skip to content

Instantly share code, notes, and snippets.

@andrewpmoore
Last active April 13, 2018 09:41
Show Gist options
  • Save andrewpmoore/2a69fe144a065b927ec64f641e9452d4 to your computer and use it in GitHub Desktop.
Save andrewpmoore/2a69fe144a065b927ec64f641e9452d4 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
class AppModel extends Model {
int _count = 0;
int get count => _count;
void increment() {
_count++;
notifyListeners();
}
void decrement() {
_count--;
notifyListeners();
}
}
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Counter Example',
theme: ThemeData.dark(),
home: new ScopedModel<AppModel>(
model: new AppModel(),
child: new FirstScreen(),
),
);
}
}
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('First Screen'),
),
body: new Center(
child: new RaisedButton(
child: new Text('Launch new screen'),
onPressed: () {
Navigator.push(
context,
new MaterialPageRoute(builder: (context) => new Home()),
);
}),
),
);
}
}
class Home extends StatelessWidget {
final AppModel appModelOne = new AppModel();
final AppModel appModelTwo = new AppModel();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title: new Text('Basic Counter'),
),
body: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
ScopedModel<AppModel>(
model: appModelOne,
child: new Counter(
counterName: 'App Model One',
),
),
ScopedModel<AppModel>(
model: appModelTwo,
child: Counter(
counterName: 'App Model Two',
),
),
],
),
));
}
}
class Counter extends StatelessWidget {
final String counterName;
Counter({Key key, this.counterName});
@override
Widget build(BuildContext context) {
return ScopedModelDescendant<AppModel>(
builder: (context, child, model) => Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text("$counterName:"),
new Text(
model.count.toString(),
style: Theme.of(context).textTheme.display1,
),
new ButtonBar(
children: <Widget>[
new IconButton(
icon: new Icon(Icons.add),
onPressed: model.increment,
),
new IconButton(
icon: new Icon(Icons.minimize),
onPressed: model.decrement,
)
],
)
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment