如何在flutter中获取默认的InputDecorationTheme.fillColor

mnowg1ta  于 2023-04-22  发布在  Flutter
关注(0)|答案(1)|浏览(73)

在我的自定义表单字段中,我想获取应用程序的默认fillColor并在自定义控件中使用它。正确的方法是什么?以下方法返回null,即使默认TextFormField填充为灰色。

class CustomFormField extends FormField<String> {
  CustomFormField({
    String? initialText,
    FormFieldSetter<String>? onSaved,
    FormFieldValidator<String>? validator,
    AutovalidateMode? autovalidateMode,
    Key? key,
  }) : super(
          onSaved: onSaved,
          validator: validator,
          initialValue: initialText,
          autovalidateMode: autovalidateMode,
          key: key,
          builder: (FormFieldState<String> state) {
            var fillColor =
                Theme.of(state.context).inputDecorationTheme.fillColor;
            // ISSUE: fillColor is null, even though the default 
            // TextFormField fills with a shade of grey in the same app. 
            return Container(
                // with custom control UI using fillColor
                );
          },
        );
}
ljsrvy3e

ljsrvy3e1#

我通过在应用初始化时在应用级别显式设置inputDecorationTheme来实现这一点。

return MaterialApp(
  ...
  theme: ThemeData(
    ...
    inputDecorationTheme: InputDecorationTheme(
      fillColor: Colors.grey.withOpacity(0.2),
    ),
    ...
  ),
);

相关问题