124 lines
3.9 KiB
Dart
124 lines
3.9 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
class SearchBarWidget extends StatelessWidget {
|
|
final String hintText;
|
|
final String buttonText;
|
|
final ValueChanged<String> onSearch;
|
|
final TextEditingController controller; // 改为必传参数
|
|
final bool autoFocus;
|
|
final bool showResetButton;
|
|
final String resetButtonText;
|
|
final VoidCallback? onReset;
|
|
final bool isClickableOnly;
|
|
final VoidCallback? onInputTap;
|
|
final ValueChanged<String>? onTextChanged; // 新增文本变化回调
|
|
|
|
const SearchBarWidget({
|
|
Key? key,
|
|
required this.onSearch,
|
|
required this.controller, // 必须传入controller
|
|
this.hintText = '请输入关键字',
|
|
this.buttonText = '搜索',
|
|
this.autoFocus = false,
|
|
this.showResetButton = false,
|
|
this.resetButtonText = '重置',
|
|
this.onReset,
|
|
this.isClickableOnly = false,
|
|
this.onInputTap,
|
|
this.onTextChanged, // 可选文本变化监听
|
|
}) : super(key: key);
|
|
|
|
// 公共方法:更新输入框内容(可在外部调用)
|
|
void updateText(String newText) {
|
|
controller.text = newText;
|
|
controller.selection = TextSelection.fromPosition(
|
|
TextPosition(offset: newText.length),
|
|
);
|
|
// 可选:触发文本变化回调
|
|
onTextChanged?.call(newText);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
|
|
return Row(
|
|
children: [
|
|
Expanded(
|
|
child: GestureDetector(
|
|
onTap: isClickableOnly ? onInputTap : null,
|
|
child: AbsorbPointer(
|
|
absorbing: isClickableOnly,
|
|
child: TextField(
|
|
controller: controller,
|
|
autofocus: autoFocus,
|
|
readOnly: isClickableOnly,
|
|
style: const TextStyle(fontSize: 15),
|
|
onChanged: onTextChanged, // 监听文本变化
|
|
decoration: InputDecoration(
|
|
filled: true,
|
|
fillColor: const Color(0xFFF5F5F5),
|
|
prefixIcon: const Icon(Icons.search_rounded),
|
|
hintText: hintText,
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(5),
|
|
borderSide: BorderSide.none,
|
|
),
|
|
isDense: true,
|
|
contentPadding: const EdgeInsets.symmetric(vertical: 8),
|
|
|
|
),
|
|
onSubmitted: onSearch,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 10),
|
|
// 搜索按钮
|
|
ElevatedButton(
|
|
onPressed: () => onSearch(controller.text.trim()),
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.green,
|
|
padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 8),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(5),
|
|
),
|
|
elevation: 4,
|
|
shadowColor: Colors.black45,
|
|
),
|
|
child: Text(
|
|
buttonText,
|
|
style: const TextStyle(
|
|
color: Colors.white,
|
|
fontSize: 16,
|
|
),
|
|
),
|
|
),
|
|
// 重置按钮
|
|
if (showResetButton) const SizedBox(width: 10),
|
|
if (showResetButton)
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
updateText(''); // 使用统一方法清空
|
|
onReset?.call();
|
|
},
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.blue,
|
|
padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 8),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(5),
|
|
),
|
|
elevation: 1,
|
|
shadowColor: Colors.black26,
|
|
),
|
|
child: Text(
|
|
resetButtonText,
|
|
style: TextStyle(
|
|
color: Colors.white,
|
|
fontSize: 16,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
} |