首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Flutter TextField -如何在输入的文本溢出时缩小字体

Flutter TextField -如何在输入的文本溢出时缩小字体
EN

Stack Overflow用户
提问于 2018-07-29 21:11:29
回答 1查看 6.7K关注 0票数 2

我有一个TextField (不是文本)小部件,它必须保持在一行上。我想缩小它的字体大小,如果输入的文本太大的TextField框,即缩小它,如果它溢出。我该怎么做呢?

我在有状态组件中编写了一些代码,如下所示

代码语言:javascript
复制
if (textLength < 32) {
  newAutoTextVM.fontSize = 35.0;
} else if (textLength < 42) {
  newAutoTextVM.fontSize = 25.0;

在视图中

代码语言:javascript
复制
fontSize: 25.0,

但它不是非常智能,它不能处理大小调整,而且,因为字体大小不是等宽的(快递等),不同的字符占用不同的空间。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-29 22:01:51

使用TextPainter计算文本的宽度。使用GlobalKey获取小部件的大小( LayoutBuilder可能更适合处理屏幕旋转)。

代码语言:javascript
复制
import 'package:flutter/material.dart';

main() => runApp(MaterialApp(home: Home()));

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

const textFieldPadding = EdgeInsets.all(8.0);
const textFieldTextStyle = TextStyle(fontSize: 30.0);

class _HomeState extends State<Home> {
  final TextEditingController _controller = TextEditingController();
  final GlobalKey _textFieldKey = GlobalKey();

  double _textWidth = 0.0;
  double _fontSize = textFieldTextStyle.fontSize;

  @override
  void initState() {
    super.initState();
    _controller.addListener(_onTextChanged);
  }

  void _onTextChanged() {
    // substract text field padding to get available space
    final inputWidth = _textFieldKey.currentContext.size.width - textFieldPadding.horizontal;

    // calculate width of text using text painter
    final textPainter = TextPainter(
      textDirection: TextDirection.ltr,
      text: TextSpan(
        text: _controller.text,
        style: textFieldTextStyle,
      ),
    );
    textPainter.layout();

    var textWidth = textPainter.width;
    var fontSize = textFieldTextStyle.fontSize;

    // not really efficient and doesn't find the perfect size, but you got all you need!
    while (textWidth > inputWidth && fontSize > 1.0) {
      fontSize -= 0.5;
      textPainter.text = TextSpan(
        text: _controller.text,
        style: textFieldTextStyle.copyWith(fontSize: fontSize),
      );
      textPainter.layout();
      textWidth = textPainter.width;
    }

    setState(() {
      _textWidth = textPainter.width;
      _fontSize = fontSize;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Autosize TextField'),
      ),
      body: Padding(
        padding: EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            TextField(
              key: _textFieldKey,
              controller: _controller,
              decoration: InputDecoration(
                border: InputBorder.none,
                fillColor: Colors.orange,
                filled: true,
                contentPadding: textFieldPadding,
              ),
              style: textFieldTextStyle.copyWith(fontSize: _fontSize),
            ),
            Text('Text width:'),
            Container(
              padding: textFieldPadding,
              color: Colors.orange,
              child: Row(
                children: <Widget>[
                  Container(width: _textWidth, height: 20.0, color: Colors.blue),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51580658

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档