Created
September 26, 2018 10:29
-
-
Save MariaMelnik/bde084e3fed197f6eae775cfb308bae3 to your computer and use it in GitHub Desktop.
flutter example navigation with scopedModel
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 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'package:scoped_model/scoped_model.dart'; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
title: 'Flutter Demo', | |
theme: new ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: new MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatelessWidget { | |
final String title; | |
MyHomePage({Key key, this.title}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text(title), | |
), | |
body: FutureBuilder( | |
future: getFuture(), | |
builder: (context, snapshot) => _buildView(snapshot.data, context), | |
), | |
); | |
} | |
Widget _buildView(String data, BuildContext context) { | |
return ScopedModel<MyModel>( | |
model: MyModel(data), | |
child: new Center( | |
child: new Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
ScopedModelDescendant<MyModel>( | |
builder: (context, child, model) => | |
RaisedButton( | |
onPressed: () => goNext(context, model._name, model), | |
child: Text("go next"), | |
), | |
), | |
ScopedModelDescendant<MyModel>( | |
builder: (context, child, model) => | |
Text("${model._names.length}") | |
) | |
], | |
), | |
), | |
); | |
} | |
void goNext(BuildContext context, String titleFromFuture, MyModel model){ | |
Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondScreen(title: titleFromFuture, model: model,))); | |
} | |
Future<String> getFuture() async { | |
await Future.delayed(Duration(seconds: 2)); | |
return "Screen with model"; | |
} | |
} | |
class SecondScreen extends StatelessWidget { | |
final String title; | |
final MyModel model; | |
SecondScreen({Key key, this.title, this.model}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return ScopedModel<MyModel>( | |
model: model, | |
child: new Scaffold( | |
appBar: new AppBar( | |
title: new Text(title), | |
), | |
body: new Center( | |
child: new Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
RaisedButton( | |
onPressed: () => showMyDialog(title, context, model), | |
child: Text("open dialog"), | |
), | |
ScopedModelDescendant<MyModel>( | |
builder: (context, child, model) => | |
Text("${model._names.length}") | |
) | |
], | |
), | |
), | |
), | |
); | |
} | |
void showMyDialog(String name, BuildContext cont, MyModel model){ | |
showDialog( | |
context: cont, | |
builder: (context) => AlertDialog( | |
title: Text(name), | |
actions: <Widget>[ | |
RaisedButton( | |
onPressed: () { | |
Navigator.of(context).pop(); | |
model.addCallback(name); | |
}, | |
child: Text("Ok"), | |
) | |
], | |
) | |
); | |
} | |
} | |
class MyModel extends Model{ | |
final String _name; | |
final List<String> _names = List<String>(); | |
MyModel(this._name); | |
void addCallback(String name){ | |
_names.add(name); | |
notifyListeners(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment