【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.
    );

在这里插入图片描述

  • 使用了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.
        );

在这里插入图片描述

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.
        );
  • 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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Flutter Zoomable Image 是一个用于 Flutter 应用程序的库,它提供了一个可缩放和拖动的图像小部件。使用 Flutter Zoomable Image,您可以轻松地实现图像的缩放、拖动和捏放手势操作。这对于创建具有可交互性的图像查看器和画廊等应用程序非常有用。 要使用 Flutter Zoomable Image,您需要在项目的 `pubspec.yaml` 文件中添加依赖项,并运行 `flutter packages get` 命令来获取库。 以下是一个简单的示例代码,演示了如何在 Flutter 中使用 Zoomable Image: ```dart import 'package:flutter/material.dart'; import 'package:flutter_zoomable_image/flutter_zoomable_image.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Zoomable Image Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(), ); } } class MyHomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Zoomable Image Demo'), ), body: Center( child: ZoomableImage( AssetImage('path/to/your/image.jpg'), placeholder: Center(child: CircularProgressIndicator()), backgroundColor: Colors.black, ), ), ); } } ``` 在上面的示例中,我们创建了一个简单的 Flutter 应用程序,其中包含一个使用 ZoomableImage 小部件的页面。ZoomableImage 接受一个 AssetImage 对象作为图像源,并提供了一些可选参数,例如 placeholder(用于在图像加载期间显示的小部件)和 backgroundColor(用于设置图像背景色)。 您可以根据自己的需求定制 Zoomable Image 的样式和行为。要了解更多关于 Flutter Zoomable Image 的信息和用法,请参考官方文档或库的 GitHub 页面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值