搜索插件、代码仓与 APP 分类

rounded_background_text 封面
插件详情v0.6.0

rounded_background_text

带有圆角的 Text、TextField 和 TextSpan

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
2690
喜欢数
101
Pub 分数
150

安装插件

flutter pub add rounded_background_text

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Flutter 包,为文本元素(包括 Text、TextField 和 TextSpan)添加圆角背景。它支持自定义颜色、边框半径和内边距,实现视觉上吸引人的文本高亮效果。非常适合创建注重文本可读性和设计美感的现代 UI。该包支持所有主要平台——Android、iOS、Web、macOS、Windows 和 Linux,确保在不同设备上风格一致。通过一条 pub add 命令即可轻松集成,让开发者无需复杂自定义绘图即可实现优雅的文本效果。

适合场景

  • 在应用中突出显示关键文本
  • 创建样式化输入字段
  • 为标签添加视觉强调
  • 设计现代 UI 组件
  • 通过对比增强可访问性
  • 构建交互式文本元素

主要特点

  • 文本的圆角背景
  • 支持带有圆角背景的 TextField
  • 可自定义的边框圆角和内边距
  • 与 TextSpan 兼容
  • 跨平台支持
  • 通过 pub.dev 实现轻松集成

适合对象

  • 寻求视觉精致的UI设计师
  • 开发现代 Flutter 应用的开发者
  • 需要高亮文本元素的应用程序
  • 专注于可访问性和可读性的项目
  • 希望快速实现效果的团队

使用建议

  • 多个实例可能影响性能
  • 需要仔细考虑颜色对比度以确保可访问性
  • 仅限文本装饰使用场景
  • 不适用于复杂布局
  • 依赖于平台渲染的一致性
  • 建议谨慎使用,以避免视觉杂乱

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

rounded_background_text

Highlight text with rounded corners

Content

Features

  • ✅ Highlight Text
  • ✅ Highlight Text Field
  • ✅ Highlight Text Span

Getting started

Import the package:

import 'package:rounded_background_text/rounded_background_text.dart';

Usage

Highlight a simple text:
RoundedBackgroundText(
  'A cool text to be highlighted',
  style: const TextStyle(fontWeight: FontWeight.bold),
  backgroundColor: Colors.white,
),

Simple Text

Multiline text is also supported

RoundedBackgroundText(
  'A cool text to be highlighted\nWith two lines or more',
  style: const TextStyle(fontWeight: FontWeight.bold),
  backgroundColor: Colors.amber,
),

Two Lines Text

Highlight a text field:

You must use a TextEditingController

RoundedBackgroundTextField(
  backgroundColor: Colors.blue,
  style: const TextStyle(fontWeight: FontWeight.bold),
  textAlign: TextAlign.center,
),

The text will be highlighted as the user types

TextField Preview

The text highlight will follow the text field scroll position.

Highlight a text span:

Highlight a small part of a text

RichText(
  text: TextSpan(
    text: 'Start your text and ',
    children: [
      RoundedBackgroundTextSpan(
        text: 'highlight something',
        backgroundColor: Colors.blue,
      ),
      const TextSpan(text: ' when necessary'),
    ],
  ),
),

TextSpan Highlight Preview

Select a highlighted text:

Use RoundedBackgroundText.selectable to make the text selectable:

RoundedBackgroundText.selectable(
  'A cool highlighted text to be selected',
  style: const TextStyle(fontWeight: FontWeight.bold),
  backgroundColor: Colors.white,
),

Selectable Text Preview

You may like to know:

Change the corner radius

You can change the radius of the corners by setting innerRadius and outerRadius:

RoundedBackgroundText(
  'A cool text to be highlighted',
  style: const TextStyle(fontWeight: FontWeight.bold),
  backgroundColor: Colors.white,
  innerRadius: 15.0,
  outerRadius: 10.0,
),

The max allowed value is 20.0. The min is 0.0

Deep dive

This section explains, with details, how the background painting works.

RoundedBackgroundText doesn't use a Text widget under the hood. Instead, it uses a custom text painter to paint the text above the background. As soon as the RoundedBackgroundText widget is initialized, the line metrics for the text is calculated (See computeLineMetrics), and is recalculated when the text changes or when the parent width changes. This is done at built time, resulting in a smooth experience for the user.

To paint the background, the line metrics generated before is used. Each line has 4 properties:

  • x - where the line starts horizontally within the size
  • y - where the line starts vertically within the size
  • width - the horizontal size of the line
  • height - the vertical size of the line

With these values, we can generate the background for each line. The background is generated around the whole text: from top-left to bottom-left to bottom-right to top-right to top-left. This makes it easy to calculate when there is a corner, either outer or inner.

The inner and outer radius are dynamically calculated based on the line height, provided by the line metrics, and the given innerRadius and outerRadius, respectively. By default, innerRadius is 10.0 and outerRadius is 10.0. For safety, in order to keep the roundnesses correct, these values must be in the bounds of 0.0 (min) and 20.0 max, otherwise the painting would be out the line.

Contribution

Feel free to file an issue if you find a problem or make pull requests.

All contributions are welcome!

所属分类

源码仓库

使用此插件的项目