radioButtonsBuilder<T> function

List<Widget> radioButtonsBuilder<T>(
  1. dynamic items,
  2. RadioButtonsController controller, {
  3. double? space,
  4. MouseCursor? mouseCursor,
  5. bool? toggleable,
  6. Color? activeColor,
  7. WidgetStateProperty<Color?>? fillColor,
  8. MaterialTapTargetSize? materialTapTargetSize,
  9. VisualDensity? visualDensity,
  10. Color? focusColor,
  11. Color? hoverColor,
  12. WidgetStateProperty<Color?>? overlayColor,
  13. double? splashRadius,
  14. FocusNode? focusNode,
  15. bool? autofocus,
  16. bool? useCupertinoCheckmarkStyle,
  17. bool? textFirst,
  18. TextStyle? style,
  19. StrutStyle? strutStyle,
  20. TextAlign? textAlign,
  21. TextDirection? textDirection,
  22. Locale? locale,
  23. bool? softWrap,
  24. TextOverflow? overflow,
  25. TextScaler? textScaler,
  26. int? maxLines,
  27. String? semanticsLabel,
  28. TextWidthBasis? textWidthBasis,
  29. TextHeightBehavior? textHeightBehavior,
  30. Color? selectionColor,
  31. int? flex,
  32. FlexFit? fit,
  33. Axis? direction,
  34. MainAxisAlignment? mainAxisAlignment,
  35. MainAxisSize? mainAxisSize,
  36. CrossAxisAlignment? crossAxisAlignment,
  37. VerticalDirection? verticalDirection,
  38. TextBaseline? textBaseline,
  39. Clip? clipBehavior,
})

Returns the 'Radio' widgets

Implementation

