英文:
How to use MaterialStateTextStyle?
问题
Flutter已经放弃了它的易用性,尤其是对于TextButtons。
我想要一个透明的TextButton,文本颜色为红色。我该如何在这里实现,你能解释一下为什么以及如何在这里使用MaterialStateTextStyle
吗?
TextButton.icon(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(Colors.transparent),
textStyle: MaterialStateTextStyle(color: Colors.red) // 错误
),
onPressed: () {},
icon: Icon(Icons.search),
label: Text('Search'),
);
英文:
Flutter has abandonded its easy applicability, in particular for TextButtons.
I would like to have a transparent TextButton with a red text color. How do I implement that here and could you explain, why and how you use MaterialStateTextStyle
here?
TextButton.icon(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(Colors.transparent),
textStyle: MaterialStateTextStyle(color : Colors.red) // wrong
),
onPressed: () {},
icon: Icon(Icons.search),
label: Text('Search'),
);
答案1
得分: 2
这段代码可以用于创建一个具有红色文字颜色的透明 TextButton。
TextButton.icon(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(Colors.transparent),
foregroundColor: MaterialStateProperty.all<Color>(Colors.red),
),
onPressed: () {},
icon: const Icon(Icons.search),
label: const Text('Search'),
);
英文:
This code can be used to make a transparent TextButton with red text color.
TextButton.icon(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(Colors.transparent),
foregroundColor: MaterialStateProperty.all<Color>(Colors.red),
),
onPressed: () {},
icon: const Icon(Icons.search),
label: const Text('Search'),
);
答案2
得分: 2
希望这有所帮助
TextButton(
onPressed: () {
// 处理按钮点击事件
},
style: ButtonStyle(
foregroundColor: MaterialStateColor.resolveWith(
(states) => Colors.red),
overlayColor: MaterialStateColor.resolveWith(
(states) => Colors.transparent),
),
child: Text('搜索'),
),
英文:
I hope this helps
TextButton(
onPressed: () {
// Handle button press
},
style: ButtonStyle(
foregroundColor: MaterialStateColor.resolveWith(
(states) => Colors.red),
overlayColor: MaterialStateColor.resolveWith(
(states) => Colors.transparent),
),
child: Text('Search'),
),
答案3
得分: 2
你可以使用 Text 和 Icon 的参数来添加颜色,就像这样:
TextButton.icon(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.transparent),
),
onPressed: () {},
icon: Icon(
Icons.search,
color: Colors.red,
),
label: Text(
'Search',
style: TextStyle(color: Colors.red),
),
);
英文:
You can add color using parameter in Text and Icon like this:
TextButton.icon(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.transparent),
),
onPressed: () {},
icon: Icon(
Icons.search,
color: Colors.red,
),
label: Text(
'Search',
style: TextStyle(color: Colors.red),
),
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论