【问题标题】:Flutter - InputDecoration border only when focusedFlutter - InputDecoration 边框仅在聚焦时
【发布时间】:2018-08-12 15:51:38
【问题描述】:

我正在尝试设计一个自定义 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);
    },
  ),
);

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

【问题讨论】:

    标签: dart flutter flutter-layout


    【解决方案1】:

    有一个名为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) {},
            );
          }
        }
    

    【讨论】:

    • 首先感谢您的回答。我希望是这样的某种属性,但在我的课堂上没有focusedBorder 属性。因此,如果我将它添加到我的复制和粘贴代码中,IDE 会告诉我该参数未定义。你运行的是什么版本?我刚刚再次运行了颤振升级,但它仍然不存在。您是否在进行某种预发布?
    • ohhh ,嗯我正在使用flutter master分支
    • 啊,好的。我在 beta 分支上。 master 是否足够稳定,可以继续工作,还是经常存在阻止进一步开发的错误?因为如果没有,我可能会切换。
    • 天哪,我完全忘记了我可以简单地处理这里的状态......非常感谢:)
    猜你喜欢
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    相关资源
    最近更新 更多