persistent draggable favorites manager

This commit is contained in:
2020-04-09 22:40:58 -04:00
parent b929018c9b
commit a59b13fc6f
5 changed files with 131 additions and 5 deletions

View File

@@ -1,4 +1,5 @@
import 'package:capstone_hungry_hippos/screens/chat.dart';
import 'package:capstone_hungry_hippos/screens/favorites_reorder.dart';
import 'package:capstone_hungry_hippos/screens/schedule.dart';
import 'package:capstone_hungry_hippos/screens/standing.dart';
import 'package:flutter/material.dart';
@@ -6,10 +7,11 @@ import 'package:capstone_hungry_hippos/screens/home_widget.dart';
import 'package:capstone_hungry_hippos/screens/sport.dart';
class RouteGenerator {
static Route<dynamic> generateRoute(RouteSettings settings){
final args = settings.arguments; //This is how we pass arguments and can be used in case
static Route<dynamic> generateRoute(RouteSettings settings) {
final args = settings
.arguments; //This is how we pass arguments and can be used in case
switch (settings.name){
switch (settings.name) {
case '/':
return MaterialPageRoute(builder: (_) => Home());
case '/Sport':
@@ -21,13 +23,15 @@ class RouteGenerator {
return MaterialPageRoute(builder: (_) => Standing());
case '/Chat':
return MaterialPageRoute(builder: (_) => Chat());
case '/Favorites':
return MaterialPageRoute(builder: (_) => FavoritesManager());
default:
return _errorRoute();
}
}
static Route _errorRoute() {
return MaterialPageRoute(builder: (_){
return MaterialPageRoute(builder: (_) {
return Scaffold(
appBar: AppBar(
title: Text("Error Page"),

View File

@@ -0,0 +1,82 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class Favorites {
Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
static const List<String> DEFAULT_FAVORITES = [
'Football',
'Basketball',
'Baseball',
'Soccer',
'Tennis',
'Volleyball'
];
Future<List<String>> get_favorites() async {
final prefs = await _prefs;
return prefs.getStringList('favorites') ?? DEFAULT_FAVORITES;
}
Future<bool> set_favorites(final List<String> favorites) async {
final prefs = await _prefs;
return await prefs.setStringList('favorites', favorites);
}
}
class FavoritesManager extends StatefulWidget {
FavoritesManager({Key key}) : super(key: key);
@override
_FavoritesManagerState createState() => _FavoritesManagerState();
}
class _FavoritesManagerState extends State<FavoritesManager> {
Favorites _mgr = Favorites();
Future<List<String>> _order;
@override
void initState() {
_order = _mgr.get_favorites();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: false,
title: Text('Manage Favorites'),
backgroundColor: Colors.green,
),
body: FutureBuilder<List<String>>(
future: _order,
builder: (ctx, snap) {
if (snap.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
} else {
var order = snap.data.toList();
return ReorderableListView(
onReorder: (int oldIdx, int newIdx) {
if (newIdx > oldIdx) newIdx--;
order.insert(newIdx, order.removeAt(oldIdx));
setState(() {
_mgr.set_favorites(order);
_order = Future.value(order);
});
},
children: order.map((sport) {
return ListTile(
key: Key(sport),
title: Text(sport),
trailing: Icon(Icons.drag_handle),
);
}).toList(),
);
}
},
),
);
}
}

View File

@@ -32,6 +32,12 @@ class Home extends StatelessWidget {
onPressed: () => Navigator.pushNamed(context, '/Chat'),
),
),
ListTile(
title: IconButton(
icon: Icon(Icons.settings),
onPressed: ()=>Navigator.pushNamed(context, '/Favorites'),
),
)
],
),
),

View File

@@ -88,6 +88,11 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
http:
dependency: "direct main"
description:
@@ -172,6 +177,34 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.23.1"
shared_preferences:
dependency: "direct main"
description:
name: shared_preferences
url: "https://pub.dartlang.org"
source: hosted
version: "0.5.6+3"
shared_preferences_macos:
dependency: transitive
description:
name: shared_preferences_macos
url: "https://pub.dartlang.org"
source: hosted
version: "0.0.1+6"
shared_preferences_platform_interface:
dependency: transitive
description:
name: shared_preferences_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.3"
shared_preferences_web:
dependency: transitive
description:
name: shared_preferences_web
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.2+4"
sky_engine:
dependency: transitive
description: flutter
@@ -242,4 +275,4 @@ packages:
version: "3.5.0"
sdks:
dart: ">=2.6.0 <3.0.0"
flutter: ">=1.12.1"
flutter: ">=1.12.13+hotfix.4 <2.0.0"

View File

@@ -22,6 +22,7 @@ dependencies:
flutter_bloc: ^3.2.0
http: ^0.12.0+4
shared_preferences: ^0.5.6+3
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.