How to dynamically resize text in Flutter?

后端 未结 7 1814
梦如初夏
梦如初夏 2021-01-31 06:53

I retrieve a piece of text from an API. I want to allot a set amount of space to it (say a max Container with width: 300.0 and height: 100.0). Sometimes, the piece of text fits

7条回答
  •  感动是毒
    2021-01-31 07:52

    You can do it using the auto_size_text package:

    Container(
      child: ConstrainedBox(
        constraints: BoxConstraints(
          minWidth: 300.0,
          maxWidth: 300.0,
          minHeight: 30.0,
          maxHeight: 100.0,
        ),
        child: AutoSizeText(
          "yourText",
          style: TextStyle(fontSize: 30.0),
        ),
      ),
    );
    

    You can also set maxLines to constrain the text even further or use presetFontSizes if you only want to allow specific font sizes.

提交回复
热议问题