How to automatically scale a font in a text widget to match the maximum number of lines?

I am trying to use BoxFit.scaleDownin FittedBoxto reduce the font size in a Textwidget to accommodate strings of different lengths.

However, the code below will shorten the entire line and put it on one line. For the example below, I would like the font to be smaller so that the line can fit in two lines (for the maxLineswidget property Text).

enter image description here

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Multi-Line Label Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Multi-Line Label Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    final textStyle = const TextStyle(fontSize: 28.0);
    final text =
        'Lorem Ipsum is simply dummy text of the printing and typesetting'
        'industry. Lorem Ipsum has been the industry\ standard dummy text'
        'ever since the 1500s, when an unknown printer took a galley of type'
        'and scrambled it to make a type specimen book.';
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new FittedBox(
              fit: BoxFit.scaleDown,
              child: new Text(
                text,
                maxLines: 2,
                style: textStyle,
                textAlign: TextAlign.center,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
+5
source share
5 answers

You can use the automatic text package.

https://pub.dartlang.org/packages/auto_size_text

"" "AutoSizeText", , :)

+2

2 , .

1.

, , , .

. . , ( , , "t" ). , , SizedBox , . FittedBox.

, , .

2.

LayoutBuilder, , , , .

, , , . , ,


, ,

+3

new Flexible(
  child: new Text(
    text,
    style: textStyle,
    textAlign: TextAlign.center,
  ),
),
0

:

This is with a long title This is the long title in the horizontal mode

, , Text FontSize, .

, . . ( ), LayoutBuilder:

new LayoutBuilder(builder: (context, constraint) {
   //screen width got from the LayoutBuilder
   double maxWidth = constraint.biggest.width;

   //I'm going to change this dinamically
   double fontSize = 33.0;

   //return my widget
   return new Text(
        _brand.name,
        maxLines: 1,
        style: new TextStyle(fontSize: fontSize),
    );
}

, , . const (maxCharInOneLine), : maxCharInOneLine:

new LayoutBuilder(builder: (context, constraint) {
   //screen width got from the LayoutBuilder
   double maxWidth = constraint.biggest.width;

   //chars shown manually counted
   const int maxCharInOneLine = 17;

   //single char width
   int charWidth = (maxWidth / maxCharInOneLine).toInt();

   //TO RUN IN DEBUG MODE
   print("Char size with this screen and Text TextStyle: " + charWidth.toString());
   ...
}

, charSize, const , .

, ( LayoutBuilder), .

new LayoutBuilder(builder: (context, constraint) {
    const int charWidth=16;
    //screen width got from the LayoutBuilder
    double maxWidth = constraint.biggest.width;

    //dinamically get the char number that fits in one line with this screen
    int charsPerLine = (maxWidth / charWidth).toInt();

    //if it doesn't fit in a line, reduce the fontSize
    double fontSize = (_brand.name.length <= charsPerLine) ? 33.0 : 23.0;

    return new Text(
        _brand.name,
        maxLines: 1,
        style: new TextStyle(fontSize: fontSize),
    );
}

:

The text is dynamically reduced The long title in the horizontal mode has the same size

, .

. :)

0

A simple solution is to use FittedBoxwith fit: BoxFit.scaleDown:

Container(
  width: 100.0,
  child: FittedBox(
    fit: BoxFit.scaleDown,
    // Optionally apply 'alignment' to position the text inside the box:
    //alignment: Alignment.topRight,
    child: SizedBox(
      child: Text('\$1,299.99'),
  )
)
0
source

Source: https://habr.com/ru/post/1690299/


All Articles