gpt4 book ai didi

flutter - 如何在自定义文本字段中添加OnChange

转载 作者:行者123 更新时间:2023-12-03 04:54:41 25 4
gpt4 key购买 nike

我正在尝试创建一个自定义文本字段,以便仅在一个地方即可轻松更改设计。问题是,无论何时提供,我都不知道如何在此自定义文本字段中添加onchange属性。有人可以告诉我如何做到这一点。方便,此onchange属性是可选的。下面是我的代码。

自定义文本字段:

  class CustomTextFieldOutline extends StatelessWidget {
CustomTextFieldOutline(
{@required this.label,
@required this.controller,
@required this.isObscureText,
@required this.isValid,
@required this.invalidMsg});

final String label;
final TextEditingController controller;
final bool isObscureText;
final bool isValid;
final String invalidMsg;

@override
Widget build(BuildContext context) {
return TextField(
decoration: InputDecoration(
labelText: label,
errorText: isValid ? null : invalidMsg,
errorStyle: TextStyle(color: constant.colorWhite),
labelStyle: TextStyle(color: constant.colorWhite),
errorBorder: UnderlineInputBorder(
borderSide: BorderSide(color: constant.colorWhite),
),
focusedErrorBorder: UnderlineInputBorder(
borderSide: BorderSide(color: constant.colorWhite),
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: constant.colorWhite),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: constant.colorWhite),
)),
style: TextStyle(color: constant.colorWhite),
controller: controller,
obscureText: isObscureText,
);
}
}

文本域:
          CustomTextFieldOutline(
label: constant.email,
controller: _emailSignUpController,
isValid: _isEmailValid,
invalidMsg: _emailValidMsg,
isObscureText: false,
)

最佳答案

您可以将函数作为参数传递到自定义文本字段中。声明一个名为Function onPressed的属性,这样您的构造函数将如下所示:

 CustomTextFieldOutline(
{@required this.label,
@required this.controller,
@required this.isObscureText,
@required this.isValid,
@required this.invalidMsg,
@required this.onPressed
});

这样,您可以通过为每个自定义文本字段定义一个自定义函数来获取自定义onChanged功能。希望能有所帮助!

关于flutter - 如何在自定义文本字段中添加OnChange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60807077/

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