$switchFor<TKey, TValue> method

Widget $switchFor<TKey, TValue>(
  1. Object propertyKey, {
  2. Map<TKey, ValueWidgetBuilder<TValue>>? options,
  3. ValueWidgetBuilder<TValue>? defalut,
  4. Widget? child,
  5. TKey valueToKey(
    1. TValue
    )?,
  6. TValue? initialValue,
})

绑定到指定属性, 当 propertyKey 对应属性值发生变化时, 其值做为 keyoptions 中查找对应 Widget 构建方法, 若未找到则使用 default 构建, 如 defaultnull 则不构建 Widget

如值与 optionskey 类型不同, 可通过指定 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);