Flutter文本高亮插件color_text_highlighter的使用
Flutter文本高亮插件color_text_highlighter的使用
color_text_highlighter
是一个用于在 Flutter 中高亮显示文本的插件。它允许你轻松地选择并高亮显示包含内联 CSS 内容的 HTML 文本。此外,你还可以添加其他工具栏,如全选、删除、搜索和复制等功能。此插件还支持多颜色高亮显示文本。
示例代码
以下是使用 color_text_highlighter
插件的基本示例:
import 'package:color_text_highlighter/highlighter.dart';
import 'package:color_text_highlighter/highligthedclass.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
// 存储高亮信息的列表
List<HighlightedList> offsetsCase = [];
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Text HIghlighter',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const TextHighlighter(),
);
}
}
class TextHighlighter extends StatefulWidget {
const TextHighlighter({Key? key}) : super(key: key);
[@override](/user/override)
_TextHighlighterState createState() => _TextHighlighterState();
}
class _TextHighlighterState extends State<TextHighlighter> {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFFFFEF4),
appBar: AppBar(title: const Text("Color Text Highlighter"), actions: [
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: () {
setState(() {});
},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(0),
side: const BorderSide(color: Colors.white54)))),
child: const Padding(
padding: EdgeInsets.only(left: 10, right: 10),
child: Text('刷新列表',
style: TextStyle(
color: Colors.white,
fontSize: 12,
fontWeight: FontWeight.bold)),
),
),
),
]),
body: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const SizedBox(
height: 10,
),
const Center(
child: Text(
"已选中的文本列表",
style: TextStyle(
color: Colors.black, fontSize: 22, fontWeight: FontWeight.bold),
)),
offsetsCase.isEmpty
? const Padding(
padding: EdgeInsets.all(8.0),
child: Text("暂无数据"),
)
: ListView.separated(
itemCount: offsetsCase.length,
shrinkWrap: true,
separatorBuilder: (BuildContext context, int index) => const Divider(
height: 1.0,
indent: 1.0,
color: Colors.grey,
),
itemBuilder: (BuildContext context, int index) {
return Dismissible(
key: UniqueKey(),
direction: DismissDirection.endToStart,
onDismissed: (direction) {
setState(() {
offsetsCase.removeAt(index);
});
},
background: Container(
color: const Color(0xff686768),
child: const Padding(
padding: EdgeInsets.all(15),
child: Align(
alignment: Alignment.topRight,
child: FittedBox(
fit: BoxFit.cover,
child: Text('移除',
style: TextStyle(color: Colors.white)),
),
),
),
),
child: Row(
children: [
Container(
height: 100,
width: 7,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(0),
color: Color(int.parse(
"0xff${offsetsCase[index].colour}"))),
),
Flexible(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(offsetsCase[index].highlightedText),
),
),
],
));
},
)
],
)),
floatingActionButton: FloatingActionButton(
child: const Text("数据"),
onPressed: () {
Navigator.of(context, rootNavigator: true).push(MaterialPageRoute(
builder: (context) => const TextHighlighterData()));
},
),
);
}
}
class TextHighlighterData extends StatefulWidget {
const TextHighlighterData({Key? key}) : super(key: key);
[@override](/user/override)
_TextHighlighterDataState createState() => _TextHighlighterDataState();
}
class _TextHighlighterDataState extends State<TextHighlighterData> {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFFFFEF4),
appBar: AppBar(
title: const Text("Color Text Highlighter"),
leading: Padding(
padding: const EdgeInsets.only(left: 8.0),
child: IconButton(
icon: const Icon(
Icons.arrow_back_ios,
size: 20,
),
onPressed: () {
Navigator.pop(context);
},
),
),
),
body: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const SizedBox(
height: 10,
),
const Center(
child: Text(
"文本",
style: TextStyle(
color: Colors.black, fontSize: 22, fontWeight: FontWeight.bold),
)),
Padding(
padding: const EdgeInsets.all(8.0),
child: Highlighter(
textData:
"""Contrary to popular belief, Lore'm Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance.\n The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced
in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).""",
preHighlightedTexts: offsetsCase,
onHighlightedCallback: (list) {
setState(() {
offsetsCase = list;
});
},
),
)
],
)),
);
}
}
更多关于Flutter文本高亮插件color_text_highlighter的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter文本高亮插件color_text_highlighter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
color_text_highlighter
是一个用于在 Flutter 中高亮显示特定文本的插件。它允许你根据正则表达式或其他规则来高亮显示文本中的某些部分。以下是如何使用 color_text_highlighter
插件的步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 color_text_highlighter
插件的依赖:
dependencies:
flutter:
sdk: flutter
color_text_highlighter: ^0.1.0 # 请检查最新版本
然后运行 flutter pub get
来获取依赖。
2. 导入插件
在你的 Dart 文件中导入 color_text_highlighter
插件:
import 'package:color_text_highlighter/color_text_highlighter.dart';
3. 使用 ColorTextHighlighter
你可以在你的 Flutter 应用中使用 ColorTextHighlighter
来高亮显示文本。以下是一个简单的示例:
import 'package:flutter/material.dart';
import 'package:color_text_highlighter/color_text_highlighter.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Text Highlighter Example'),
),
body: Center(
child: ColorTextHighlighter(
text: 'Hello, this is a sample text with some keywords like Flutter, Dart, and Widget.',
wordsToHighlight: ['Flutter', 'Dart', 'Widget'],
highlightColor: Colors.yellow,
textStyle: TextStyle(fontSize: 16, color: Colors.black),
),
),
),
);
}
}
4. 参数说明
text
: 需要高亮的文本。wordsToHighlight
: 需要高亮的单词或短语列表。highlightColor
: 高亮颜色。textStyle
: 文本的样式。
5. 高级用法
你还可以使用正则表达式来高亮显示文本中的特定模式:
ColorTextHighlighter(
text: 'This is a test string with numbers like 123 and 456.',
regexPattern: r'\d+', // 高亮所有数字
highlightColor: Colors.orange,
textStyle: TextStyle(fontSize: 16, color: Colors.black),
),
6. 自定义高亮样式
你可以通过 highlightStyle
参数来自定义高亮文本的样式:
ColorTextHighlighter(
text: 'Custom highlight style example.',
wordsToHighlight: ['highlight'],
highlightColor: Colors.green,
textStyle: TextStyle(fontSize: 16, color: Colors.black),
highlightStyle: TextStyle(fontWeight: FontWeight.bold, color: Colors.red),
),