1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-08 13:58:47 +00:00
Files
samples/place_tracker/lib/app_model.dart
Kenzie Schmoll 79d9d143b4 Add a list view to the place tracker app. (#30)
* Add list view to place tracker. Note: map in listTile is not WAI in this commit.

* Remove map from list tiles. Make list tiles tappable (currently editing a place and saving will do nothing if the details screen is pushed from the list view.

* Fix text alignment in list.

* Initial implementation of using an InheritedWidget to maintain data between list and map. Map does not update correctly at this point.

* Use AppModel.update to set the AppState. Add MapConfiguration class to handle map changes based on AppState.

* Don't cache AppState - lookup directly. Extract AppState code into it's own file and add static methods. Address comments from Hans.

* Extract generic AppModel code.
2018-11-13 10:39:06 -08:00

69 lines
1.6 KiB
Dart

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;
_AppModelState<T> createState() => _AppModelState<T>();
static _typeOf<T>() => T;
static T of<T>(BuildContext context) {
final Type appModelScopeType = _typeOf<_AppModelScope<T>>();
final _AppModelScope<T> scope = context.inheritFromWidgetOfExactType(appModelScopeType);
return scope.appModelState.currentState;
}
static void update<T>(BuildContext context, T newState) {
final Type appModelScopeType = _typeOf<_AppModelScope<T>>();
final _AppModelScope<T> scope = context.inheritFromWidgetOfExactType(appModelScopeType);
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,
);
}
}