YSSearchView.dart 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import '../../tool/YSTools.dart';
  4. class YSSearchView extends StatefulWidget {
  5. final ValueSetter postSearch;
  6. const YSSearchView({Key? key, required this.postSearch}) : super(key: key);
  7. @override
  8. YSSearchViewState createState() => YSSearchViewState();
  9. }
  10. class YSSearchViewState extends State<YSSearchView> {
  11. final TextEditingController _editingController = TextEditingController();
  12. @override
  13. void dispose() {
  14. _editingController.dispose();
  15. super.dispose();
  16. }
  17. @override
  18. Widget build(BuildContext context) {
  19. return Container(
  20. padding: EdgeInsets.only(left: hsp(15),right: hsp(15)),
  21. height: hsp(42),
  22. width: ysWidth(context)-hsp(40),
  23. decoration: const BoxDecoration(
  24. color: Color(0xFF1A1C1F),
  25. borderRadius: BorderRadius.all(Radius.circular(3))
  26. ),
  27. child: CupertinoTextField(
  28. placeholder: '搜索内容',
  29. style: TextStyle(fontSize: zsp(14),color: Colors.white),
  30. placeholderStyle: TextStyle(fontSize: zsp(14),color: const Color(0xFFD5D5D5)),
  31. decoration: const BoxDecoration(color: Colors.transparent),
  32. prefix: Image.asset('images/tzh_search.png',height: hsp(20),width: hsp(20),),
  33. suffix: _editingController.text.isNotEmpty?GestureDetector(
  34. onTap: (){
  35. _editingController.text = '';
  36. FocusScope.of(context).unfocus();
  37. widget.postSearch('');
  38. },
  39. child: Image.asset('images/tzh_clear.png',height: hsp(20),width: hsp(20),),
  40. ):Container(),
  41. onChanged: (value){
  42. setState(() {});
  43. },
  44. controller:_editingController,
  45. textInputAction: TextInputAction.search,
  46. onSubmitted: (value){
  47. FocusScope.of(context).unfocus();
  48. widget.postSearch(value);
  49. },
  50. ),
  51. )
  52. ;
  53. }
  54. }