import 'package:flutter/material.dart';
import 'package:rich_text_controller/rich_text_controller.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: RichTextExample(),
);
}
}
class RichTextExample extends StatelessWidget {
RichTextExample({super.key});
final controller = RichTextController(
targetMatches: [
MatchTargetItem(
style: TextStyle(color: Colors.blue, fontWeight: FontWeight.bold),
regex: RegExp(r'#[a-zA-Z0-9_]+'), // Doesn't work
),
MatchTargetItem(
style: TextStyle(color: Colors.green),
regex: RegExp(r'@[a-zA-Z0-9_]+'), // Doesn't work
),
MatchTargetItem(
style: TextStyle(color: Colors.red),
regex: RegExp(
r'(foo|bar)+:'), // Used to highlight right after ':' character is typed in version 1.x.x, now you need to type another character for it to highlight
),
],
onMatch: (matches) {
print('Matched: $matches');
},
);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: TextField(
controller: controller,
),
),
);
}
}