Line data Source code
1 : import 'package:basf_flutter_components/basf_flutter_components.dart'; 2 : import 'package:flutter/material.dart'; 3 : 4 : /// {@template radio} 5 : /// Radio option row with BASF Style 6 : /// {@radio} 7 : class RadioOptions<T> extends StatelessWidget { 8 : /// {@macro radio} 9 1 : const RadioOptions({ 10 : super.key, 11 : required this.values, 12 : required this.selectedValue, 13 : required this.title, 14 : required this.onSelected, 15 : required this.labelGenerator, 16 : this.descriptionGenerator, 17 : this.titleStyle, 18 : this.identityHelperFunction, 19 : this.separator, 20 : }); 21 : 22 : /// List of values 23 : final List<T> values; 24 : 25 : /// Selected value 26 : final T selectedValue; 27 : 28 : /// Radio title 29 : final String title; 30 : 31 : /// Text style 32 : final TextStyle? titleStyle; 33 : 34 : /// Action to be performed on selection 35 : final ValueChanged<T> onSelected; 36 : 37 : /// Helper identity function 38 : final T Function(T)? identityHelperFunction; 39 : 40 : /// Label generator function 41 : final String Function(T value) labelGenerator; 42 : 43 : /// Description generator function 44 : final String Function(T value)? descriptionGenerator; 45 : 46 : /// Widget separation 47 : final Widget? separator; 48 : // typedef StringGetter<T> = String Function(T value); 49 : 50 1 : @override 51 : Widget build(BuildContext context) { 52 1 : final theme = Theme.of(context); 53 : 54 1 : return Column( 55 : crossAxisAlignment: CrossAxisAlignment.start, 56 1 : children: [ 57 1 : Text( 58 1 : title, 59 3 : style: titleStyle ?? theme.textTheme.bodyText2, 60 : ), 61 1 : VerticalSpacer.normal(), 62 1 : Row( 63 : crossAxisAlignment: CrossAxisAlignment.start, 64 1 : children: List.generate( 65 2 : values.length, 66 2 : (index) => Expanded( 67 1 : child: OptionButton( 68 4 : labelText: labelGenerator(values[index]), 69 1 : description: descriptionGenerator?.call(values[index]), 70 1 : isSelected: identityHelperFunction != null 71 4 : ? identityHelperFunction?.call(selectedValue) == 72 4 : identityHelperFunction?.call(values[index]) 73 4 : : selectedValue == values[index], 74 1 : onPressed: () { 75 4 : onSelected(values[index]); 76 : }, 77 : ), 78 : ), 79 3 : ).joinWithSeparator(separator ?? HorizontalSpacer.semi()), 80 : ), 81 : ], 82 : ); 83 : } 84 : }