gpt4 book ai didi

flutter - 从 FlatButton 迁移到 TextButton 时如何指定 disabledTextColor

转载 作者:行者123 更新时间:2023-12-04 02:31:00 27 4
gpt4 key购买 nike

考虑以下指定 disabledTextColor 的代码对于 FlatButton :

FlatButton.icon(
icon: const Icon(Icons.check_box),
label: const Text('Foo'),
disabledTextColor: Colors.black,
onPressed: null,
),
我该如何翻译 disabledTextColor相当于 TextButton ?
我想我需要覆盖 style ,但我似乎无法得到 TextButton.styleFrom 工作,和 Theme.of(context).textButtonTheme.style一片空白。

最佳答案

以下是等效的:

            FlatButton.icon(
icon: const Icon(Icons.check_box),
label: const Text('FlatButton'),
disabledTextColor: Colors.black,
onPressed: null,
),
TextButton.icon(
icon: const Icon(Icons.check_box),
label: const Text('TextButton'),
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all(Colors.black),
),
onPressed: null,
),
在这种情况下,由于 onPressednull ,该按钮将始终处于禁用状态,因此 foregroundColor在所有情况下都可以简单地为黑色。但是,如果您有不同的场景, style可以做类似的事情:
style: ButtonStyle(
foregroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) => states.contains(MaterialState.disabled) ? Colors.black : null,
),
),
Migrating to the New Material Buttons and their Themes - Migrating buttons with custom disabled colors提供更多细节。

关于flutter - 从 FlatButton 迁移到 TextButton 时如何指定 disabledTextColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64182279/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com