搜尋套件、儲存庫與 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)加入圓角背景。支援自訂顏色、邊框半徑與內縮間距,實現視覺上吸引人的文字強調效果。非常適合打造重視文字可讀性與設計美感的現代介面。此套件支援所有主要平台——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!

所屬分類

原始碼儲存庫

使用此套件的專案