textOrNull function

Widget? textOrNull(
  1. dynamic txt, {
  2. TextAlign? alignment,
  3. TextStyle? style,
  4. Color? color,
  5. bool wrap = true,
  6. int? maxLines,
})

Implementation

Widget? textOrNull(final txt, {TextAlign? alignment, TextStyle? style, Color? color, bool wrap = true, int? maxLines}) {
  if (txt is Widget) return txt;
  if (txt == null) return null;
  final text = "$txt";
  var _style = style ?? TextStyle();
  _style = _style.copyWith(color: color);

  return text.isNullOrBlank
      ? null
      : wrap == false
          ? maxLines != null && maxLines > 1
              ? Text(
                  text,
                  maxLines: maxLines,
                  style: _style,
                  textAlign: alignment,
                  softWrap: false,
                  overflow: TextOverflow.fade,
                )
              : SingleLineText(
                  text,
                  style: _style,
                  alignment: alignment,
                )
          : Text(
              text,
              maxLines: maxLines,
              style: _style,
              textAlign: alignment,
              softWrap: true,
              overflow: TextOverflow.fade,
            );
}