我想在文本小部件中展示一个图标,我该怎么做?
以下代码只显示IconData。
以下代码只显示IconData。
Text("Click ${Icons.add} to add");
Text("Click ${Icons.add} to add");
Flutter有WidgetSpan()
可以在RichText()
中添加小部件。
示例用法:
RichText(
text: TextSpan(
children: [
TextSpan(
text: "Click ",
),
WidgetSpan(
child: Icon(Icons.add, size: 14),
),
TextSpan(
text: " to add",
),
],
),
)
上面的代码将会产生:
您可以像对待常规小部件一样对待WidgetSpan
的子元素。Wrap
:Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: [
Text('Click'),
Icon(Icons.add),
Text('to add'),
],
)
Row
:Row(
mainAxisSize: MainAxisSize.min,
children: [
Text('Click'),
Icon(Icons.add),
Text('to add'),
],
)
Text.rich
:Text.rich(
TextSpan(
children: [
TextSpan(text: 'Click'),
WidgetSpan(child: Icon(Icons.add)),
TextSpan(text: 'to add'),
],
),
)
Wrap
有什么问题呢?你可以提一个单独的问题吗? - CopsOnRoad另一种选择可能是使用表情符号。
在Dart中,字符串支持特殊字符的转义序列。对于Unicode表情符号,您可以使用\u和大括号,并在其中放置表情符号的十六进制代码。像这样:
Text('Click \u{2795} to add')
结果为:
你可以在这里找到完整的Unicode表情符号列表:http://unicode.org/Public/emoji/1.0/emoji-data.txt
Row Widget可以是解决此问题的一种方案,但您需要使用不同的小部件来实现它。
请按照以下示例进行操作。
Row(
children: <Widget>[
Text("Hi"),
Icon(Icons.add),
Text("Hello")
]
)
另一个选项是 String.fromCharCode(int charCode)
这是由Flutter团队创建的icons.dart
,可以在此处找到charCodes。
RichText(
text: TextSpan(
style: TextStyle(
color: Colors.black,
fontSize: 24.0,
),
text: 'This is alarm icon : ',
children: <TextSpan>[
TextSpan(
text: String.fromCharCode(0xe190), //<-- charCode
style: TextStyle(
fontFamily: 'MaterialIcons', //<-- fontFamily
fontSize: 24.0,
color: Colors.red,
),
)
],
),
)
结果:
我认为最好使用Row小部件。这是一个例子:
Row(
children: [
Icon(Icons.download_rounded),
Text(" Download")
],
)
Widget _spanIcon(String text, IconData icon, TextStyle st) {
return Row(
children: [
Text(text,style: st,),
Text(
String.fromCharCode(icon.codePoint), //<-- charCode from icon data
style: TextStyle(
fontFamily: icon.fontFamily, //<-- fontFamily from icon data
fontSize: st.fontSize! + 4.0,//you can increase font size for icon here
color: st.color,
),)
],
);}
然后使用它:
_spanIcon('11',Icons.access_alarm, TextStyle(fontSize: 14.0, color: Color.fromARGB(113, 0, 0, 0))),
您可以使用this包来实现此功能。
我找不到这个包的发布版本。
以下是实现代码。
RealRichText(
[
TextSpan(
text: "A Text Link",
style: TextStyle(color: Colors.red, fontSize: 14),
recognizer: TapGestureRecognizer()
..onTap = () {
debugPrint("Link Clicked.");
},
),
ImageSpan(
AssetImage("packages/real_rich_text/images/emoji_9.png"),
imageWidth: 24,
imageHeight: 24,
),
ImageSpan(AssetImage("packages/real_rich_text/images/emoji_10.png"),
imageWidth: 24,
imageHeight: 24,
margin: EdgeInsets.symmetric(horizontal: 10)),
TextSpan(
text: "哈哈哈",
style: TextStyle(color: Colors.yellow, fontSize: 14),
),
TextSpan(
text: "@Somebody",
style: TextStyle(
color: Colors.black, fontSize: 14, fontWeight: FontWeight.bold),
recognizer: TapGestureRecognizer()
..onTap = () {
debugPrint("Link Clicked");
},
),
TextSpan(
text: " #RealRichText# ",
style: TextStyle(color: Colors.blue, fontSize: 14),
recognizer: TapGestureRecognizer()
..onTap = () {
debugPrint("Link Clicked");
},
),
TextSpan(
text: "showing a bigger image",
style: TextStyle(color: Colors.black, fontSize: 14),
),
ImageSpan(AssetImage("packages/real_rich_text/images/emoji_10.png"),
imageWidth: 24,
imageHeight: 24,
margin: EdgeInsets.symmetric(horizontal: 5)),
TextSpan(
text: "and seems working perfect……",
style: TextStyle(color: Colors.black, fontSize: 14),
),
],
);
alignment: PlaceholderAlignment.middle,
可以使垂直居中对齐。 - kuzey beytar