asNullableText method

Text? asNullableText({
  1. TextStyle? style,
  2. StrutStyle? strutStyle,
  3. TextAlign? textAlign,
  4. TextDirection? textDirection,
  5. Locale? locale,
  6. bool? softWrap,
  7. TextOverflow? overflow,
  8. TextScaler? textScaler,
  9. int? maxLines,
  10. String? semanticsLabel,
  11. TextWidthBasis? textWidthBasis,
  12. bool validate = true,
  13. String? defaultText,
  14. string dateFormat = "",
})

Converts the object to a nullable Text widget with the specified properties.

If the object is null, it returns null. If the object is already a Text widget, it returns the object itself. If validate is true and the object is not valid (see isValid()), it returns null. Otherwise, it converts the object to a Text widget with the string representation of the object.

The style, strutStyle, textAlign, textDirection, locale, softWrap, overflow, textScaler, maxLines, semanticsLabel, and textWidthBasis parameters are used to customize the appearance and behavior of the Text widget.

Returns a nullable Text widget with the specified properties.

Implementation

Text? asNullableText({
  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,
  string dateFormat = "",
}) {
  Text? text;

  locale ??= platformLocale;

  if (this == null && defaultText == null) return null;

  if (this is Text) {
    text = this as Text;
  } else {
    if (validate == false || this.isValid()) {
      if (this is Map || this is List) {
        text = Text(jsonEncode(this));
      } else if (this is DateTime) {
        text = Text((this as DateTime).format(dateFormat, locale.toLanguageTag()));
      } else {
        text = Text("$this" | defaultText);
      }
    } else if (defaultText != null) {
      text = Text(defaultText);
    }
  }

  return text?.copyWith(
    style: style,
    strutStyle: strutStyle,
    textAlign: textAlign,
    textDirection: textDirection,
    locale: locale,
    softWrap: softWrap,
    overflow: overflow,
    textScaler: textScaler,
    maxLines: maxLines,
    semanticsLabel: semanticsLabel,
    textWidthBasis: textWidthBasis,
  );
}