YSSearch.dart 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:shared_preferences/shared_preferences.dart';
  4. import 'package:ysairplane/code/YSSearchResult.dart';
  5. import 'package:ysairplane/tools/YSTools.dart';
  6. class YSSearch extends StatefulWidget {
  7. @override
  8. _YSSearchState createState() => _YSSearchState();
  9. }
  10. class _YSSearchState extends State<YSSearch> {
  11. TextEditingController _searchStr = TextEditingController();
  12. List<String> _historyList = [];
  13. @override
  14. void initState() {
  15. SharedPreferences.getInstance().then((value){
  16. setState(() {
  17. _historyList = value.getStringList('history')??[];
  18. });
  19. });
  20. super.initState();
  21. }
  22. @override
  23. Widget build(BuildContext context) {
  24. return Scaffold(
  25. backgroundColor: Colors.white,
  26. body: SingleChildScrollView(
  27. child: Container(
  28. width: MediaQuery.of(context).size.width,
  29. child: Column(
  30. crossAxisAlignment: CrossAxisAlignment.start,
  31. children: [
  32. Container(
  33. margin: EdgeInsets.only(top: MediaQuery.of(context).padding.top+hsp(42),left: wsp(32),right: wsp(22)),
  34. child: Row(
  35. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  36. children: [
  37. Container(
  38. width: MediaQuery.of(context).size.width-wsp(130),
  39. padding: EdgeInsets.only(left: wsp(26),right: wsp(26)),
  40. height: hsp(66),
  41. decoration: BoxDecoration(
  42. border: Border.all(color: Color(0xFF007AFF),width: 1),
  43. borderRadius: BorderRadius.all(Radius.circular(5))
  44. ),
  45. child: CupertinoTextField(
  46. placeholder: '请输入搜索内容',
  47. placeholderStyle: TextStyle(fontSize: zsp(25),color: Color(0xFF545454)),
  48. style: TextStyle(fontSize: zsp(25),color: Color(0xFF545454)),
  49. prefix: Icon(Icons.search,size: hsp(40),color: Color(0xFF8E8E93),),
  50. suffix: GestureDetector(child: Icon(Icons.close,size: hsp(40),color: Color(0xFF8E8E93),),onTap: (){_searchStr.text = '';},),
  51. suffixMode: OverlayVisibilityMode.editing,
  52. controller: _searchStr,
  53. padding: EdgeInsets.all(0),
  54. decoration: BoxDecoration(),
  55. onSubmitted: (value){
  56. if(value.isNotEmpty){
  57. if(_historyList.contains(value)){
  58. _historyList.remove(value);
  59. }
  60. _historyList.insert(0, value);
  61. SharedPreferences.getInstance().then((prefer){
  62. prefer.setStringList('history', _historyList);
  63. setState(() {});
  64. Navigator.of(context).push(
  65. CupertinoPageRoute(
  66. builder: (context){
  67. return YSSearchResult(searchStr: value,);
  68. }
  69. )
  70. );
  71. });
  72. }
  73. },
  74. textInputAction: TextInputAction.search,
  75. ),
  76. ),
  77. GestureDetector(child: Text('取消',style: TextStyle(fontSize: zsp(30),color: Color(0xFF444444)),),onTap: (){Navigator.pop(context);},)
  78. ],
  79. ),
  80. ),
  81. Container(
  82. child: Row(
  83. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  84. children: [
  85. Text('定位/历史',style: TextStyle(fontSize: zsp(24),color: Color(0xFF9A9A9A)),),
  86. GestureDetector(
  87. child: Icon(Icons.delete,color: Color(0xFFE0E0E0),size: hsp(40),),
  88. onTap: (){
  89. SharedPreferences.getInstance().then((prefer){
  90. _historyList = [];
  91. prefer.setStringList('history', _historyList);
  92. setState(() {
  93. });
  94. });
  95. },
  96. )
  97. ],
  98. ),
  99. padding: EdgeInsets.only(left: wsp(32),right: wsp(62),top: hsp(34),bottom: hsp(10)),
  100. ),
  101. Container(
  102. height: MediaQuery.of(context).size.height-MediaQuery.of(context).padding.top-hsp(200),
  103. child: SingleChildScrollView(
  104. padding: EdgeInsets.only(left: wsp(16)),
  105. child: Wrap(
  106. alignment: WrapAlignment.start,
  107. children: <Widget>[
  108. for (int i =0;i<_historyList.length;i++ ) GestureDetector(
  109. behavior: HitTestBehavior.opaque,
  110. onTap: (){
  111. String value = _historyList[i];
  112. _historyList.remove(_historyList[i]);
  113. _historyList.insert(0, value);
  114. SharedPreferences.getInstance().then((prefer){
  115. prefer.setStringList('history', _historyList);
  116. setState(() {});
  117. Navigator.of(context).push(
  118. CupertinoPageRoute(
  119. builder: (context){
  120. return YSSearchResult(searchStr: value,);
  121. }
  122. )
  123. );
  124. });
  125. },
  126. child: Container(
  127. margin: EdgeInsets.symmetric(vertical: hsp(16), horizontal: hsp(16)),
  128. decoration: BoxDecoration(
  129. color: Color(0xFFE0E0E0),
  130. borderRadius: BorderRadius.all(Radius.circular(3))
  131. ),
  132. padding: EdgeInsets.only(left: wsp(40), right: wsp(40),top: hsp(20),bottom: hsp(20)),
  133. child: Text('${_historyList[i]}', style: TextStyle(fontSize: zsp(26),color: Color(0xFF16181A)), textAlign: TextAlign.center,),
  134. ),
  135. )
  136. ]
  137. ),
  138. ),
  139. )
  140. ],
  141. ),
  142. ),
  143. physics: NeverScrollableScrollPhysics(),
  144. ),
  145. );
  146. }
  147. }