feat(增加历史记录列表):

This commit is contained in:
sanfan.hx
2019-02-18 17:06:31 +08:00
parent d9221a55ff
commit 619c1bd04e
7 changed files with 239 additions and 55 deletions

View File

@ -1,6 +1,10 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:fluro/fluro.dart';
import 'package:meta/meta.dart';
import 'package:flutter_go/resources/widget_name_to_icon.dart';
import 'package:flutter_go/routers/application.dart';
import '../model/search_history.dart';
typedef String FormFieldFormatter<T>(T v);
typedef bool MaterialSearchFilter<T>(T v, String c);
@ -412,7 +416,7 @@ class History extends StatefulWidget {
* AppBar 默认的实例,有状态
* */
class _History extends State<History> {
SearchHistoryList searchHistoryList = new SearchHistoryList();
@override
void initState() {
@ -423,11 +427,68 @@ class _History extends State<History> {
void dispose() {
super.dispose();
}
buildChips(BuildContext context) {
List<Widget> list = [];
List<SearchHistory> historyList = searchHistoryList.getList();
print("historyList> $historyList");
Color bgColor = Theme.of(context).primaryColor;
historyList.forEach((SearchHistory value) {
Widget icon = CircleAvatar(
backgroundColor: bgColor,
child: Text(
value.name.substring(0, 1),
style: TextStyle(color: Colors.white),
),
);
if (WidgetName2Icon.icons[value.name] != null) {
icon = Icon(WidgetName2Icon.icons[value.name], size: 25);
}
list.add(
InkWell(
onTap: () {
Application.router.navigateTo(context, "${value.targetRouter}", transition: TransitionType.inFromRight);
},
child: Chip(
avatar: icon,
label: Text("${value.name}"),
),
)
);
});
return list;
}
@override
Widget build(BuildContext context) {
return new Center(
child: Text('这是一个即将完善的历史记录的面板'),
List<Widget> childList = buildChips(context);
if (childList.length == 0) {
return Center(
child: Text("当前历史面板为空"),
);
}
return Column(
children: <Widget>[
Container(
alignment: Alignment.centerLeft,
padding: EdgeInsets.fromLTRB(12.0, 12, 12, 0),
child: InkWell(
onLongPress: () {
searchHistoryList.clear();
},
child: Text('历史搜索'),
),
),
Container(
padding: EdgeInsets.only(left: 10),
alignment: Alignment.topLeft,
child: Wrap(
spacing: 6.0, // gap between adjacent chips
runSpacing: 0.0, // gap between lines
children: childList
),
)
],
);
}
}

View File

@ -6,6 +6,7 @@ import 'routers/application.dart';
import 'package:flutter_go/utils/provider.dart';
import 'package:flutter_go/utils/shared_preferences.dart';
import 'package:flutter_go/views/first_page/home.dart';
import 'package:flutter_go/model/search_history.dart';
import 'views/welcome_page/index.dart';
const int ThemeColor = 0xFFC91B3A;
@ -19,7 +20,7 @@ class MyApp extends StatelessWidget {
Application.router = router;
}
showWelcomePage() {
bool showWelcome = sp.getBool(sharedPreferencesKeys.showWelcome);
bool showWelcome = sp.getBool(SharedPreferencesKeys.showWelcome);
if (showWelcome == null || showWelcome == true) {
return WelcomePage();
} else {
@ -57,6 +58,7 @@ void main() async {
final provider = new Provider();
await provider.init(true);
sp = await SpUtil.getInstance();
new SearchHistoryList(sp);
db = Provider.db;
runApp(new MyApp());
}

View File

@ -0,0 +1,102 @@
//
// Created with Android Studio.
// User: 三帆
// Date: 18/02/2019
// Time: 14:19
// email: sanfan.hx@alibaba-inc.com
// tartget: xxx
//
import 'package:flutter/material.dart';
import 'package:flutter_go/utils/shared_preferences.dart';
import 'dart:convert';
class SearchHistory {
final String name;
final String targetRouter;
SearchHistory({@required this.name, @required this.targetRouter});
}
class SearchHistoryList {
static SpUtil _sp;
static SearchHistoryList _instance;
static List<SearchHistory> _searchHistoryList = [];
static SearchHistoryList _getInstance(SpUtil sp) {
// print("SearchHistoryList _getInstance ${_searchHistoryList} ${_instance==null}");
if (_instance == null) {
_sp = sp;
String json = sp.get(SharedPreferencesKeys.searchHistory);
_instance = new SearchHistoryList.fromJSON(json);
}
return _instance;
}
factory SearchHistoryList([SpUtil sp]) {
if (sp == null && _instance == null) {
print(new ArgumentError(
['SearchHistoryList need instantiatied SpUtil at first timte ']));
}
return _getInstance(sp);
}
// List<SearchHistory> _searchHistoryList = [];
// 存放的最大数量
int _count = 10;
SearchHistoryList.fromJSON(String jsonData) {
_searchHistoryList = [];
if (jsonData == null) {
return;
}
List jsonList = json.decode(jsonData);
jsonList.forEach((value) {
_searchHistoryList.add(SearchHistory(
name: value['name'], targetRouter: value['targetRouter']));
});
}
List<SearchHistory> getList() {
return _searchHistoryList;
}
clear() {
_sp.remove(SharedPreferencesKeys.searchHistory);
_searchHistoryList = [];
}
save() {
_sp.putString(SharedPreferencesKeys.searchHistory, this.toJson());
}
add(SearchHistory item) {
print("_searchHistoryList> ${_searchHistoryList.length}");
for (SearchHistory value in _searchHistoryList) {
if (value.name == item.name) {
return ;
}
}
if (_searchHistoryList.length > _count) {
_searchHistoryList.removeAt(0);
}
_searchHistoryList.add(item);
save();
}
toJson() {
List<Map<String, String>> jsonList = [];
_searchHistoryList.forEach((SearchHistory value) {
jsonList.add({'name': value.name, 'targetRouter': value.targetRouter});
});
return json.encode(jsonList);
}
@override
String toString() {
// TODO: implement toString
return this.toJson();
}
}

View File

@ -1,11 +1,11 @@
/**
* Created with Android Studio.
* User: 三帆
* Date: 31/01/2019
* Time: 18:13
* email: sanfan.hx@alibaba-inc.com
* tartget: xxx
*/
//
// Created with Android Studio.
// User: 三帆
// Date: 31/01/2019
// Time: 18:13
// email: sanfan.hx@alibaba-inc.com
// tartget: xxx
//
//enum DateType {
// Int,
@ -22,9 +22,16 @@
// spKey({this.name, this.type});
//}
class sharedPreferencesKeys {
class SharedPreferencesKeys {
/// boolean
/// 用于欢迎页面. 只有第一次访问才会显示. 或者手动将这个值设为false
static String showWelcome = 'loginWelcone';
/// json
/// 用于存放搜索页的搜索数据.
/// [{
/// name: 'name'
///
/// }]
static String searchHistory = 'searchHistory';
}

View File

@ -28,7 +28,7 @@ class SpUtil {
return _instance;
}
static bool _beforCheck() {
static bool _beforeCheck() {
if (_spf == null) {
return true;
}
@ -41,52 +41,52 @@ class SpUtil {
}
Set<String> getKeys() {
if (_beforCheck()) return null;
if (_beforeCheck()) return null;
return _spf.getKeys();
}
get(String key) {
if (_beforCheck()) return null;
if (_beforeCheck()) return null;
return _spf.get(key);
}
getString(String key) {
if (_beforCheck()) return null;
if (_beforeCheck()) return null;
return _spf.getString(key);
}
Future<bool> putString(String key, String value) {
if (_beforCheck()) return null;
if (_beforeCheck()) return null;
return _spf.setString(key, value);
}
bool getBool(String key) {
if (_beforCheck()) return null;
if (_beforeCheck()) return null;
return _spf.getBool(key);
}
Future<bool> putBool(String key, bool value) {
if (_beforCheck()) return null;
if (_beforeCheck()) return null;
return _spf.setBool(key, value);
}
int getInt(String key) {
if (_beforCheck()) return null;
if (_beforeCheck()) return null;
return _spf.getInt(key);
}
Future<bool> putInt(String key, int value) {
if (_beforCheck()) return null;
if (_beforeCheck()) return null;
return _spf.setInt(key, value);
}
double getDouble(String key) {
if (_beforCheck()) return null;
if (_beforeCheck()) return null;
return _spf.getDouble(key);
}
Future<bool> putDouble(String key, double value) {
if (_beforCheck()) return null;
if (_beforeCheck()) return null;
return _spf.setDouble(key, value);
}
@ -95,24 +95,24 @@ class SpUtil {
}
Future<bool> putStringList(String key, List<String> value) {
if (_beforCheck()) return null;
if (_beforeCheck()) return null;
return _spf.setStringList(key, value);
}
dynamic getDynamic(String key) {
if (_beforCheck()) return null;
if (_beforeCheck()) return null;
return _spf.get(key);
}
Future<bool> remove(String key) {
if (_beforCheck()) return null;
if (_beforeCheck()) return null;
return _spf.remove(key);
}
Future<bool> clear() {
if (_beforCheck()) return null;
if (_beforeCheck()) return null;
return _spf.clear();
}
}

View File

@ -1,17 +1,14 @@
/**
* Created with Android Studio.
* User: 三帆
* Date: 16/01/2019
* Time: 11:16
* email: sanfan.hx@alibaba-inc.com
* tartget: app首页
*/
// Created with Android Studio.
// User: 三帆
// Date: 16/01/2019
// Time: 11:16
// email: sanfan.hx@alibaba-inc.com
// target: app首页
//
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_go/utils/shared_preferences.dart';
import 'package:flutter_go/views/first_page/first_page.dart';
import 'package:flutter_go/views/widget_page/widget_page.dart';
import 'package:flutter_go/views/welcome_page/fourth_page.dart';
@ -21,6 +18,7 @@ import 'package:flutter_go/utils/provider.dart';
import 'package:flutter_go/model/widget.dart';
import 'package:flutter_go/widgets/index.dart';
import 'package:flutter_go/components/search_input.dart';
import 'package:flutter_go/model/search_history.dart';
import 'package:flutter_go/resources/widget_name_to_icon.dart';
const int ThemeColor = 0xFFC91B3A;
@ -34,7 +32,9 @@ class AppPage extends StatefulWidget {
class _MyHomePageState extends State<AppPage>
with SingleTickerProviderStateMixin {
SpUtil sp;
WidgetControlModel widgetControl = new WidgetControlModel();
SearchHistoryList searchHistoryList;
TabController controller;
bool isSearch = false;
String data = '';
@ -52,6 +52,8 @@ class _MyHomePageState extends State<AppPage>
@override
void initState() {
super.initState();
initSearchHistory();
controller = new TabController(
initialIndex: 0, vsync: this, length: 4); // 这里的length 决定有多少个底导 submenus
for (int i = 0; i < tabData.length; i++) {
@ -71,6 +73,13 @@ class _MyHomePageState extends State<AppPage>
super.dispose();
}
initSearchHistory() async {
sp = await SpUtil.getInstance();
String json = sp.getString(SharedPreferencesKeys.searchHistory);
print("json $json");
searchHistoryList = SearchHistoryList.fromJSON(json);
}
void onWidgetTap(WidgetPoint widgetPoint, BuildContext context) {
List widgetDemosList = new WidgetDemoList().getDemos();
String targetName = widgetPoint.name;
@ -80,6 +89,9 @@ class _MyHomePageState extends State<AppPage>
targetRouter = item.routerName;
}
});
searchHistoryList
.add(SearchHistory(name: targetName, targetRouter: targetRouter));
print("searchHistoryList ${searchHistoryList.toString()}");
Application.router.navigateTo(context, "$targetRouter");
}
@ -134,13 +146,13 @@ class _MyHomePageState extends State<AppPage>
),
child: TabBar(
controller: controller,
indicatorColor: Theme.of(context).primaryColor, //tab标签的下划线颜色
indicatorColor: Theme.of(context).primaryColor,
//tab标签的下划线颜色
// labelColor: const Color(0xFF000000),
indicatorWeight: 3.0,
labelColor: Theme.of(context).primaryColor,
unselectedLabelColor: const Color(0xFF8E8E8E),
tabs: myTabs
),
tabs: myTabs),
),
),
),

View File

@ -47,7 +47,7 @@ class Page extends StatelessWidget {
onPressed: () async {
if (type == 'start') {
await SpUtil.getInstance()
..putBool(sharedPreferencesKeys.showWelcome, false);
..putBool(SharedPreferencesKeys.showWelcome, false);
_goHomePage(context);
} else if (type == 'goGithub') {
Application.router.navigateTo(context,