Last active
December 20, 2018 00:54
-
-
Save filiph/e0b12abd70522a70464751bc2bc2ba86 to your computer and use it in GitHub Desktop.
The starter Flutter app rewritten using 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() { | |
// Initialize the model. Can be done outside a widget, like here. | |
var counter = Counter(); | |
// Just because we can: wait five seconds after the start of the app ... | |
Timer( | |
const Duration(seconds: 5), | |
// ... then change the state of the model from outside of the widget tree. | |
() => counter.increment(), | |
); | |
runApp( | |
// Provide the model to our app. | |
ScopedModel( | |
model: counter, | |
child: MyApp(), | |
), | |
); | |
} | |
class Counter extends Model { | |
int value = 0; | |
void increment() { | |
value += 1; | |
notifyListeners(); | |
} | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Flutter Demo Home Page'), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
'You have pushed the button this many times:', | |
), | |
// Use the model and rebuild this part of tree when it changes. | |
ScopedModelDescendant<Counter>( | |
builder: (context, child, counter) => Text( | |
'${counter.value}', | |
style: Theme.of(context).textTheme.display1, | |
), | |
), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
// Access the model. | |
onPressed: () => ScopedModel.of<Counter>(context).increment(), | |
tooltip: 'Increment', | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment