Flutter文本高亮插件text_highlight的使用
Flutter文本高亮插件text_highlight的使用
text_highlight
是一个用于语法高亮显示多种编程语言的富文本小部件。目前它支持九种编程语言:C、C++、C#、Go、Java、JavaScript、Python、R 和 Swift。该小部件完全可定制,您可以使用 HighlightTheme
类创建自己的主题。默认情况下,主题为暗模式。
使用
导入包
首先,您需要导入 text_highlight
包:
import 'package:text_highlight/text_highlight.dart';
指定特定语言模式
您可以指定一种特定的语言模式来高亮显示代码。例如,对于 Python 代码:
HighlightText(
'''
# python example
print("""Hello world""")
''' ,
mode: HighlightTextModes.PYTHON,
fontSize: 15,
)
您也可以指定其他语言模式。如果传递了一个不存在的语言模式,将会抛出 HighlightTextModeNotFoundException
异常。
自动模式
自动模式允许用户决定要高亮显示哪种语言。根据输入字符串的第一行,它会自动设置语言模式。请注意,它只分析输入字符串的第一行来确定语言。如果没有任何模式匹配第一行,则将模式设置为 TEXT
。以下是一个 Python 代码的自动模式示例:
HighlightText(
'''python:
# python example
print("""Hello world""")
''' ,
mode: HighlightTextModes.AUTO,
fontSize: 15,
)
// 第一行应包含语言名称和冒号(冒号是可选的)。
自定义主题
默认情况下,只有两个主题可用:defaultDarkTheme
和 defaultLightTheme
。如果您想要创建自己的主题,可以使用 HighlightTheme
类。您可以在示例部分查看如何实现自定义主题。
示例输出
完整示例代码
以下是完整的示例代码,展示了如何在 Flutter 应用程序中使用 text_highlight
插件:
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:text_highlight/text_highlight.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Text Editor',
debugShowCheckedModeBanner: false,
theme: ThemeData.dark(),
home: Scaffold(
appBar: AppBar(
title: Center(child: Text('Text Editor')),
),
body: Center(
child: Temp(),
),
),
);
}
}
class Temp extends StatefulWidget {
Temp({Key key}) : super(key: key);
[@override](/user/override)
_Temp createState() => _Temp();
}
class _Temp extends State<Temp> {
TextEditingController _controller = TextEditingController();
String _text = '';
[@override](/user/override)
Widget build(BuildContext context) {
return ListView(
children: [
// Java 示例
Align(
alignment: Alignment.topRight,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
HighlightText('''
// java example
class Hello{
public static void main(String args[]){
System.out.println("Hello world");
}
}''' , mode: HighlightTextModes.JAVA, fontSize: 15,),
],
),
),
// C# 示例
Align(
alignment: Alignment.topLeft,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
HighlightText('''c#:
//csharp example
namespace HelloWorld
{
class Hello {
static void Main(string[] args)
{
System.Console.WriteLine("Hello World!");
}
}
}''' , mode: HighlightTextModes.AUTO, fontSize: 15,),
],
),
),
// Python 示例
Align(
alignment: Alignment.topRight,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
HighlightText('''python:
# python example
print(r"""Hello world""")''' , mode: HighlightTextModes.AUTO, fontSize: 15,),
],
),
),
// C 示例
Align(
alignment: Alignment.topLeft,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
HighlightText('''c:
//c example
#include<stdio.h>
void main(){
printf("Hello World\\n");
}
''' , mode: HighlightTextModes.AUTO, fontSize: 15,),
],
),
),
// C++ 示例
Align(
alignment: Alignment.topRight,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
HighlightText('''c++:
// c++/cpp example
#include<iostream>
using namespace std;
int main(){
cout<<"Hello World";
}''' , mode: HighlightTextModes.AUTO, fontSize: 15,),
],
),
),
// R 示例
Align(
alignment: Alignment.topLeft,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
HighlightText('''r:
# r example
print('Hello World')
''' , mode: HighlightTextModes.AUTO, fontSize: 15,),
],
),
),
// Swift 示例
Align(
alignment: Alignment.topRight,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
HighlightText('''swift:
// swift example
import Swift
var hw = "Hello World"
print(hw)''' , mode: HighlightTextModes.AUTO, fontSize: 15,),
],
),
),
// Go 示例
Align(
alignment: Alignment.topLeft,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
HighlightText('''Go:
// go example
package main
import "fmt"
func main(){
fmt.Println("Hello World");
}''' , mode: HighlightTextModes.AUTO, fontSize: 15,),
],
),
),
// JavaScript 示例
Align(
alignment: Alignment.topRight,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
HighlightText('''javascript:
//javascript example
console.log('Hello world')
''' , mode: HighlightTextModes.AUTO, fontSize: 15,),
],
),
),
// Dart 示例
Align(
alignment: Alignment.topLeft,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
HighlightText('''dart:
/*Dart example*/
main(){
print(r'Hello World');
}''' , mode: HighlightTextModes.AUTO, fontSize: 15,),
],
),
),
// 正常文本示例
Align(
alignment: Alignment.topRight,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
HighlightText( 'this is a normal text \n' , mode: HighlightTextModes.AUTO, fontSize: 15,),
],
),
),
],
);
}
}
更多关于Flutter文本高亮插件text_highlight的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
text_highlight
是一个用于 Flutter 的文本高亮插件,它允许你在文本中高亮显示特定的关键词或短语。这个插件非常适合用于搜索功能、文本突出显示等场景。
安装插件
首先,你需要在 pubspec.yaml
文件中添加 text_highlight
依赖:
dependencies:
flutter:
sdk: flutter
text_highlight: ^latest_version
然后运行 flutter pub get
来安装插件。
使用 text_highlight
1. 基本用法
import 'package:flutter/material.dart';
import 'package:text_highlight/text_highlight.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Text Highlight Example'),
),
body: Center(
child: TextHighlight(
text: 'This is a sample text to highlight certain words.',
words: ['sample', 'highlight'],
textStyle: TextStyle(fontSize: 24),
highlightStyle: TextStyle(
fontSize: 24,
color: Colors.red,
fontWeight: FontWeight.bold,
),
),
),
),
);
}
}
在这个例子中,TextHighlight
组件会在文本中高亮显示 sample
和 highlight
这两个词。
2. 自定义高亮样式
你可以通过 highlightStyle
来自定义高亮文本的样式。例如:
TextHighlight(
text: 'This is a sample text to highlight certain words.',
words: ['sample', 'highlight'],
textStyle: TextStyle(fontSize: 24),
highlightStyle: TextStyle(
fontSize: 24,
color: Colors.blue,
backgroundColor: Colors.yellow,
fontStyle: FontStyle.italic,
),
)
3. 高亮多个关键词
你可以传入多个关键词来高亮显示它们:
TextHighlight(
text: 'This is a sample text to highlight certain words.',
words: ['sample', 'highlight', 'words'],
textStyle: TextStyle(fontSize: 24),
highlightStyle: TextStyle(
fontSize: 24,
color: Colors.green,
fontWeight: FontWeight.bold,
),
)
4. 区分大小写
默认情况下,TextHighlight
是区分大小写的。如果你希望不区分大小写,可以使用 caseSensitive
参数:
TextHighlight(
text: 'This is a Sample text to Highlight certain words.',
words: ['sample', 'highlight'],
caseSensitive: false,
textStyle: TextStyle(fontSize: 24),
highlightStyle: TextStyle(
fontSize: 24,
color: Colors.purple,
fontWeight: FontWeight.bold,
),
)
5. 高亮整个单词
如果你想确保只高亮整个单词,而不是部分匹配的单词,可以使用 wholeWord
参数:
TextHighlight(
text: 'This is a sample text to highlight certain words.',
words: ['sample', 'high'],
wholeWord: true,
textStyle: TextStyle(fontSize: 24),
highlightStyle: TextStyle(
fontSize: 24,
color: Colors.orange,
fontWeight: FontWeight.bold,
),
)