List<Widget> radioButtonsBuilder<T>(
  // Radio
  dynamic items,
  RadioButtonsController<dynamic> controller, {
  double? space,
  MouseCursor? mouseCursor,
  bool? toggleable,
  Color? activeColor,
  WidgetStateProperty<Color?>? fillColor,
  MaterialTapTargetSize? materialTapTargetSize,
  VisualDensity? visualDensity,
  Color? focusColor,
  Color? hoverColor,
  WidgetStateProperty<Color?>? overlayColor,
  double? splashRadius,
  FocusNode? focusNode,
  bool? autofocus,
  bool? useCupertinoCheckmarkStyle,
  bool? textFirst,
  // Text
  TextStyle? style,
  StrutStyle? strutStyle,
  TextAlign? textAlign,
  TextDirection? textDirection,
  Locale? locale,
  bool? softWrap,
  TextOverflow? overflow,
  TextScaler? textScaler,
  int? maxLines,
  String? semanticsLabel,
  TextWidthBasis? textWidthBasis,
  ui.TextHeightBehavior? textHeightBehavior,
  Color? selectionColor,
  // Flexible
  int? flex,
  FlexFit? fit,
  // Flex
  Axis? direction, // Axis.vertical or Axis.horizontal
  MainAxisAlignment? mainAxisAlignment,
  MainAxisSize? mainAxisSize,
  CrossAxisAlignment? crossAxisAlignment,
  VerticalDirection? verticalDirection,
  TextBaseline? textBaseline,
  Clip? clipBehavior,
}) {
  //
  Radio<T> buildRadioButton(T value) => Radio<T>.adaptive(
        value: value,
        groupValue: controller.groupValue,
        onChanged: controller.disabled ? null : controller.onChanged,
        mouseCursor: mouseCursor,
        toggleable: toggleable ?? false,
        activeColor: activeColor,
        fillColor: fillColor,
        focusColor: focusColor,
        hoverColor: hoverColor,
        overlayColor: overlayColor,
        splashRadius: splashRadius,
        materialTapTargetSize: materialTapTargetSize,
        visualDensity: visualDensity,
        focusNode: focusNode,
        autofocus: autofocus ?? false,
        useCupertinoCheckmarkStyle: useCupertinoCheckmarkStyle ?? false,
      );

  space ??= 0;

  final List<Widget> radioButtons = [];

  assert(() {
    if (controller.type != T) {
      debugPrint(
          '############ controller.type == T fails in radiobutton_widget.dart');
    }
    return true;
  }());

  if (controller.type == T) {
    //
    if (items is List<String>) {
      //
      assert(() {
        if (items.runtimeType != T) {
          debugPrint(
              '############ items.runtimeType == T fails in radiobutton_widget.dart');
        }
        return true;
      }());

      if (items.runtimeType == T) {
        //
        for (final value in items) {
          //
          var radioItems = [
            Flexible(
              child: buildRadioButton(value as T),
            ),
            if (space > 0) SizedBox(width: space),
            Flexible(
              flex: flex ?? 1,
              fit: fit ?? FlexFit.loose,
              child: Text(
                value,
                style: style,
                strutStyle: strutStyle,
                textAlign: textAlign,
                textDirection: textDirection,
                locale: locale,
                softWrap: softWrap,
                overflow: overflow,
                textScaler: textScaler,
                maxLines: maxLines,
                semanticsLabel: semanticsLabel,
                textWidthBasis: textWidthBasis,
                textHeightBehavior: textHeightBehavior,
                selectionColor: selectionColor,
              ),
            ),
          ];

          if (textFirst ?? false) {
            radioItems = radioItems.reversed.toList(growable: false);
          }

          radioButtons.add(
            Flex(
                direction: direction ?? Axis.horizontal,
                mainAxisAlignment: mainAxisAlignment ?? MainAxisAlignment.start,
                mainAxisSize: mainAxisSize ?? MainAxisSize.max,
                crossAxisAlignment:
                    crossAxisAlignment ?? CrossAxisAlignment.center,
                textDirection: textDirection,
                verticalDirection: verticalDirection ?? VerticalDirection.down,
                textBaseline: textBaseline,
                clipBehavior: clipBehavior ?? Clip.none,
                children: radioItems),
          );
        }
      }
    } else if (items is Map<String, T>) {
      //
      items.forEach((String key, T value) {
        //
        var radioItems = [
          Flexible(
            child: buildRadioButton(value),
          ),
          if (space! > 0) SizedBox(width: space),
          Flexible(
            flex: flex ?? 1,
            fit: fit ?? FlexFit.loose,
            child: Text(
              key,
              style: style,
              strutStyle: strutStyle,
              textAlign: textAlign,
              textDirection: textDirection,
              locale: locale,
              softWrap: softWrap,
              overflow: overflow,
              textScaler: textScaler,
              maxLines: maxLines,
              semanticsLabel: semanticsLabel,
              textWidthBasis: textWidthBasis,
              textHeightBehavior: textHeightBehavior,
              selectionColor: selectionColor,
            ),
          ),
        ];

        if (textFirst ?? false) {
          radioItems = radioItems.reversed.toList(growable: false);
        }

        radioButtons.add(
          Flex(
            direction: direction ?? Axis.horizontal,
            mainAxisAlignment: mainAxisAlignment ?? MainAxisAlignment.start,
            mainAxisSize: mainAxisSize ?? MainAxisSize.max,
            crossAxisAlignment: crossAxisAlignment ?? CrossAxisAlignment.center,
            textDirection: textDirection,
            verticalDirection: verticalDirection ?? VerticalDirection.down,
            textBaseline: textBaseline,
            clipBehavior: clipBehavior ?? Clip.none,
            children: radioItems,
          ),
        );
      });
    }
  }

  // Nothing to show.
  if (radioButtons.isEmpty) {
    //
    assert(() {
      if (radioButtons.isEmpty) {
        debugPrint(
            '############ items should be a List or a Map in radiobutton_widget.dart');
      }
      return true;
    }());
    radioButtons.add(const SizedBox.shrink());
  }
  return radioButtons;
}