mirror of https://github.com/flutter/samples.git
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.pull/33/head
parent
e59c865884
commit
79d9d143b4
@ -0,0 +1,68 @@
|
|||||||
|
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,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -1,24 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'place_map.dart';
|
|
||||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
|
||||||
|
|
||||||
class _Home extends StatelessWidget {
|
import 'place_tracker_app.dart';
|
||||||
const _Home({ Key key }) : super(key: key);
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
return PlaceMap(
|
|
||||||
center: const LatLng(45.521563, -122.677433),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(
|
runApp(PlaceTrackerApp());
|
||||||
MaterialApp(
|
|
||||||
debugShowCheckedModeBanner: false,
|
|
||||||
title: 'Place Tracker',
|
|
||||||
home: _Home(),
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,209 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
import 'place.dart';
|
||||||
|
import 'place_details.dart';
|
||||||
|
import 'place_tracker_app.dart';
|
||||||
|
|
||||||
|
class PlaceList extends StatefulWidget {
|
||||||
|
const PlaceList({ Key key }) : super(key: key);
|
||||||
|
|
||||||
|
@override
|
||||||
|
PlaceListState createState() => PlaceListState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class PlaceListState extends State<PlaceList> {
|
||||||
|
ScrollController _scrollController = ScrollController();
|
||||||
|
|
||||||
|
void _onCategoryChanged(PlaceCategory newCategory) {
|
||||||
|
_scrollController.jumpTo(0.0);
|
||||||
|
AppState.updateWith(context, selectedCategory: newCategory);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _onPlaceChanged(Place value) {
|
||||||
|
// Replace the place with the modified version.
|
||||||
|
final List<Place> newPlaces = List.from(AppState.of(context).places);
|
||||||
|
final int index = newPlaces.indexWhere((Place place) => place.id == value.id);
|
||||||
|
newPlaces[index] = value;
|
||||||
|
|
||||||
|
AppState.updateWith(context, places: newPlaces);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Column(
|
||||||
|
children: <Widget>[
|
||||||
|
_ListCategoryButtonBar(
|
||||||
|
selectedCategory: AppState.of(context).selectedCategory,
|
||||||
|
onCategoryChanged: (value) => _onCategoryChanged(value),
|
||||||
|
),
|
||||||
|
Expanded(
|
||||||
|
child: ListView(
|
||||||
|
padding: const EdgeInsets.fromLTRB(16.0, 0.0, 16.0, 8.0),
|
||||||
|
controller: _scrollController,
|
||||||
|
shrinkWrap: true,
|
||||||
|
children: AppState.of(context).places
|
||||||
|
.where((Place place) => place.category == AppState.of(context).selectedCategory)
|
||||||
|
.map((Place place) => _PlaceListTile(
|
||||||
|
place: place,
|
||||||
|
onPlaceChanged: (Place value) => _onPlaceChanged(value),
|
||||||
|
)
|
||||||
|
).toList(),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _PlaceListTile extends StatelessWidget {
|
||||||
|
const _PlaceListTile({
|
||||||
|
Key key,
|
||||||
|
@required this.place,
|
||||||
|
@required this.onPlaceChanged,
|
||||||
|
}) : assert(place != null),
|
||||||
|
assert(onPlaceChanged != null),
|
||||||
|
super(key: key);
|
||||||
|
|
||||||
|
final Place place;
|
||||||
|
final ValueChanged<Place> onPlaceChanged;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return InkWell(
|
||||||
|
onTap: () => Navigator.push(
|
||||||
|
context,
|
||||||
|
MaterialPageRoute(builder: (context) {
|
||||||
|
return PlaceDetails(
|
||||||
|
place: place,
|
||||||
|
onChanged: (Place value) => onPlaceChanged(value),
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
child: Container(
|
||||||
|
padding: EdgeInsets.only(top: 16.0),
|
||||||
|
child: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: <Widget>[
|
||||||
|
Text(
|
||||||
|
place.name,
|
||||||
|
textAlign: TextAlign.left,
|
||||||
|
style: TextStyle(
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
fontSize: 20.0,
|
||||||
|
),
|
||||||
|
maxLines: 3,
|
||||||
|
),
|
||||||
|
Row(
|
||||||
|
children: List.generate(5, (int index) {
|
||||||
|
return Icon(Icons.star,
|
||||||
|
size: 28.0,
|
||||||
|
color: place.starRating > index ? Colors.amber : Colors.grey[400],
|
||||||
|
);
|
||||||
|
}).toList(),
|
||||||
|
),
|
||||||
|
Text(
|
||||||
|
place.description != null ? place.description : '',
|
||||||
|
style: Theme.of(context).textTheme.subhead,
|
||||||
|
maxLines: 4,
|
||||||
|
overflow: TextOverflow.ellipsis,
|
||||||
|
),
|
||||||
|
SizedBox(height: 16.0),
|
||||||
|
Divider(
|
||||||
|
height: 2.0,
|
||||||
|
color: Colors.grey[700],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _ListCategoryButtonBar extends StatelessWidget {
|
||||||
|
const _ListCategoryButtonBar({
|
||||||
|
Key key,
|
||||||
|
@required this.selectedCategory,
|
||||||
|
@required this.onCategoryChanged,
|
||||||
|
}) : assert(selectedCategory != null),
|
||||||
|
assert(onCategoryChanged != null),
|
||||||
|
super(key: key);
|
||||||
|
|
||||||
|
final PlaceCategory selectedCategory;
|
||||||
|
final ValueChanged<PlaceCategory> onCategoryChanged;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||||
|
children: <Widget>[
|
||||||
|
_CategoryButton(
|
||||||
|
category: PlaceCategory.favorite,
|
||||||
|
selected: selectedCategory == PlaceCategory.favorite,
|
||||||
|
onCategoryChanged: onCategoryChanged,
|
||||||
|
),_CategoryButton(
|
||||||
|
category: PlaceCategory.visited,
|
||||||
|
selected: selectedCategory == PlaceCategory.visited,
|
||||||
|
onCategoryChanged: onCategoryChanged,
|
||||||
|
),_CategoryButton(
|
||||||
|
category: PlaceCategory.wantToGo,
|
||||||
|
selected: selectedCategory == PlaceCategory.wantToGo,
|
||||||
|
onCategoryChanged: onCategoryChanged,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _CategoryButton extends StatelessWidget {
|
||||||
|
const _CategoryButton({
|
||||||
|
Key key,
|
||||||
|
@required this.category,
|
||||||
|
@required this.selected,
|
||||||
|
@required this.onCategoryChanged,
|
||||||
|
}) : assert(category != null),
|
||||||
|
assert(selected != null),
|
||||||
|
super(key: key);
|
||||||
|
|
||||||
|
final PlaceCategory category;
|
||||||
|
final bool selected;
|
||||||
|
final ValueChanged<PlaceCategory> onCategoryChanged;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
String _buttonText;
|
||||||
|
switch (category) {
|
||||||
|
case PlaceCategory.favorite:
|
||||||
|
_buttonText = 'Favorites';
|
||||||
|
break;
|
||||||
|
case PlaceCategory.visited:
|
||||||
|
_buttonText = 'Visited';
|
||||||
|
break;
|
||||||
|
case PlaceCategory.wantToGo:
|
||||||
|
_buttonText = 'Want To Go';
|
||||||
|
}
|
||||||
|
|
||||||
|
return Container(
|
||||||
|
margin: EdgeInsets.symmetric(vertical: 12.0),
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
border: Border(
|
||||||
|
bottom: BorderSide(
|
||||||
|
color: selected ? Colors.blue : Colors.transparent,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
child: ButtonTheme(
|
||||||
|
height: 50.0,
|
||||||
|
child: FlatButton(
|
||||||
|
child: Text(
|
||||||
|
_buttonText,
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: selected ? 20.0 : 18.0,
|
||||||
|
color: selected ? Colors.blue : Colors.black87,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
onPressed: () => onCategoryChanged(category),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,146 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
||||||
|
|
||||||
|
import 'app_model.dart';
|
||||||
|
import 'place.dart';
|
||||||
|
import 'place_list.dart';
|
||||||
|
import 'place_map.dart';
|
||||||
|
import 'stub_data.dart';
|
||||||
|
|
||||||
|
enum PlaceTrackerViewType {
|
||||||
|
map,
|
||||||
|
list,
|
||||||
|
}
|
||||||
|
|
||||||
|
class PlaceTrackerApp extends StatefulWidget {
|
||||||
|
@override
|
||||||
|
_PlaceTrackerAppState createState() => _PlaceTrackerAppState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _PlaceTrackerAppState extends State<PlaceTrackerApp> {
|
||||||
|
AppState appState = AppState();
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return MaterialApp(
|
||||||
|
builder: (BuildContext context, Widget child) {
|
||||||
|
return AppModel<AppState>(
|
||||||
|
initialState: AppState(),
|
||||||
|
child: child,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
home: _PlaceTrackerHomePage(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _PlaceTrackerHomePage extends StatelessWidget {
|
||||||
|
const _PlaceTrackerHomePage({ Key key }) : super(key: key);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(
|
||||||
|
title: Row(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.center,
|
||||||
|
children: const <Widget>[
|
||||||
|
Padding(
|
||||||
|
padding: EdgeInsets.fromLTRB(0.0, 0.0, 8.0, 0.0),
|
||||||
|
child: Icon(Icons.pin_drop, size: 24.0),
|
||||||
|
),
|
||||||
|
Text('Place Tracker'),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
backgroundColor: Colors.green[700],
|
||||||
|
actions: <Widget>[
|
||||||
|
Padding(
|
||||||
|
padding: EdgeInsets.fromLTRB(0.0, 0.0, 16.0, 0.0),
|
||||||
|
child: IconButton(
|
||||||
|
icon: Icon(
|
||||||
|
AppState.of(context).viewType == PlaceTrackerViewType.map
|
||||||
|
? Icons.list
|
||||||
|
: Icons.map,
|
||||||
|
size: 32.0
|
||||||
|
),
|
||||||
|
onPressed: () {
|
||||||
|
AppState.updateWith(
|
||||||
|
context,
|
||||||
|
viewType: AppState.of(context).viewType == PlaceTrackerViewType.map
|
||||||
|
? PlaceTrackerViewType.list
|
||||||
|
: PlaceTrackerViewType.map,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
body: IndexedStack(
|
||||||
|
index: AppState.of(context).viewType == PlaceTrackerViewType.map ? 0 : 1,
|
||||||
|
children: <Widget>[
|
||||||
|
PlaceMap(center: const LatLng(45.521563, -122.677433)),
|
||||||
|
PlaceList(),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class AppState {
|
||||||
|
const AppState({
|
||||||
|
this.places = StubData.places,
|
||||||
|
this.selectedCategory = PlaceCategory.favorite,
|
||||||
|
this.viewType = PlaceTrackerViewType.map,
|
||||||
|
}) : assert(places != null),
|
||||||
|
assert(selectedCategory != null);
|
||||||
|
|
||||||
|
final List<Place> places;
|
||||||
|
final PlaceCategory selectedCategory;
|
||||||
|
final PlaceTrackerViewType viewType;
|
||||||
|
|
||||||
|
AppState copyWith({
|
||||||
|
List<Place> places,
|
||||||
|
PlaceCategory selectedCategory,
|
||||||
|
PlaceTrackerViewType viewType,
|
||||||
|
}) {
|
||||||
|
return AppState(
|
||||||
|
places: places ?? this.places,
|
||||||
|
selectedCategory: selectedCategory ?? this.selectedCategory,
|
||||||
|
viewType: viewType ?? this.viewType,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
static AppState of(BuildContext context) => AppModel.of<AppState>(context);
|
||||||
|
|
||||||
|
static void update(BuildContext context, AppState newState) {
|
||||||
|
AppModel.update<AppState>(context, newState);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void updateWith(
|
||||||
|
BuildContext context,
|
||||||
|
{List<Place> places,
|
||||||
|
PlaceCategory selectedCategory,
|
||||||
|
PlaceTrackerViewType viewType,
|
||||||
|
}) {
|
||||||
|
update(
|
||||||
|
context,
|
||||||
|
AppState.of(context).copyWith(
|
||||||
|
places: places, selectedCategory: selectedCategory, viewType: viewType,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool operator ==(Object other) {
|
||||||
|
if (identical(this, other))
|
||||||
|
return true;
|
||||||
|
if (other.runtimeType != runtimeType)
|
||||||
|
return false;
|
||||||
|
final AppState otherAppState = other;
|
||||||
|
return otherAppState.places == places
|
||||||
|
&& otherAppState.selectedCategory == selectedCategory
|
||||||
|
&& otherAppState.viewType == viewType;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
int get hashCode => hashValues(places, selectedCategory, viewType);
|
||||||
|
}
|
Loading…
Reference in new issue