Skip to content

Instantly share code, notes, and snippets.

@suragch
Last active March 31, 2021 04:27
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 suragch/06c531aa12ae5196511fa9594cdada5e to your computer and use it in GitHub Desktop.
Save suragch/06c531aa12ae5196511fa9594cdada5e to your computer and use it in GitHub Desktop.
View for the Provider Architecture example
import 'package:flutter/material.dart';
import 'package:stacked/stacked.dart';
import 'counter_viewmodel.dart';
// Since the state was moved to the view model, this is now a StatelessWidget.
class CounterScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
// ViewModelBuilder is what provides the view model to the widget tree.
return ViewModelBuilder<CounterViewModel>.reactive(
viewModelBuilder: () => CounterViewModel(),
builder: (context, model, child) => 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:',
),
Text(
'${model.counter}', // <-- view model
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
model.increment(); // <-- view model
},
tooltip: 'Increment',
child: Icon(Icons.add),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment