问题
Is there any way to change color on particular word in a string ?
Text("some long string")
now i want to give color to only long word.
can someone tell me how can i do this programatically ?
eg:-
I am long a really long and long string in some variable, a long one
now here i want to seperate long word. I can seperate simple string to highlight one word but not sure how to find and highlight each of these words.
回答1:
Wrap the word in a TextSpan and assign style
properties to change the text appearance and use RichText instead of Text
RichText( text: TextSpan( text: 'Hello ', style: DefaultTextStyle.of(context).style, children: <TextSpan>[ TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)), TextSpan(text: ' world!'), ], ), )
or use the Text.rich
constructor https://docs.flutter.io/flutter/widgets/Text-class.html
const Text.rich( TextSpan( text: 'Hello', // default text style children: <TextSpan>[ TextSpan(text: ' beautiful ', style: TextStyle(fontStyle: FontStyle.italic)), TextSpan(text: 'world', style: TextStyle(fontWeight: FontWeight.bold)), ], ), )
回答2:
Here is my code.
class HighlightText extends StatelessWidget {
final String text;
final String highlight;
final TextStyle style;
final Color highlightColor;
const HighlightText({
Key key,
this.text,
this.highlight,
this.style,
this.highlightColor,
}) : super(key: key);
@override
Widget build(BuildContext context) {
String text = this.text ?? '';
if ((highlight?.isEmpty ?? true) || text.isEmpty) {
return Text(text, style: style);
}
List<TextSpan> spans = [];
int start = 0;
int indexOfHighlight;
do {
indexOfHighlight = text.indexOf(highlight, start);
if (indexOfHighlight < 0) {
// no highlight
spans.add(_normalSpan(text.substring(start, text.length)));
break;
}
if (indexOfHighlight == start) {
// start with highlight.
spans.add(_highlightSpan(highlight));
start += highlight.length;
} else {
// normal + highlight
spans.add(_normalSpan(text.substring(start, indexOfHighlight)));
spans.add(_highlightSpan(highlight));
start = indexOfHighlight + highlight.length;
}
} while (true);
return Text.rich(TextSpan(children: spans));
}
TextSpan _highlightSpan(String content) {
return TextSpan(text: content, style: style.copyWith(color: highlightColor));
}
TextSpan _normalSpan(String content) {
return TextSpan(text: content, style: style);
}
}
回答3:
that's Good Answer @Gauter Zochbauer. if You want to change dynamically then Follow following answer.
import 'package:flutter/material.dart';
void main() => runApp(new MaterialApp(
title: 'Forms in Flutter',
home: new LoginPage(),
));
class LoginPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => new _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
Color color =Colors.yellowAccent;
@override
Widget build(BuildContext context) {
final Size screenSize = MediaQuery.of(context).size;
return new Scaffold(
appBar: new AppBar(
title: new Text('Login'),
),
body: new Container(
padding: new EdgeInsets.all(20.0),
child: Column(
children: <Widget>[
Text.rich(
TextSpan(
text: 'Hello', // default text style
children: <TextSpan>[
TextSpan(text: ' beautiful ', style: TextStyle(fontStyle: FontStyle.italic,color: color)),
TextSpan(text: 'world', style: TextStyle(fontWeight: FontWeight.bold)),
],
),
),
new RaisedButton(
onPressed: (){
setState(() {
color == Colors.yellowAccent ? color = Colors.red : color = Colors.yellowAccent;
});
},
child: Text("Click Me!!!")
),
],
)
));
}
}
来源:https://stackoverflow.com/questions/52917243/how-to-highlight-a-word-in-string-in-flutter-programmatically