forceWidget function
- dynamic item, {
- TextStyle? style,
- StrutStyle? strutStyle,
- TextAlign? textAlign,
- TextDirection? textDirection,
- Locale? locale,
- bool? softWrap,
- TextOverflow? overflow,
- TextScaler? textScaler,
- int? maxLines,
- String? semanticsLabel,
- TextWidthBasis? textWidthBasis,
- bool validate = true,
- String? defaultText,
- BoxFit? fit,
- AlignmentGeometry? alignment,
- ResponsiveColumn? columnSizes,
A utility extension method that allows forcing a widget to be returned, with optional customization of its properties.
This method can be used on any object, and it will return a Widget if the object
is already a widget, or it will convert the object to a Text widget with the provided
customizations if the object is a Text. If the object is neither a Widget nor a Text,
it will be converted to a Text widget using the asNullableText
method.
The optional parameters can be used to customize the properties of the returned widget,
such as the style
, textAlign
, maxLines
, etc.
Example 1:
Text myText = Text('Hello');
Widget myWidget = myText.forceWidget(style: TextStyle(fontSize: 16));
Example 2:
Text anotherText = Text('Welcome');
Widget anotherWidget = anotherText.forceWidget(
style: TextStyle(fontSize: 20),
textAlign: TextAlign.center,
);
Example 3:
String myString = 'Hello World';
Widget myStringWidget = myString.forceWidget(
style: TextStyle(color: Colors.blue),
defaultText: 'No text provided',
);
Example 4:
int myNumber = 42;
Widget myNumberWidget = myNumber.forceWidget(
style: TextStyle(fontWeight: FontWeight.bold),
defaultText: 'No number provided',
);
Example 5:
bool myBool = true;
Widget myBoolWidget = myBool.forceWidget(
style: TextStyle(fontStyle: FontStyle.italic),
defaultText: 'No boolean provided',
);
Example 6:
double myDouble = 3.14;
Widget myDoubleWidget = myDouble.forceWidget(
style: TextStyle(color: Colors.red),
defaultText: 'No double provided',
);
Example 7:
List<String> myList = ['Apple', 'Banana', 'Orange'];
Widget myListWidget = myList.forceWidget(
style: TextStyle(color: Colors.green),
defaultText: 'No list provided',
);
Example 8:
Map<String, int> myMap = {'Apple': 1, 'Banana': 2, 'Orange': 3};
Widget myMapWidget = myMap.forceWidget(
style: TextStyle(color: Colors.orange),
defaultText: 'No map provided',
);
Example 9:
DateTime myDateTime = DateTime.now();
Widget myDateTimeWidget = myDateTime.forceWidget(
style: TextStyle(color: Colors.purple),
defaultText: 'No date provided',
);
Example 10:
Object myObject = Object();
Widget myObjectWidget = myObject.forceWidget(
style: TextStyle(color: Colors.yellow),
defaultText: 'No object provided',
);
Implementation
Widget? forceWidget(dynamic item, {TextStyle? style, StrutStyle? strutStyle, TextAlign? textAlign, TextDirection? textDirection, Locale? locale, bool? softWrap, TextOverflow? overflow, TextScaler? textScaler, int? maxLines, String? semanticsLabel, TextWidthBasis? textWidthBasis, bool validate = true, String? defaultText, BoxFit? fit, AlignmentGeometry? alignment, ResponsiveColumn? columnSizes}) {
if (item == null) return null;
if (item is Widget) return item;
if (item is ResponsiveColumn) return item.child;
if (item is Iterable) {
return ResponsiveRow.withAutoColumns(
children: item.map((e) {
dynamic w = forceWidget(e, style: style, strutStyle: strutStyle, textAlign: textAlign, textDirection: textDirection, locale: locale, softWrap: softWrap, overflow: overflow, textScaler: textScaler, maxLines: maxLines, semanticsLabel: semanticsLabel, textWidthBasis: textWidthBasis, validate: validate, defaultText: defaultText, fit: fit, alignment: alignment);
if (columnSizes != null) w = ResponsiveColumn.copy(columnSizes, child: w);
return w;
}).toList(),
);
}
if (item is IconData) {
return (item).asIcon(
size: style?.fontSize,
color: style?.color,
applyTextScaling: textScaler != null,
weight: style?.fontWeight?.value.toDouble(),
semanticLabel: semanticsLabel ?? defaultText,
shadows: style?.shadows,
textDirection: textDirection,
);
}
if (item is ImageProvider) {
return Image(
image: item,
fit: fit,
semanticLabel: semanticsLabel,
alignment: alignment ?? Alignment.center,
color: style?.color,
);
}
textAlign ??= alignment?.toTextAlign;
return (item as Object?).asNullableText(
style: style,
strutStyle: strutStyle,
textAlign: textAlign,
textDirection: textDirection,
locale: locale,
softWrap: softWrap,
overflow: overflow,
textScaler: textScaler,
maxLines: maxLines,
semanticsLabel: semanticsLabel,
textWidthBasis: textWidthBasis,
validate: validate,
defaultText: defaultText,
);
}