当 Textformfield 处于非活动状态、活动状态和已填充状态时具有不同的边框颜色

发布于 2025-01-17 21:18:08 字数 1168 浏览 5 评论 0原文

当字段不活动时,当活动且不活跃时,在flutter中的每个状态都有不同的边框颜色,但在字段中具有文本

 TextFormField emailUserForm() {
return TextFormField(
  keyboardType: TextInputType.emailAddress,
  cursorColor: textBlack50Color,
  autocorrect: false,
  validator: (text) => validateEmail(text!),
  onSaved: (name) {
    _email = name!;
    print('on save called');
  },
  decoration: const  InputDecoration(
    fillColor: Colors.white,
    filled: true,
    border:  OutlineInputBorder(
        borderSide: BorderSide(color: textBlack50Color, width: 1.0),
        borderRadius: BorderRadius.all(Radius.circular(15.0))),
    labelStyle:  TextStyle(
        color: textBlack50Color,
        fontSize: 14,
        fontWeight: FontWeight.w500),
    hintStyle:  TextStyle(fontSize: 17),
    hintText: 'Your email address',
    focusedBorder:  OutlineInputBorder(
        borderSide: BorderSide(color: textBlackColor, width: 1.0),
        borderRadius: BorderRadius.all(Radius.circular(15.0))),
    enabledBorder: OutlineInputBorder(
        borderSide: BorderSide(color:  textBlackColor , width: 1.0),
        borderRadius:  BorderRadius.all(Radius.circular(15.0))),
  ),
);

}

Is is possible to have a different border color for each state of the TextFormField in flutter ie when the field is not active , when active and not active but has text in the field

 TextFormField emailUserForm() {
return TextFormField(
  keyboardType: TextInputType.emailAddress,
  cursorColor: textBlack50Color,
  autocorrect: false,
  validator: (text) => validateEmail(text!),
  onSaved: (name) {
    _email = name!;
    print('on save called');
  },
  decoration: const  InputDecoration(
    fillColor: Colors.white,
    filled: true,
    border:  OutlineInputBorder(
        borderSide: BorderSide(color: textBlack50Color, width: 1.0),
        borderRadius: BorderRadius.all(Radius.circular(15.0))),
    labelStyle:  TextStyle(
        color: textBlack50Color,
        fontSize: 14,
        fontWeight: FontWeight.w500),
    hintStyle:  TextStyle(fontSize: 17),
    hintText: 'Your email address',
    focusedBorder:  OutlineInputBorder(
        borderSide: BorderSide(color: textBlackColor, width: 1.0),
        borderRadius: BorderRadius.all(Radius.circular(15.0))),
    enabledBorder: OutlineInputBorder(
        borderSide: BorderSide(color:  textBlackColor , width: 1.0),
        borderRadius:  BorderRadius.all(Radius.circular(15.0))),
  ),
);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神回复 2025-01-24 21:18:08

是的,您应该在 InputDecoration 类中设置特定属性。根据文档,以下是不同的边框样式:

  • [focusedBorder],当[InputDecorator.isFocused]为true并且[InputDecoration.errorText]为null时显示。
  • [focusedErrorBorder],当[InputDecorator.isFocused]为true且[InputDecoration.errorText]非空时显示。
  • [disabledBorder],当[InputDecoration.enabled]为false且[InputDecoration.errorText]为null时显示。
  • [enabledBorder],当[InputDecoration.enabled]为true并且[InputDecoration.errorText]为null时显示。

代码:

InputDecoration(
  <...>
  enabledBorder: const OutlineInputBorder(
    borderSide: BorderSide(color: Colors.black),
  ),
  disabledBorder: const OutlineInputBorder(
    borderSide: BorderSide(color: Colors.grey),
  ),
  errorBorder: const OutlineInputBorder(
    borderSide: BorderSide(color: Colors.red),
  ),
  focusedBorder: const OutlineInputBorder(
    borderSide: BorderSide(color: Colors.blue),
  ),
);

Yes, you should set the specific properties inside the InputDecoration class. Based on the documentation, here are the different border styles:

  • [focusedBorder], displayed when [InputDecorator.isFocused] is true and [InputDecoration.errorText] is null.
  • [focusedErrorBorder], displayed when [InputDecorator.isFocused] is true and [InputDecoration.errorText] is non-null.
  • [disabledBorder], displayed when [InputDecoration.enabled] is false and [InputDecoration.errorText] is null.
  • [enabledBorder], displayed when [InputDecoration.enabled] is true and [InputDecoration.errorText] is null.

Code:

InputDecoration(
  <...>
  enabledBorder: const OutlineInputBorder(
    borderSide: BorderSide(color: Colors.black),
  ),
  disabledBorder: const OutlineInputBorder(
    borderSide: BorderSide(color: Colors.grey),
  ),
  errorBorder: const OutlineInputBorder(
    borderSide: BorderSide(color: Colors.red),
  ),
  focusedBorder: const OutlineInputBorder(
    borderSide: BorderSide(color: Colors.blue),
  ),
);
肤浅与狂妄 2025-01-24 21:18:08

尝试下面的代码希望对您有帮助。

TextFormField(
  keyboardType: TextInputType.emailAddress,
  cursorColor: Colors.green,
  autocorrect: false,
  onSaved: (name) {
    print('on save called');
  },
  decoration: InputDecoration(
    fillColor: Colors.white,
    filled: true,
    border: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.green, width: 5.0),
      borderRadius: BorderRadius.all(
        Radius.circular(15.0),
      ),
    ),
    labelStyle: TextStyle(
        color: Colors.brown, fontSize: 14, fontWeight: FontWeight.w500),
    hintStyle: TextStyle(fontSize: 17),
    hintText: 'Your email address',
    focusedBorder: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.purple, width: 1.0),
      borderRadius: BorderRadius.all(
        Radius.circular(15.0),
      ),
    ),
    enabledBorder: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.pink, width: 1.0),
      borderRadius: BorderRadius.all(
        Radius.circular(15.0),
      ),
    ),
    disabledBorder: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.blue, width: 1.0),
      borderRadius: BorderRadius.all(
        Radius.circular(15.0),
      ),
    ),
    errorBorder: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.red, width: 1.0),
      borderRadius: BorderRadius.all(
        Radius.circular(15.0),
      ),
    ),
  ),
),

