diff --git a/lib/components/search_input.dart b/lib/components/search_input.dart index 8ec0d5b2..364abd80 100644 --- a/lib/components/search_input.dart +++ b/lib/components/search_input.dart @@ -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 v); typedef bool MaterialSearchFilter(T v, String c); @@ -412,7 +416,7 @@ class History extends StatefulWidget { * AppBar 默认的实例,有状态 * */ class _History extends State { - + SearchHistoryList searchHistoryList = new SearchHistoryList(); @override void initState() { @@ -423,11 +427,68 @@ class _History extends State { void dispose() { super.dispose(); } + buildChips(BuildContext context) { + List list = []; + List 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 childList = buildChips(context); + if (childList.length == 0) { + return Center( + child: Text("当前历史面板为空"), + ); + } + return Column( + children: [ + 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 + ), + ) + ], ); } } \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index dfc33e73..b7ad0160 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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()); } diff --git a/lib/model/search_history.dart b/lib/model/search_history.dart new file mode 100644 index 00000000..43b2b3ed --- /dev/null +++ b/lib/model/search_history.dart @@ -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 _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 _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 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> 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(); + } +} diff --git a/lib/resources/shared_preferences_keys.dart b/lib/resources/shared_preferences_keys.dart index 92c53031..0c44f774 100644 --- a/lib/resources/shared_preferences_keys.dart +++ b/lib/resources/shared_preferences_keys.dart @@ -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'; } diff --git a/lib/utils/shared_preferences.dart b/lib/utils/shared_preferences.dart index 6d3fc27e..370cd694 100644 --- a/lib/utils/shared_preferences.dart +++ b/lib/utils/shared_preferences.dart @@ -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 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 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 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 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 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 putStringList(String key, List 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 remove(String key) { - if (_beforCheck()) return null; + if (_beforeCheck()) return null; return _spf.remove(key); } Future clear() { - if (_beforCheck()) return null; + if (_beforeCheck()) return null; return _spf.clear(); } } \ No newline at end of file diff --git a/lib/views/first_page/home.dart b/lib/views/first_page/home.dart index 03a974af..103de682 100644 --- a/lib/views/first_page/home.dart +++ b/lib/views/first_page/home.dart @@ -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 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 @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 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 targetRouter = item.routerName; } }); + searchHistoryList + .add(SearchHistory(name: targetName, targetRouter: targetRouter)); + print("searchHistoryList ${searchHistoryList.toString()}"); Application.router.navigateTo(context, "$targetRouter"); } @@ -90,13 +102,13 @@ class _MyHomePageState extends State return list .map((item) => new MaterialSearchResult( - value: item.name, - icon: WidgetName2Icon.icons[item.name] ?? null, - text: 'widget', - onTap: () { - onWidgetTap(item, context); - }, - )) + value: item.name, + icon: WidgetName2Icon.icons[item.name] ?? null, + text: 'widget', + onTap: () { + onWidgetTap(item, context); + }, + )) .toList(); } else { return null; @@ -133,14 +145,14 @@ class _MyHomePageState extends State ], ), child: TabBar( - controller: controller, - indicatorColor: Theme.of(context).primaryColor, //tab标签的下划线颜色 - // labelColor: const Color(0xFF000000), - indicatorWeight: 3.0, - labelColor: Theme.of(context).primaryColor, - unselectedLabelColor: const Color(0xFF8E8E8E), - tabs: myTabs - ), + controller: controller, + indicatorColor: Theme.of(context).primaryColor, + //tab标签的下划线颜色 + // labelColor: const Color(0xFF000000), + indicatorWeight: 3.0, + labelColor: Theme.of(context).primaryColor, + unselectedLabelColor: const Color(0xFF8E8E8E), + tabs: myTabs), ), ), ), diff --git a/lib/views/fourth_page/pages.dart b/lib/views/fourth_page/pages.dart index e9efb923..08b8c391 100644 --- a/lib/views/fourth_page/pages.dart +++ b/lib/views/fourth_page/pages.dart @@ -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,