我想在我的 Flutter 应用程序中禁用文本字段的标签上应用一个主题,因为我现在的灰色很难阅读。
我想将其应用到我的整个应用程序,所以我想使用主题,但是,我没有找到任何解决方案可以让我自定义标签的文本样式仅当文本表单字段被禁用时
如何在 Flutter 中主题化并全局设置禁用文本表单字段标签的颜色?
我知道如何有条件地更改标签的文本样式,但是,我需要记住始终使用相同的样式(或者我可以包装小部件,但这听起来也不是最理想的)。我可以通过以下方式自定义标签的颜色decoration
命名参数,如下所示:
TextFormField(
enabled: isEnabled,
decoration: InputDecoration(
labelText: 'Value',
labelStyle: TextStyle(color: isEnabled ? Colors.green : Colors.red),
),
// .... other fields, like controller might come here
),
你可以使用Theme https://api.flutter.dev/flutter/material/ThemeData-class.html环绕你的小部件设置属性禁用颜色.
例子:Demo https://codepen.io/vong-panha-huot/pen/WNrxBwb
final customText = Theme(
data: ThemeData(
disabledColor: Colors.blue,
),
child: TextFormField(
enabled: false,
decoration: const InputDecoration(
icon: Icon(Icons.person),
hintText: 'What do people call you?',
labelText: 'Name *',
),
),
);
或全球范围内
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
disabledColor: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)