gpt4 book ai didi

dart - Flutter - InputDecoration 边框仅在聚焦时

转载 作者:IT老高 更新时间:2023-10-28 12:44:08 25 4
gpt4 key购买 nike

我正在尝试设计一个自定义 TextFormField 并且一切正常,除了我只需要在 TextFormField 聚焦时显示边框(有人已经点击它)。

由于我不认为这是可能的,所以我尝试更改边框的颜色,但在我看来,这种颜色只能通过主题的 hintColor 设置。但是由于 hintColor 也改变了正在显示的提示文本的颜色,所以我不能使用它。

final theme = Theme.of(context);

return new Theme(
data: theme.copyWith(primaryColor: Colors.blue),
child: TextFormField(
autocorrect: false,
style: TextStyle(color: Colors.green),
decoration: new InputDecoration(
fillColor: Colors.white,
filled: true,
contentPadding:
EdgeInsets.only(bottom: 10.0, left: 10.0, right: 10.0),
labelText: title,
),
validator: this.validator,
onSaved: (String newValue) {
setMethod(newValue);
},
),
);

有人知道如何解决这个问题吗?

最佳答案

有一个名为focusedBorder的属性,你可以根据自己的需要使用和改变它,也可以设置默认边框为InputBorder.none,例如:

  @override
Widget build(BuildContext context) {
return TextFormField(
autocorrect: false,
focusNode: _focusNode,
style: TextStyle(color: Colors.green),
decoration: new InputDecoration(
fillColor: Colors.white,
border: InputBorder.none,
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(5.0)),
borderSide: BorderSide(color: Colors.blue)),
filled: true,
contentPadding:
EdgeInsets.only(bottom: 10.0, left: 10.0, right: 10.0),
labelText: widget.title,
),
validator: widget.validator,
onSaved: (String newValue) {},
);
}

如果您没有 focusBorder 属性,请更新

    class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Center(
child: Column(
children: <Widget>[
MyCustomTextField(
title: "Testing 1",
),
MyCustomTextField(
title: "Testing 2",
)
],
));
}
}

class MyCustomTextField extends StatefulWidget {
final String title;
final ValueChanged<String> validator;

MyCustomTextField({this.title, this.validator});

@override
_MyCustomTextFieldState createState() => _MyCustomTextFieldState();
}

class _MyCustomTextFieldState extends State<MyCustomTextField> {
var _focusNode = new FocusNode();

_focusListener() {
setState(() {});
}

@override
void initState() {
_focusNode.addListener(_focusListener);
super.initState();
}

@override
void dispose() {
_focusNode.removeListener(_focusListener);
super.dispose();
}

@override
Widget build(BuildContext context) {
return TextFormField(
autocorrect: false,
focusNode: _focusNode,
style: TextStyle(color: Colors.green),
decoration: new InputDecoration(
fillColor: Colors.white,
border: _focusNode.hasFocus
? OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(5.0)),
borderSide: BorderSide(color: Colors.blue))
: InputBorder.none,
filled: true,
contentPadding: EdgeInsets.only(bottom: 10.0, left: 10.0, right: 10.0),
labelText: widget.title,
),
validator: widget.validator,
onSaved: (String newValue) {},
);
}
}

关于dart - Flutter - InputDecoration 边框仅在聚焦时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51810420/

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