【问题标题】:Flutter: How to change border color in disabled TextField?Flutter:如何在禁用的 TextField 中更改边框颜色?
【发布时间】:2021-09-02 09:11:12
【问题描述】:

当禁用 Textfield 时,我无法更改边框颜色。

装饰设置

const textshowad = InputDecoration(
    labelText: 'Title',
    contentPadding: EdgeInsets.only(left: 40.0,top: 20.0,right: 20.0,bottom: 20.0),
    border: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.purple,width: 2.0),
      borderRadius: BorderRadius.all(Radius.elliptical(15, 15)),
    ),
  focusedBorder: OutlineInputBorder(
  borderSide: BorderSide(color: Colors.blue, width: 2.0),
));

文本字段

 TextFormField(
          decoration: textshowad,
        enabled: false,
        initialValue: widget.titletext,
      ),

【问题讨论】:

    标签: flutter dart flutter-layout


    【解决方案1】:

    InputDecoration 中有一个“disabledBorder”参数。

    const textshowad = InputDecoration(
        labelText: 'Title',
        contentPadding: EdgeInsets.only(left: 40.0,top: 20.0,right: 20.0,bottom: 20.0),
        border: OutlineInputBorder(
          borderSide: BorderSide(color: Colors.purple,width: 2.0),
          borderRadius: BorderRadius.all(Radius.elliptical(15, 15)),
        ),
         disabledBorder : OutlineInputBorder(
          borderSide: BorderSide(color: Colors.grey,width: 2.0),
          borderRadius: BorderRadius.all(Radius.elliptical(15, 15)),
        ),
      focusedBorder: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.blue, width: 2.0),
    ));
    

    【讨论】:

      【解决方案2】:

      您可以为此使用三元运算符。请参阅下面的代码以更好地理解。

      bool isEnabled=false;
      
      const textshowad = InputDecoration(
        labelText: 'Title',
        contentPadding: EdgeInsets.only(left: 40.0,top: 20.0,right: 20.0,bottom: 20.0),
        border: isEnabled 
          ? OutlineInputBorder(...) 
          : OutlineInputBorder(
              borderSide: BorderSide(color: Colors.purple,width: 2.0),
              borderRadius: BorderRadius.all(Radius.elliptical(15, 15)),
        ),
        focusedBorder: OutlineInputBorder(
        borderSide: BorderSide(color: Colors.blue, width: 2.0),
      ));
      
      ...
      
      TextFormField(
        decoration: textshowad,
        enabled: isEnabled,
        initialValue: widget.titletext,
      ),
      

      【讨论】:

        猜你喜欢
        • 2019-11-23
        • 1970-01-01
        • 2019-06-29
        • 2019-03-25
        • 2019-12-23
        • 1970-01-01
        • 2021-12-07
        • 2018-10-11
        • 2017-08-20
        相关资源
        最近更新 更多