mobile_skt/lib/widgets/form_item/sk_search_select.dart

107 lines
4.0 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';
import 'package:sk_base_mobile/app_theme.dart';
import 'package:sk_base_mobile/util/screen_adaper_util.dart';
2024-04-01 08:41:52 +08:00
class SkSearchSelect<T> extends StatelessWidget {
final TextEditingController textController;
final Function? suggestionsCallback;
2024-03-25 14:16:00 +08:00
final void Function(T)? onSelected;
final VoidCallback? onClear;
final String? labelText;
2024-03-26 11:35:39 +08:00
final String hintText;
final EdgeInsetsGeometry? contentPadding;
final bool isRequired;
2024-03-25 14:16:00 +08:00
final Widget Function(BuildContext, T) itemBuilder;
2024-04-01 08:41:52 +08:00
const SkSearchSelect(
{super.key,
required this.textController,
required this.itemBuilder,
required this.suggestionsCallback,
2024-03-26 11:35:39 +08:00
this.hintText = '请选择',
this.onSelected,
this.labelText,
this.onClear,
this.contentPadding,
this.isRequired = false});
@override
Widget build(BuildContext context) {
return TypeAheadField(
hideOnUnfocus: true,
controller: textController,
suggestionsCallback: (String keyword) {
return suggestionsCallback != null
? suggestionsCallback!(keyword)
: [];
},
debounceDuration: const Duration(milliseconds: 500),
itemSeparatorBuilder: (context, index) => const Divider(
color: AppTheme.dividerColor,
),
builder: (context, _, focusNode) {
return TextFormField(
focusNode: focusNode,
controller: _,
decoration: InputDecoration(
contentPadding: contentPadding,
focusedBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: AppTheme.primaryColorLight, width: 2),
borderRadius: BorderRadius.circular(ScreenAdaper.sp(15))),
suffixIcon: _.text.isNotEmpty && focusNode.hasFocus
? IconButton(
icon: const Icon(Icons.clear),
// 当点击这个按钮时清除TextFormField的值
onPressed: () {
_.clear();
if (onClear != null) {
onClear!();
}
},
)
: null,
floatingLabelBehavior: FloatingLabelBehavior.always,
2024-03-26 11:35:39 +08:00
hintText: hintText,
label: labelText != null
? Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
if (isRequired)
Text(
"*",
style: TextStyle(
color: Colors.red,
fontSize: ScreenAdaper.height(30)),
),
Text(
labelText ?? '',
style: TextStyle(
fontSize: ScreenAdaper.height(30)),
),
])
2024-03-31 17:13:29 +08:00
: const SizedBox()));
},
emptyBuilder: (_) => Container(
alignment: Alignment.center,
width: ScreenAdaper.width(200),
height: ScreenAdaper.height(50),
child: Text(
'未找到记录',
2024-03-28 17:18:46 +08:00
style: TextStyle(fontSize: ScreenAdaper.height(20)),
),
),
itemBuilder: (context, itemData) {
2024-03-25 14:16:00 +08:00
return itemBuilder(context, itemData as T);
},
onSelected: (selectedItemData) {
// 键盘关闭
FocusScope.of(context).unfocus();
if (onSelected != null) {
2024-03-25 14:16:00 +08:00
onSelected!(selectedItemData as T);
}
});
}
}