import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:ysairplane/code/YSSearchResult.dart'; import 'package:ysairplane/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 = '';},), suffixMode: OverlayVisibilityMode.editing, controller: _searchStr, padding: EdgeInsets.all(0), decoration: BoxDecoration(), 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);},) ], ), ), 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,), ), ) ] ), ), ) ], ), ), physics: NeverScrollableScrollPhysics(), ), ); } }