$switchFor<TKey, TValue> method
Widget
$switchFor<TKey, TValue>(
- Object propertyKey, {
- Map<
TKey, ValueWidgetBuilder< ? options,TValue> > - ValueWidgetBuilder<
TValue> ? defalut, - Widget? child,
- TKey valueToKey(
- TValue
- TValue? initialValue,
绑定到指定属性, 当 propertyKey
对应属性值发生变化时,
其值做为 key
到 options
中查找对应 Widget 构建方法,
若未找到则使用 default
构建, 如 default
为 null
则不构建 Widget
如值与 options
中 key
类型不同, 可通过指定 valueToKey
进行转换
child
用于向构建方法中传入 Widget
// example
@override
Widget build(BuildContext context) {
return $switchFor<String, int>(#account,
options: { "tom": (context, value, child) => Text("${value}! cat"),
"jerry": (context, value, child) => Text("mouse") },
default: (context, value, child) => Text("default"));
}
Implementation
Widget $switchFor<TKey, TValue>(Object propertyKey,
{Map<TKey, ValueWidgetBuilder<TValue>>? options,
ValueWidgetBuilder<TValue>? defalut,
Widget? child,
TKey Function(TValue)? valueToKey,
TValue? initialValue}) =>
$switch<TKey, TValue>(
ensureProperty<TValue>(propertyKey, initialValue: initialValue),
options: options,
defalut: defalut,
child: child,
valueToKey: valueToKey);