search_input.dart 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import 'package:flutter/material.dart';
  2. class VSearchInput extends StatelessWidget {
  3. /// 占位提示
  4. final String? placeholder;
  5. /// 搜索回调
  6. final ValueChanged<String>? onSearch;
  7. const VSearchInput({
  8. super.key,
  9. this.placeholder,
  10. this.onSearch,
  11. });
  12. @override
  13. Widget build(BuildContext context) {
  14. return LayoutBuilder(
  15. builder: (_, c) {
  16. final controller = TextEditingController();
  17. final radius = c.maxHeight / 2;
  18. final border = OutlineInputBorder(
  19. borderRadius: BorderRadius.all(Radius.circular(radius)),
  20. borderSide: BorderSide.none,
  21. );
  22. return TextField(
  23. style: const TextStyle(fontSize: 20),
  24. controller: controller,
  25. decoration: InputDecoration(
  26. enabledBorder: border,
  27. focusedBorder: border,
  28. fillColor: const Color.fromRGBO(238, 238, 238, 1),
  29. filled: true,
  30. hintText: placeholder,
  31. hintStyle: const TextStyle(
  32. fontSize: 20,
  33. color: Colors.black54,
  34. ),
  35. contentPadding: EdgeInsets.symmetric(
  36. horizontal: radius * 1.2,
  37. vertical: (c.maxHeight - 24) / 2,
  38. ),
  39. // isDense: true,
  40. isCollapsed: false,
  41. prefixIcon: IconButton(
  42. padding: const EdgeInsets.symmetric(horizontal: 18),
  43. icon: const Icon(Icons.search, size: 36),
  44. onPressed: () {
  45. onSearch?.call(controller.text);
  46. },
  47. ),
  48. ),
  49. onSubmitted: (value) {
  50. onSearch?.call(value);
  51. },
  52. );
  53. },
  54. );
  55. }
  56. }