import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:ysairplane2/code/YSSearchResult.dart'; import 'package:ysairplane2/tools/YSNetWorking.dart'; import 'package:ysairplane2/tools/YSTools.dart'; class YSSearch extends StatefulWidget { @override _YSSearchState createState() => _YSSearchState(); } class _YSSearchState extends State { TextEditingController _searchStr = TextEditingController(); List _historyList = []; @override void initState() { SharedPreferences.getInstance().then((value){ setState(() { _historyList = value.getStringList('history')??[]; }); }); super.initState(); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, body: SingleChildScrollView( child: Container( width: MediaQuery.of(context).size.width, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( margin: EdgeInsets.only(top: MediaQuery.of(context).padding.top+hsp(42),left: wsp(32),right: wsp(22)), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Container( width: MediaQuery.of(context).size.width-wsp(130), padding: EdgeInsets.only(left: wsp(26),right: wsp(26)), height: hsp(66), decoration: BoxDecoration( border: Border.all(color: Color(0xFF007AFF),width: 1), borderRadius: BorderRadius.all(Radius.circular(5)) ), child: CupertinoTextField( placeholder: '请输入搜索内容', placeholderStyle: TextStyle(fontSize: zsp(25),color: Color(0xFF545454)), style: TextStyle(fontSize: zsp(25),color: Color(0xFF545454)), prefix: Icon(Icons.search,size: hsp(40),color: Color(0xFF8E8E93),), suffix: GestureDetector(child: Icon(Icons.close,size: hsp(40),color: Color(0xFF8E8E93),),onTap: (){ _searchStr.text = ''; searchKey.currentState.getSearchWordData(''); },), suffixMode: OverlayVisibilityMode.editing, controller: _searchStr, padding: EdgeInsets.all(0), decoration: BoxDecoration(), onChanged: (value) async{ searchKey.currentState.getSearchWordData(value); }, onSubmitted: (value){ if(value.isNotEmpty){ if(_historyList.contains(value)){ _historyList.remove(value); } _historyList.insert(0, value); SharedPreferences.getInstance().then((prefer){ prefer.setStringList('history', _historyList); setState(() {}); Navigator.of(context).push( CupertinoPageRoute( builder: (context){ return YSSearchResult(searchStr: value,); } ) ); }); } }, textInputAction: TextInputAction.search, ), ), GestureDetector(child: Text('取消',style: TextStyle(fontSize: zsp(30),color: Color(0xFF444444)),),onTap: (){Navigator.pop(context);},) ], ), ), Stack( children: [ Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text('定位/历史',style: TextStyle(fontSize: zsp(24),color: Color(0xFF9A9A9A)),), GestureDetector( child: Icon(Icons.delete,color: Color(0xFFE0E0E0),size: hsp(40),), onTap: (){ SharedPreferences.getInstance().then((prefer){ _historyList = []; prefer.setStringList('history', _historyList); setState(() { }); }); }, ) ], ), padding: EdgeInsets.only(left: wsp(32),right: wsp(62),top: hsp(34),bottom: hsp(10)), ), Container( height: MediaQuery.of(context).size.height-MediaQuery.of(context).padding.top-hsp(200), child: SingleChildScrollView( padding: EdgeInsets.only(left: wsp(16)), child: Wrap( alignment: WrapAlignment.start, children: [ for (int i =0;i<_historyList.length;i++ ) GestureDetector( behavior: HitTestBehavior.opaque, onTap: (){ String value = _historyList[i]; _historyList.remove(_historyList[i]); _historyList.insert(0, value); SharedPreferences.getInstance().then((prefer){ prefer.setStringList('history', _historyList); setState(() {}); Navigator.of(context).push( CupertinoPageRoute( builder: (context){ return YSSearchResult(searchStr: value,); } ) ); }); }, child: Container( margin: EdgeInsets.symmetric(vertical: hsp(16), horizontal: hsp(16)), decoration: BoxDecoration( color: Color(0xFFE0E0E0), borderRadius: BorderRadius.all(Radius.circular(3)) ), padding: EdgeInsets.only(left: wsp(40), right: wsp(40),top: hsp(20),bottom: hsp(20)), child: Text('${_historyList[i]}', style: TextStyle(fontSize: zsp(26),color: Color(0xFF16181A)), textAlign: TextAlign.center,), ), ) ] ), ), ) ], ), YSSearchWordView(key: searchKey, historyList: _historyList,) ], ) ], ), ), physics: NeverScrollableScrollPhysics(), ), ); } } class YSSearchWordView extends StatefulWidget { final List historyList; final ValueSetter wordSetter; const YSSearchWordView({Key key, @required this.historyList, this.wordSetter}) : super(key: key); @override _YSSearchWordViewState createState() => _YSSearchWordViewState(); } GlobalKey<_YSSearchWordViewState> searchKey = GlobalKey(); GlobalKey<_YSSearchWordViewState> searchKey2 = GlobalKey(); class _YSSearchWordViewState extends State { List _dataArray = []; @override Widget build(BuildContext context) { return Container( child: ListView.separated(itemBuilder: (context,index){ return GestureDetector( onTap: (){ String value = _dataArray[index]; if(widget.wordSetter!=null){ widget.wordSetter(value); }else{ if(widget.historyList.contains(value)){ widget.historyList.remove(value); } widget.historyList.insert(0, value); SharedPreferences.getInstance().then((prefer){ prefer.setStringList('history', widget.historyList); setState(() {}); Navigator.of(context).push( CupertinoPageRoute( builder: (context){ return YSSearchResult(searchStr: value,); } ) ); }); } }, child: Container( width: MediaQuery.of(context).size.width, padding: EdgeInsets.all(hsp(30)), color: Colors.white, child: Text(_dataArray[index]), ), ); },separatorBuilder: (context,index){ return Divider(height: hsp(1),thickness: hsp(1),color: Colors.grey.withOpacity(0.1),); },itemCount: _dataArray.length,shrinkWrap: true,padding: EdgeInsets.all(0),), ); } getSearchWordData(String word) async{ if(word.isEmpty){ _dataArray.clear(); }else{ Map dict = await ysRequestHttp(context,type: requestType.get, api: '/app/applets/AirTour/search_ideas', parameter: {'keyWords':word},isLoading: false,isToken: true,unfocus: false); if(dict!=null){ _dataArray = dict['data']; } } setState(() {}); } }