Flutter文本溢出处理插件ellipsis_overflow_text的使用

Flutter文本溢出处理插件ellipsis_overflow_text的使用

ellipsis_overflow_text

Version GitHub license

Flutter小部件,自动设置在具有省略号溢出类型的文本上要显示的行数。

请给项目点个星星以支持!

AutoSizeText包启发并基于该包。

资源:

目录

用法

常规用法

EllipsisOverflowText的行为与Text完全相同。唯一的不同在于它会自动设置文本上要显示的行数,并采用省略号溢出类型。

EllipsisOverflowText(
  'Some text here',
  style: TextStyle(fontSize: 22),
)

在换行溢出时显示省略号

若要在最后一行文本结束时显示省略号溢出,可以将showEllipsisOnBreakLineOverflow属性设置为true

默认行为:

EllipsisOverflowText(
  'First String here\n'
  'This String will fit the given bounds.',
  style: TextStyle(fontSize: 22),
)

image

设置showEllipsisOnBreakLineOverflowtrue时:

EllipsisOverflowText(
  'First String here\n'
  'This String will fit the given bounds.',
  showEllipsisOnBreakLineOverflow: true,
),

image

参数

参数 描述
data 需要显示的字符串文本。
key 控制一个组件如何替换树中的另一个组件。
textKey 内部Text小部件的键。
locale 用于选择字体,因为相同的Unicode字符根据不同的地区可能有不同的渲染方式。
maxLines 文本可以跨越的最大行数。
semanticsLabel 文本的替代语义标签。
selectionColor 用于绘制选择的颜色。
softWrap 文本是否应该在软换行处断开。
strutStyle 定义支柱,即相对于基线的最小行高。
style 如果非空,则为此文本使用的样式。
textAlign 文本水平对齐的方式。
textDirection 文本的方向性。此决定textAlign值如TextAlign.startTextAlign.end如何解释。
textHeightBehavior 定义如何应用[TextStyle.height]在文本上方和下方。
textScaleFactor 每个逻辑像素的字体像素数。
textWidthBasis 测量一行或多行文本宽度的不同方式。
showEllipsisOnBreakLineOverflow 是否在最后一行文本以换行符(例如"\n")结尾时显示省略号溢出。

带*标记的参数与Text中的行为完全相同。

性能

EllipsisOverflowText非常快。实际上,你可以将所有的Text小部件替换为EllipsisOverflowText

故障排除

缺少约束

如果EllipsisOverflowText溢出或没有用省略号替换文本,你应该检查它是否有宽度和高度的约束。

错误代码:

Row(
  children: [
    EllipsisOverflowText(
      'Here is a very long text, which will use ellipsis in case the text overflows',
    ),
  ],
)

由于Row和其他像ContainerColumnListView这样的小部件不会约束它们的子组件,因此文本将会溢出。你可以通过约束EllipsisOverflowText来解决这个问题。如果是RowColumn,可以用Expanded包裹;也可以使用SizedBox或其他具有固定宽度(和高度)的小部件。

正确代码:

Row(
  children: [
    Expanded( // 约束EllipsisOverflowText到Row的宽度(你也可以使用Flexible)
      child: EllipsisOverflowText(
        'Here is a very long text, which will use ellipsis in case the text overflows',
      )
    ),
  ],
)

进一步的解释可以在这里找到,问题关于AutoSizeText包,但答案同样适用于本包。如果你仍然有问题,请在这里打开一个问题。

MIT 许可证

Copyright (c) 2022 Vitor Lucas (Reiko-Dev)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

示例代码

以下是一个完整的示例代码,展示了如何使用EllipsisOverflowText

import 'package:ellipsis_overflow_text/ellipsis_overflow_text.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const App());
}

class App extends StatelessWidget {
  const App({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return Scaffold(
      backgroundColor: Colors.black,
      body: Center(
        child: Container(
          width: size.width * .3,
          height: size.height * .25,
          color: Colors.white,
          child: const EllipsisOverflowText(
            'This string will automatically fit within the bounds of the container.',
            style: TextStyle(color: Colors.white),
          ),
        ),
      ),
    );
  }
}

更多关于Flutter文本溢出处理插件ellipsis_overflow_text的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter文本溢出处理插件ellipsis_overflow_text的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter中使用ellipsis_overflow_text插件来处理文本溢出的示例代码。这个插件提供了一个更灵活的方式来显示带有省略号的溢出文本。

首先,确保你已经在pubspec.yaml文件中添加了ellipsis_overflow_text依赖:

dependencies:
  flutter:
    sdk: flutter
  ellipsis_overflow_text: ^最新版本号  # 请替换为最新的版本号

然后,运行flutter pub get来安装依赖。

接下来是一个使用ellipsis_overflow_text的示例代码:

import 'package:flutter/material.dart';
import 'package:ellipsis_overflow_text/ellipsis_overflow_text.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Ellipsis Overflow Text Demo'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              // 使用 EllipsisOverflowText 处理文本溢出
              EllipsisOverflowText(
                '这是一个非常长的文本,需要被截断并显示省略号...。',
                style: TextStyle(fontSize: 20),
                maxLines: 1, // 最大显示行数
                overflowWidget: Text('...'), // 自定义溢出显示内容
              ),
              SizedBox(height: 20),
              // 多行文本溢出处理示例
              EllipsisOverflowText(
                '这是一个多行的非常长的文本示例。当文本超出容器宽度时,它将被截断并显示省略号。这可以用于各种UI组件中,以提供更好的用户体验。',
                style: TextStyle(fontSize: 16),
                maxLines: 3, // 最大显示行数
                ellipsis: '...', // 省略号文本
                alignment: TextAlign.justify, // 文本对齐方式
              ),
            ],
          ),
        ),
      ),
    );
  }
}

在这个示例中,我们展示了如何使用EllipsisOverflowText来处理单行和多行文本的溢出。EllipsisOverflowText提供了几个关键的参数:

  • text: 要显示的文本。
  • style: 文本样式。
  • maxLines: 最大显示行数。
  • ellipsis: 自定义省略号文本(可选,默认是...)。
  • overflowWidget: 自定义溢出显示内容(可选,通常用于更复杂的情况)。
  • alignment: 文本对齐方式。

这些参数可以帮助你根据具体需求灵活地处理文本溢出显示。注意,ellipsis_overflow_text插件可能在不同版本中会有不同的API,因此请参考最新的文档和示例代码进行调整。

回到顶部