search_input.dart 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. controller: controller,
  24. decoration: InputDecoration(
  25. enabledBorder: border,
  26. focusedBorder: border,
  27. fillColor: Colors.grey.shade400,
  28. filled: true,
  29. hintText: placeholder,
  30. hintStyle: const TextStyle(
  31. fontSize: 20,
  32. color: Colors.black54,
  33. ),
  34. contentPadding: EdgeInsets.symmetric(
  35. horizontal: radius * 1.2,
  36. vertical: (c.maxHeight - 24) / 2,
  37. ),
  38. // isDense: true,
  39. isCollapsed: false,
  40. prefixIcon: IconButton(
  41. padding: const EdgeInsets.symmetric(horizontal: 18),
  42. icon: const Icon(Icons.search, size: 36),
  43. onPressed: () {
  44. onSearch?.call(controller.text);
  45. },
  46. ),
  47. ),
  48. onSubmitted: (value) {
  49. onSearch?.call(value);
  50. },
  51. );
  52. },
  53. );
  54. }
  55. }