没有对焦屏幕的结果-> image

焦点屏幕的结果 -> image

Try below code hope its help to you.

TextFormField(
  keyboardType: TextInputType.emailAddress,
  cursorColor: Colors.green,
  autocorrect: false,
  onSaved: (name) {
    print('on save called');
  },
  decoration: InputDecoration(
    fillColor: Colors.white,
    filled: true,
    border: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.green, width: 5.0),
      borderRadius: BorderRadius.all(
        Radius.circular(15.0),
      ),
    ),
    labelStyle: TextStyle(
        color: Colors.brown, fontSize: 14, fontWeight: FontWeight.w500),
    hintStyle: TextStyle(fontSize: 17),
    hintText: 'Your email address',
    focusedBorder: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.purple, width: 1.0),
      borderRadius: BorderRadius.all(
        Radius.circular(15.0),
      ),
    ),
    enabledBorder: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.pink, width: 1.0),
      borderRadius: BorderRadius.all(
        Radius.circular(15.0),
      ),
    ),
    disabledBorder: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.blue, width: 1.0),
      borderRadius: BorderRadius.all(
        Radius.circular(15.0),
      ),
    ),
    errorBorder: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.red, width: 1.0),
      borderRadius: BorderRadius.all(
        Radius.circular(15.0),
      ),
    ),
  ),
),

Your result without focus screen-> image

Your result with focus screen-> image

猫瑾少女 2025-01-24 21:18:08

您可以设置控制器,并在enabledborder属性中尝试一些内容。使用AnimatedBuilder来收听文本输入更改。

final TextEditingController _controller = TextEditingController();
final Color colorWhenFilled = Colors.amber;
...
AnimatedBuilder(
animation: _controller,
builder: (context, child) => TextFormField(
  keyboardType: TextInputType.emailAddress,
  cursorColor: textBlack50Color,
  autocorrect: false,
  validator: (text) => validateEmail(text!),
  onSaved: (name) {
    _email = name!;
    print('on save called');
  },
  decoration: const  InputDecoration(
    fillColor: Colors.white,
    filled: true,
    border:  OutlineInputBorder(
        borderSide: BorderSide(color: textBlack50Color, width: 1.0),
        borderRadius: BorderRadius.all(Radius.circular(15.0))),
    labelStyle:  TextStyle(
        color: textBlack50Color,
        fontSize: 14,
        fontWeight: FontWeight.w500),
    hintStyle:  TextStyle(fontSize: 17),
    hintText: 'Your email address',
    focusedBorder:  OutlineInputBorder(
        borderSide: BorderSide(color: textBlackColor, width: 1.0),
        borderRadius: BorderRadius.all(Radius.circular(15.0))),
    enabledBorder: OutlineInputBorder(
        borderSide: BorderSide(color: _controller.text.isNotEmpty ? colorWhenFilled : textBlackColor , width: 1.0),
        borderRadius:  BorderRadius.all(Radius.circular(15.0))),
  ),
));

You can set a controller and try something in the enabledBorder property. Use AnimatedBuilder to listen to text input changes.

final TextEditingController _controller = TextEditingController();
final Color colorWhenFilled = Colors.amber;
...
AnimatedBuilder(
animation: _controller,
builder: (context, child) => TextFormField(
  keyboardType: TextInputType.emailAddress,
  cursorColor: textBlack50Color,
  autocorrect: false,
  validator: (text) => validateEmail(text!),
  onSaved: (name) {
    _email = name!;
    print('on save called');
  },
  decoration: const  InputDecoration(
    fillColor: Colors.white,
    filled: true,
    border:  OutlineInputBorder(
        borderSide: BorderSide(color: textBlack50Color, width: 1.0),
        borderRadius: BorderRadius.all(Radius.circular(15.0))),
    labelStyle:  TextStyle(
        color: textBlack50Color,
        fontSize: 14,
        fontWeight: FontWeight.w500),
    hintStyle:  TextStyle(fontSize: 17),
    hintText: 'Your email address',
    focusedBorder:  OutlineInputBorder(
        borderSide: BorderSide(color: textBlackColor, width: 1.0),
        borderRadius: BorderRadius.all(Radius.circular(15.0))),
    enabledBorder: OutlineInputBorder(
        borderSide: BorderSide(color: _controller.text.isNotEmpty ? colorWhenFilled : textBlackColor , width: 1.0),
        borderRadius:  BorderRadius.all(Radius.circular(15.0))),
  ),
));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文