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'; class SearchSelectComponent extends StatelessWidget { final TextEditingController textController; final Function? suggestionsCallback; final Function? onSelected; final String labelText; final bool isRequired; final Widget Function(T itemData)? itemTextBuilder; const SearchSelectComponent( {super.key, required this.textController, required this.labelText, required this.itemTextBuilder, this.suggestionsCallback, this.onSelected, this.isRequired = false}); @override Widget build(BuildContext context) { return TypeAheadField( hideOnUnfocus: false, controller: textController, suggestionsCallback: (String keyword) { return suggestionsCallback != null ? suggestionsCallback!(keyword) : []; }, builder: (context, _, focusNode) { return TextFormField( focusNode: focusNode, controller: _, decoration: InputDecoration( 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(), ) : const SizedBox(), floatingLabelBehavior: FloatingLabelBehavior.always, label: Row( crossAxisAlignment: CrossAxisAlignment.center, mainAxisSize: MainAxisSize.min, children: [ if (isRequired) const Text( "*", style: TextStyle(color: Colors.red), ), Text( labelText, ), ]))); }, emptyBuilder: (_) => Container( alignment: Alignment.center, width: ScreenAdaper.width(200), height: ScreenAdaper.height(50), child: Text( '未找到记录', style: TextStyle(fontSize: ScreenAdaper.sp(20)), ), ), itemBuilder: (context, itemData) { return ListTile( title: itemTextBuilder!(itemData), ); }, onSelected: (selectedItemData) { onSelected ?? onSelected!(selectedItemData); }); } }