fittedbox with maximum two lines in flutter

Viewed 2703
Container(
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 60),
        child: Text(
          //  subCategoryName,
          "This is a very Long Text takes more space",
          textAlign: TextAlign.center,
          maxLines: 2,
          overflow: TextOverflow.fade,
          style: Theme.of(context)
              .textTheme
              .headline3
              .copyWith(color: Colors.white, fontSize: 32),
        ),
      ),
    ),

Look this Picture

I need this line to be a maximum of two lines and shrink the size like a FittedBox (with BoxFit.fitWidth) if the text gets even longer

When I used the FittedBox it looking like this. But I need to take it up to 2 lines if needed.. Any solution is appreciated

2 Answers

Got it working by the reply of Sam Chan. I used the AutoSizeText Package

If you don't want to add an pakage, here is workaround. You can check the length of the text and based on that you can handle it.

// declare a widget
    Widget titleTextWidget = Text(
      title,
      style: TextStyle(
        fontWeight: FontWeight.w600,
      ),
      maxLines: 2,
      textAlign: TextAlign.center,
    );
// use condition to render it.
    title.length > 45
        ? FittedBox(
            child: titleTextWidget,
          )
        : titleTextWidget,
Related