How to dynamically resize text in Flutter?

后端 未结 7 1740
梦如初夏
梦如初夏 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:32

    Using BoxFit.scaleDown and fixing the FontSize you can adjust the maximum size of the font.

    If the content is small, it occupies the minimum width with the specified font size. At the same time, if the content is large, it resizes to the smallest font size.

    FittedBox(
                          fit: BoxFit.scaleDown,
                          child: 
                              Text(
                                "Text here",
                                style: TextStyle(fontSize: 18),
                              ),)
    

    If you need the text to fill the entire width, using any font size use BoxFit.cover

    FittedBox(
                          fit: BoxFit.cover,
                          child: 
                              Text(
                                "Text here",
                                //style: TextStyle(fontSize: 18),
                              ),)
    

提交回复
热议问题