Flutter文本高亮插件flutter_highlight_text的使用
Flutter文本高亮插件flutter_highlight_text的使用
Flutter文本指定文字或者指定分割符高亮,高亮文字点击之后回调点击内容。
使用方法
以下是一个完整的示例代码,展示了如何使用flutter_highlight_text
插件来实现文本高亮功能。
import 'package:flutter/material.dart';
import 'package:flutter_highlight_text/flutter_highlight_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: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
String text =
'可能说起 Flutter 绘制,大家第一反应就是用 `CustomPaint` 组件,自定义 `CustomPainter` 对象来画。Flutter 中所有可以看得到的组件,比如 Text、Image、Switch、Slider 等等,追其根源都是`画出来`的,但通过查看源码可以发现,Flutter 中绝大多数组件并不是使用 `CustomPaint` 组件来画的,其实 `CustomPaint` 组件是对框架底层绘制的一层封装。这个系列便是对 Flutter 绘制的探索,通过`测试`、`调试`及`源码分析`来给出一些在绘制时`被忽略`或`从未知晓`的东西,而有些要点如果被忽略,就很可能出现问题。';
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
HighLightText(
style: TextStyle(fontSize: 14, color: Colors.black),
highLightTextStyle:
TextStyle(color: Colors.red, fontSize: 18, fontWeight: FontWeight.bold),
text: text,
highLightWords: ['Flutter', '可'],
splitString: '`',
highLightType: HighLightType.keyWords,
wordClickAction: (word) {
print("=====$word");
},
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
解释
-
导入库:
import 'package:flutter/material.dart'; import 'package:flutter_highlight_text/flutter_highlight_text.dart';
-
主应用类:
class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(title: 'Flutter Demo Home Page'), ); } }
-
主页状态类:
class MyHomePage extends StatefulWidget { MyHomePage({Key? key, required this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); }
-
主页状态实现类:
class _MyHomePageState extends State<MyHomePage> { int _counter = 0; String text = '...'; // 文本内容 void _incrementCounter() { setState(() { _counter++; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ HighLightText( style: TextStyle(fontSize: 14, color: Colors.black), highLightTextStyle: TextStyle(color: Colors.red, fontSize: 18, fontWeight: FontWeight.bold), text: text, highLightWords: ['Flutter', '可'], splitString: '`', highLightType: HighLightType.keyWords, wordClickAction: (word) { print("=====$word"); }, ), Text( '$_counter', style: Theme.of(context).textTheme.headline4, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Increment', child: Icon(Icons.add), ), ); } }
更多关于Flutter文本高亮插件flutter_highlight_text的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter文本高亮插件flutter_highlight_text的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中使用flutter_highlight_text
插件来实现文本高亮的代码示例。这个插件允许你在文本中指定关键词并进行高亮显示。
首先,确保你已经在pubspec.yaml
文件中添加了flutter_highlight_text
依赖:
dependencies:
flutter:
sdk: flutter
flutter_highlight_text: ^最新版本号 # 请替换为当前最新版本号
然后,运行flutter pub get
来安装依赖。
接下来,你可以按照以下步骤在你的Flutter应用中使用flutter_highlight_text
:
1. 导入包
在你的Dart文件中导入flutter_highlight_text
包:
import 'package:flutter/material.dart';
import 'package:flutter_highlight_text/flutter_highlight_text.dart';
2. 创建高亮文本
你可以使用HighlightText
小部件来创建高亮文本。以下是一个完整的示例,展示了如何在一个简单的Flutter应用中实现文本高亮:
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Highlight Text Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Highlight Text Example'),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: HighlightText(
text: '这是一个示例文本,其中包含一些关键词,比如Flutter、Dart和编程。',
keywordList: ['Flutter', 'Dart', '编程'],
textStyle: TextStyle(fontSize: 18),
keywordStyle: TextStyle(
fontSize: 18,
color: Colors.red,
backgroundColor: Colors.yellow.withOpacity(0.5),
decoration: TextDecoration.underline,
),
),
),
),
),
);
}
}
3. 解释代码
HighlightText
小部件接受几个参数:text
: 要显示的完整文本。keywordList
: 一个包含要高亮显示的关键词的列表。textStyle
: 用于非高亮文本的样式。keywordStyle
: 用于高亮文本的样式。
在上面的示例中,文本“这是一个示例文本,其中包含一些关键词,比如Flutter、Dart和编程。”中的“Flutter”、“Dart”和“编程”将会被高亮显示,背景色为黄色(带有0.5的不透明度),文字颜色为红色,并且带有下划线。
4. 运行应用
保存你的代码并运行Flutter应用。你应该会看到一个文本,其中指定的关键词已经被高亮显示。
这个插件非常灵活,允许你自定义高亮文本的样式,以满足你的应用需求。希望这个示例能帮助你理解如何在Flutter项目中使用flutter_highlight_text
插件。