mirror of https://github.com/flutter/samples.git
[place_tracker] ChangeNotifierProvider for state management (#424)
parent
af5be70f34
commit
084c532ac0
@ -1,67 +0,0 @@
|
|||||||
import 'package:flutter/material.dart';
|
|
||||||
|
|
||||||
class _AppModelScope<T> extends InheritedWidget {
|
|
||||||
const _AppModelScope({
|
|
||||||
Key key,
|
|
||||||
this.appModelState,
|
|
||||||
Widget child,
|
|
||||||
}) : super(key: key, child: child);
|
|
||||||
|
|
||||||
final _AppModelState<T> appModelState;
|
|
||||||
|
|
||||||
@override
|
|
||||||
bool updateShouldNotify(_AppModelScope oldWidget) => true;
|
|
||||||
}
|
|
||||||
|
|
||||||
class AppModel<T> extends StatefulWidget {
|
|
||||||
AppModel({
|
|
||||||
Key key,
|
|
||||||
@required this.initialState,
|
|
||||||
this.child,
|
|
||||||
}) : assert(initialState != null),
|
|
||||||
super(key: key);
|
|
||||||
|
|
||||||
final T initialState;
|
|
||||||
final Widget child;
|
|
||||||
|
|
||||||
@override
|
|
||||||
_AppModelState<T> createState() => _AppModelState<T>();
|
|
||||||
|
|
||||||
static T of<T>(BuildContext context) {
|
|
||||||
final scope =
|
|
||||||
context.dependOnInheritedWidgetOfExactType<_AppModelScope<T>>();
|
|
||||||
return scope.appModelState.currentState;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void update<T>(BuildContext context, T newState) {
|
|
||||||
final scope =
|
|
||||||
context.dependOnInheritedWidgetOfExactType<_AppModelScope<T>>();
|
|
||||||
scope.appModelState.updateState(newState);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class _AppModelState<T> extends State<AppModel<T>> {
|
|
||||||
@override
|
|
||||||
void initState() {
|
|
||||||
super.initState();
|
|
||||||
currentState = widget.initialState;
|
|
||||||
}
|
|
||||||
|
|
||||||
T currentState;
|
|
||||||
|
|
||||||
void updateState(T newState) {
|
|
||||||
if (newState != currentState) {
|
|
||||||
setState(() {
|
|
||||||
currentState = newState;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
return _AppModelScope<T>(
|
|
||||||
appModelState: this,
|
|
||||||
child: widget.child,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +1,11 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
import 'place_tracker_app.dart';
|
import 'place_tracker_app.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(PlaceTrackerApp());
|
runApp(ChangeNotifierProvider(
|
||||||
|
create: (context) => AppState(),
|
||||||
|
child: PlaceTrackerApp(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue