flutter_command 0.9.3 flutter_command: ^0.9.3 copied to clipboard
flutter_command is a way to manage your state based on `ValueListenable` and the `Command` design pattern.
import 'package:flutter/material.dart';
import 'package:meta/meta.dart';
import 'homepage.dart';
import 'weather_viewmodel.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
MyAppState createState() {
return MyAppState();
}
}
class MyAppState extends State<MyApp> {
WeatherViewModel viewModel;
@override
void initState() {
viewModel = WeatherViewModel();
super.initState();
}
@override
Widget build(BuildContext context) {
return TheViewModel(
theModel: viewModel,
child: MaterialApp(title: 'Flutter Demo', home: HomePage()),
);
}
}
// InheritedWidgets allow you to propagate values down the widgettree.
// it can then be accessed by just writing TheViewModel.of(context)
class TheViewModel extends InheritedWidget {
final WeatherViewModel theModel;
const TheViewModel({Key key, @required this.theModel, @required Widget child})
: assert(theModel != null),
assert(child != null),
super(key: key, child: child);
static WeatherViewModel of(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<TheViewModel>().theModel;
@override
bool updateShouldNotify(TheViewModel oldWidget) =>
theModel != oldWidget.theModel;
}