MySelectorController<T> class

选择器控制器,封装状态与配置,适合"同页面多选择器"场景。

与 Flutter 的 TextEditingController 类似:每个选择器对应一个 MySelectorController 实例,控制器持有选中状态,外部可直接操控。

基础用法

// 在 GetxController 或 StatefulWidget 的 State 里声明
final colorCtrl = MySelectorController<String>(
  items: [
    MySelectorItem(value: 'red',   title: '红色'),
    MySelectorItem(value: 'green', title: '绿色'),
    MySelectorItem(value: 'blue',  title: '蓝色'),
  ],
);

// 触发弹出(仍需 BuildContext 定位浮层)
GestureDetector(
  onTap: () => colorCtrl.show(context),
  child: Obx(() => Text(colorCtrl.selectedTitle ?? '请选择颜色')),
)

// 外部直接修改选中状态
colorCtrl.setValue('blue');
colorCtrl.clear();

监听变化

// 方式一:onChanged 回调(声明时传入)
final ctrl = MySelectorController<String>(
  items: [...],
  onChanged: (item) => print(item?.title),
);

// 方式二:在 UI 中用 Obx 响应式读取
Obx(() => Text(ctrl.selectedTitle ?? '未选'))

Constructors

MySelectorController({required List<MySelectorItem<T>> items, MySelectorItem<T>? initialItem, T? initialValue, MySelectorClearOption? clearOption, bool allowReselect = false, bool? showPanelAbove, bool showSearch = false, String searchHint = '搜索…', bool searchFilter(MySelectorItem<T> item, String query)?, Widget itemBuilder(BuildContext, MySelectorItem<T>, bool)?, Widget footerBuilder(BuildContext, VoidCallback )?, MySelectorStyle? style, void onChanged(MySelectorItem<T>? item)?})

Properties

allowReselect bool
final
clearOption MySelectorClearOption?
final
footerBuilder Widget Function(BuildContext, VoidCallback )?
final
hashCode int
The hash code for this object.
no setterinherited
itemBuilder Widget Function(BuildContext, MySelectorItem<T>, bool)?
final
items List<MySelectorItem<T>>
选项列表,初始化后不可变更;若需动态列表,直接用 MySelector.show
final
onChanged → void Function(MySelectorItem<T>? item)?
用户在面板内做出操作(选中或清除)后触发;点击外部关闭时触发。
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
rx Rxn<MySelectorItem<T>>
暴露底层 Rxn,便于在 Obx 外部直接监听或绑定复杂逻辑。
no setter
searchFilter bool Function(MySelectorItem<T> item, String query)?
final
searchHint String
final
selectedItem MySelectorItem<T>?
当前选中项(可在 Obx 内读取以响应变化)。
no setter
selectedTitle String?
当前选中项的标题,等同于 selectedItem?.title
no setter
selectedValue → T?
当前选中的值,等同于 selectedItem?.value
no setter
showPanelAbove bool?
final
showSearch bool
final
style MySelectorStyle?
final

Methods

clear() → void
清除选中状态。
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
setItem(MySelectorItem<T>? item) → void
直接设置选中项;传 null 等同于 clear
setValue(T? value) → void
value 设置选中项;找不到对应项时效果等同于 clear
show(BuildContext context) Future<void>
context 所对应的 Widget 附近弹出选择面板。
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited