Storage.dartjson
import 'package:shared_preferences/shared_preferences.dart'; class Storage{ static Future<void> setString(key,value) async{ SharedPreferences sp=await SharedPreferences.getInstance(); sp.setString(key, value); } static Future<String> getString(key) async{ SharedPreferences sp=await SharedPreferences.getInstance(); return sp.getString(key); } static Future<String> remove(key) async{ SharedPreferences sp=await SharedPreferences.getInstance(); sp.remove(key); } static Future<String> clear(key) async{ SharedPreferences sp=await SharedPreferences.getInstance(); sp.clear(); } }
SearchServices.dartapi
import 'dart:convert'; import 'Storage.dart'; class SearchServices { static setHistoryData(keywords) async { /** * 1.獲取本地存儲裏面的數據。(searchList) * 2.判斷本地存儲是否有數據 * 2.1若是有數據: * 1.讀取本地存儲的數據。 * 2.判斷本地存儲中有沒有當前數據; * 3.若是有不作操做 * 若是沒有當前數據,本地存儲的數據和當前數據拼接後從新寫入。 * 2.2若是沒有數據: * 直接把當前數據放在數組中寫入到本地存儲。 * * * */ try { List searchListData = json.decode(await Storage.getString('searchList')); // print(searchListData); var hasData = searchListData.any((v) { return v == keywords; }); if (!hasData) { searchListData.add(keywords); await Storage.setString('searchList', json.encode(searchListData)); } } catch (e) { List tempList = new List(); tempList.add(keywords); await Storage.setString('searchList', json.encode(tempList)); } } static getHistoryList() async { try { List searchListData = json.decode(await Storage.getString('searchList')); return searchListData; } catch (e) { return []; } } static removeHistoryList() async { await Storage.remove('searchList'); } }
Search.dart數組
import 'package:flutter/material.dart'; import 'package:flutter_jdshop/services/ScreenAdaper.dart'; import 'package:flutter_jdshop/services/SearchServices.dart'; class SearchPage extends StatefulWidget { SearchPage({Key key}) : super(key: key); _SearchPageState createState() => _SearchPageState(); } class _SearchPageState extends State<SearchPage> { var _keywords; List _historyListData = []; @override void initState() { super.initState(); this._historyListWidget(); } _getHistoryData() async { var _historyListData = await SearchServices.getHistoryList(); setState(() { this._historyListData=_historyListData; }); } Widget _historyListWidget() { if (_historyListData.length > 0) { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ Container( child: Text('歷史記錄', style: Theme.of(context).textTheme.title), ), Divider(), Column( children: this._historyListData.map((value) { return Column( children: <Widget>[ ListTile( title: Text('${value}'), ), Divider() ], ); }).toList(), ), SizedBox(height: 100), Row( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ InkWell( onTap: () { SearchServices.removeHistoryList(); this._getHistoryData(); }, child: Container( width: ScreenAdaper.width(400), height: ScreenAdaper.height(64), decoration: BoxDecoration( border: Border.all(color: Colors.black54, width: 1)), child: Row( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[Icon(Icons.delete), Text('清空歷史記錄')], ), ), ) ], ) ], ); } else { return Text(''); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Container( child: TextField( autofocus: true, decoration: InputDecoration( border: OutlineInputBorder( borderRadius: BorderRadius.circular(30), borderSide: BorderSide.none)), onChanged: (value) { this._keywords = value; }, ), height: ScreenAdaper.height(68), decoration: BoxDecoration( color: Color.fromRGBO(233, 233, 233, 0.8), borderRadius: BorderRadius.circular(30)), ), actions: <Widget>[ InkWell( child: Container( height: ScreenAdaper.height(68), width: ScreenAdaper.width(80), child: Row( children: <Widget>[Text('搜索')], ), ), onTap: () { SearchServices.setHistoryData(this._keywords); Navigator.pushReplacementNamed(context, '/productList', arguments: {"keywords": this._keywords}); }, ) ], ), body: Container( padding: EdgeInsets.all(10), child: ListView( children: <Widget>[ Container( child: Text('熱搜', style: Theme.of(context).textTheme.title), ), Divider(), Wrap( children: <Widget>[ Container( padding: EdgeInsets.all(10), margin: EdgeInsets.all(10), decoration: BoxDecoration( color: Color.fromRGBO(233, 233, 233, 0.9), borderRadius: BorderRadius.circular(10)), child: Text('女裝'), ), Container( padding: EdgeInsets.all(10), margin: EdgeInsets.all(10), decoration: BoxDecoration( color: Color.fromRGBO(233, 233, 233, 0.9), borderRadius: BorderRadius.circular(10)), child: Text('女裝'), ), Container( padding: EdgeInsets.all(10), margin: EdgeInsets.all(10), decoration: BoxDecoration( color: Color.fromRGBO(233, 233, 233, 0.9), borderRadius: BorderRadius.circular(10)), child: Text('女裝'), ), Container( padding: EdgeInsets.all(10), margin: EdgeInsets.all(10), decoration: BoxDecoration( color: Color.fromRGBO(233, 233, 233, 0.9), borderRadius: BorderRadius.circular(10)), child: Text('女裝'), ), Container( padding: EdgeInsets.all(10), margin: EdgeInsets.all(10), decoration: BoxDecoration( color: Color.fromRGBO(233, 233, 233, 0.9), borderRadius: BorderRadius.circular(10)), child: Text('女裝'), ) ], ), SizedBox(height: 10), //歷史記錄: _historyListWidget() ], ), )); } }
Home.dartapp
import 'package:flutter/material.dart'; import '../../services/SearchServices.dart'; //熱門推薦: import '../../model/ProductModel.dart'; import 'package:flutter_swiper/flutter_swiper.dart'; // import 'dart:convert'; import '../../services/ScreenAdaper.dart'; import '../../config/Config.dart'; import 'package:dio/dio.dart'; //輪播圖類模型: import '../../model/FocusModel.dart'; class HomePage extends StatefulWidget { HomePage({Key key}) : super(key: key); _HomePageState createState() => _HomePageState(); } class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin { //輪播圖: //flutter run -d all 連接多個設備的命令: List _focusData = []; List _hotProductList=[]; List _bestProductList=[]; @override // TODO: implement wantKeepAlive bool get wantKeepAlive => true; void initState() { super.initState(); _getFocusData(); _getHotProductData(); _getBestProductData(); SearchServices.setHistoryData('aaa'); } //獲取輪播圖數據: _getFocusData() async { var api = '${Config.domain}api/focus'; var result = await Dio().get(api); var focusList = FocusModel.fromJson(result.data); focusList.result.forEach((value) { print(value.title); print(value.pic); }); setState(() { this._focusData = focusList.result; }); } //獲取猜你喜歡的數據: _getHotProductData() async{ var api='${Config.domain}api/plist?is_hot=1'; var result=await Dio().get(api); var hotProductList=ProductModel.fromJson(result.data); setState(() { this._hotProductList= hotProductList.result; }); } //獲取熱門推薦的數據: _getBestProductData() async{ var api='${Config.domain}api/plist?is_best=1'; var result=await Dio().get(api); var bestProductList=ProductModel.fromJson(result.data); setState(() { this._bestProductList= bestProductList.result; }); } Widget _swiperWidget() { // List<Map> imgList = [ // {"url": "https://www.itying.com/images/flutter/slide01.jpg"}, // {"url": "https://www.itying.com/images/flutter/slide02.jpg"}, // {"url": "https://www.itying.com/images/flutter/slide03.jpg"} // ]; if (this._focusData.length > 0) { return Container( child: AspectRatio( aspectRatio: 2 / 1, child: Swiper( itemBuilder: (BuildContext context, int index) { String pic=this._focusData[index].pic; pic=Config.domain+pic.replaceAll('\\', '/'); return new Image.network( "${pic}", fit: BoxFit.fill, ); }, itemCount: this._focusData.length, pagination: new SwiperPagination(), control: new SwiperControl(), autoplay: true, ), ), ); } else { return Text('加載中~'); } } //標題: Widget _titleWidget(value) { return Container( height: ScreenAdaper.height(46), margin: EdgeInsets.only(left: ScreenAdaper.width(20)), padding: EdgeInsets.only(left: ScreenAdaper.width(20)), decoration: BoxDecoration( border: Border( left: BorderSide( color: Colors.red, width: ScreenAdaper.width(10)))), child: Text(value, style: TextStyle(color: Colors.black54)), ); } //熱門商品: Widget _hotProductListWidget() { if(this._hotProductList.length>0){ return Container( height: ScreenAdaper.height(240), padding: EdgeInsets.all(ScreenAdaper.width(20)), // width: double.infinity, //寬度自適應 child: ListView.builder( scrollDirection: Axis.horizontal, itemBuilder: (contxt, index) { String sPic=this._hotProductList[index].sPic; sPic=Config.domain+sPic.replaceAll('\\', '/'); return Column( children: <Widget>[ Container( height: ScreenAdaper.height(140), width: ScreenAdaper.width(140), margin: EdgeInsets.only(right: ScreenAdaper.width(21)), child: Image.network( "${sPic}", fit: BoxFit.cover), ), Container( padding: EdgeInsets.only(top: ScreenAdaper.height(10)), height: ScreenAdaper.height(44), child: Text( "¥${this._hotProductList[index].price}", style: TextStyle(color: Colors.red), ), ) ], ); }, itemCount: this._hotProductList.length, ), ); }else{ return Text('暫無熱門推薦數據'); } } Widget _recProductListWidget() { var itemWidth = (ScreenAdaper.getScreenWidth() - 30) / 2; return Container( padding: EdgeInsets.all(10), child: Wrap( runSpacing: 10, spacing: 10, children:this._bestProductList.map((value){ //圖片: var sPic=value.sPic; sPic=Config.domain+sPic.replaceAll('\\','/'); return Container( padding: EdgeInsets.all(ScreenAdaper.width(20)), width: itemWidth, decoration: BoxDecoration(border: Border.all(color: Colors.black12, width: 1)), child: Column( children: <Widget>[ Container( width: double.infinity, child: AspectRatio( aspectRatio: 1 / 1, child: Image.network( "${sPic}", fit: BoxFit.cover), ), ), Padding( padding: EdgeInsets.only(top: ScreenAdaper.height(10)), child: Text( "${value.title}", maxLines: 2, overflow: TextOverflow.ellipsis, style: TextStyle(color: Colors.black54), ), ), Padding( padding: EdgeInsets.only(top: ScreenAdaper.height(20)), child: Stack( children: <Widget>[ Align( alignment: Alignment.centerLeft, child: Text( "${value.price}", style: TextStyle(color: Colors.red, fontSize: 16), ), ), Align( alignment: Alignment.centerRight, child: Text( "¥${value.oldPrice}", style: TextStyle( color: Colors.black54, fontSize: 16, decoration: TextDecoration.lineThrough), ), ) ], ), ) ], ), ); }).toList(), ), ); } @override Widget build(BuildContext context) { ScreenAdaper.init(context); return ListView( children: <Widget>[ _swiperWidget(), SizedBox(height: ScreenAdaper.height(20)), _titleWidget("猜你喜歡"), _hotProductListWidget(), SizedBox(height: ScreenAdaper.height(20)), _titleWidget("熱門推薦"), _recProductListWidget() ], ); } }