Flutter: лишнее пространство в виджетах многострочного текста - PullRequest
2 голосов
/ 19 июня 2020

У меня есть текстовый виджет, который ограничен Контейнером с BoxConstraints, и я заметил, что, когда текст состоит из нескольких строк, из-за переполнения появляется лишний интервал (справа).

Text widget with overlay

Код для этого без стилизации прост:

Container(
  constraints: BoxConstraints(maxWidth: MediaQuery.of(context).size.width * (2/3)),
  child: Padding(
    padding: const EdgeInsets.symmetric(vertical: 15, horizontal: 20),
    child: Text(
      message
    ),
  ),
)

Вы можете увидеть пробел справа от текста. Глядя на наложение, похоже, что это предполагаемое поведение, но есть ли способ ограничить виджет, чтобы удалить лишнее пространство?

1 Ответ

1 голос
/ 19 июня 2020

Вы ищете свойство: textWidthBasis. Установите его TextWidthBasis.longestLine, и ширина текста изменится в зависимости от longestLine, следовательно, будет удалено пустое пространство справа.

Text("Why don't you I set up an appointment and we can discuss this further...",
                  textWidthBasis: TextWidthBasis.longestLine,),

enter image description here

Полный код:

class MultiLinee extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          color: Colors.grey,
          child: Container(
            constraints: BoxConstraints(maxWidth: MediaQuery
                .of(context)
                .size
                .width * (2 / 3)),
            child: Padding(
              padding: const EdgeInsets.symmetric(vertical: 15, horizontal: 20),
              child: Container(
                color: Colors.yellow,
                child: Text("Why don't you I set up an appointment and we can discuss this further...",
                  textWidthBasis: TextWidthBasis.longestLine,),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
...