• 【Flutter】【package】auto_size_text 文字自动适配大小



    在这里插入图片描述

    前言

    auto_size_text :https://pub.flutter-io.cn/packages/auto_size_text


    一、auto_size_text 是什么?

    第三方的插件,能够自动适配你的文本的大小。来适应边界。

    二、使用

    1.简单的使用

    style 部分同text的一样的,基础的功能设备都是同text 文本的使用一样。
    下面做一个简单的对比。我们限制一个宽度高度。在里面放入文本。

    • 使用text:文字显示不全的
    return Scaffold(
        appBar: AppBar(
          // Here we take the value from the MyHomePage object that was created by
          // the App.build method, and use it to set our appbar title.
          title: Text(widget.title),
        ),
        body: const Center(
            child: SizedBox(
          width: 200,
          height: 140,
          child: Text(
              style: TextStyle(fontSize: 100),
              maxLines: 2,
              'Here we take the value from the MyHomePage object that was created by'),
        ))
    
        // This trailing comma makes auto-formatting nicer for build methods.
        );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    • 使用了auto_size_text :自动缩小了文本的size,达到能显示的情况
        return Scaffold(
            appBar: AppBar(
              // Here we take the value from the MyHomePage object that was created by
              // the App.build method, and use it to set our appbar title.
              title: Text(widget.title),
            ),
            body: const Center(
                child: SizedBox(
              width: 200,
              height: 140,
              child: AutoSizeText(
                  style: TextStyle(fontSize: 30),
                  maxLines: 2,
                  'Here we take the value from the MyHomePage object that was created by'),
            ))
    
            // This trailing comma makes auto-formatting nicer for build methods.
            );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    2.参数说明

    参数描述
    key*控制一个构件如何替换树中的另一个构件。
    textKey设置结果小组件的键Text
    style*如果非 null,则用于此文本的样式
    minFontSize自动调整文本大小时使用的最小文本大小约束。如果设置了预设字体大小,则被忽略。
    maxFontSize自动调整文本大小时使用的最大文本大小约束。如果设置了预设字体大小,则被忽略。
    stepGranularity字体大小适应约束的步长。
    presetFontSizes预定义所有可能的字体大小。重要:必须按降序排列。presetFontSizes
    group同步倍数的大小AutoSizeText
    textAlign*文本应如何水平对齐。
    textDirection*文本的方向性。这决定了如何解释类似值。textAlignTextAlign.startTextAlign.end
    locale*用于在相同的 Unicode 字符可以以不同的方式呈现时选择字体,具体取决于区域设置。
    softWrap*文本是否应在软换行符处中断。
    wrapWords不适合一行的单词是否应换行。默认为true以表现得像文本。
    overflow*应如何处理视觉溢出。
    overflowReplacement如果文本溢出且不适合其边界,则改为显示此微件。
    textScaleFactor*每个逻辑像素的字体像素数。也影响,和。minFontSizemaxFontSizepresetFontSizes
    maxLines文本跨越的可选最大行数。
    semanticsLabel*此文本的替代语义标签。

    3.group

    可以统一各个autosizetext的大小。fontsize大小。来达到统一各个text的字体大小是一致的

        return Scaffold(
            appBar: AppBar(
              // Here we take the value from the MyHomePage object that was created by
              // the App.build method, and use it to set our appbar title.
              title: Text(widget.title),
            ),
            body: Column(
              children: [
                AutoSizeText(
                  "text1",
                  group: myautosize,
               
                  maxLines: 1,
                ),
                AutoSizeText(
                  "Here we take the value from the MyHomePage object that was created byHere we take the value from the MyHomePage object that was created by",
                  minFontSize: 100,
                  group: myautosize,
                )
              ],
            )
    
            // This trailing comma makes auto-formatting nicer for build methods.
            );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • minFontSize: 100, 最终并不会使用这个最小的的这个fontsize,结果如图
      ---

    4.rich text

        return Scaffold(
            appBar: AppBar(
              // Here we take the value from the MyHomePage object that was created by
              // the App.build method, and use it to set our appbar title.
              title: Text(widget.title),
            ),
            body: Column(
              children:const [
                AutoSizeText.rich(
                  TextSpan(text: 'A really long String'),
                  style: TextStyle(fontSize: 20),
                  minFontSize: 5,
                ),
                AutoSizeText.rich(
                  TextSpan(children: [
                    TextSpan(text: '我是 1'),
                    TextSpan(text: '我是 2'),
                    TextSpan(text: '我是 3'),
                    TextSpan(text: '我是 4'),
                    TextSpan(text: '我是 5', style: TextStyle(color: Colors.green)),
                  ]),
                  style: TextStyle(fontSize: 20),
                  minFontSize: 5,
                ),
              ],
            )
    
            // This trailing comma makes auto-formatting nicer for build methods.
            );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    在这里插入图片描述

    总结

    欢迎关注,留言,咨询,交流!
    在这里插入图片描述

  • 相关阅读:
    Qt 学习(四) —— QCheckBox复选框
    这是一个隐藏的(绝世武功)Java 学习路线图,祝你Offer拿到手软
    开源的分布式 NoSQL 数据库管理系统 王者 Cassandra 简介 应用场景 优点 不足 版本历史
    都说“存算分离”好,分布式数据库为何还要“进一步分离”?
    手写ngIf,使用视图容器引用ViewContainerRef和模板引用TemplateRef
    一文了解和使用nginx(附带图文)
    自动驾驶仿真:如何通过TCP方式进行VTD驾驶员仿真
    【C++】1085:球弹跳高度的计算(信息学奥赛)
    Python自然语言处理的力量:NLTK库介绍
    虹科Pico汽车示波器学院 | 第二课直播精彩回顾
  • 原文地址:https://blog.csdn.net/weixin_43444734/article/details/127859144