【问题标题】:How can i add a divider between icon and text in TextFormField in flutter如何在 Flutter 中的 TextFormField 中的图标和文本之间添加分隔线
【发布时间】:2021-06-04 14:18:45
【问题描述】:

我在 Flutter 中使用 TextFormField。我可以在 prefixIcon 和 text 之间放置一个分隔符吗?是否有一个看起来像分隔线的图标(在任何包装中)??这是我的代码-

Container(
                color: Colors.black45,
                width: MediaQuery.of(context).size.width,
                padding: EdgeInsets.only(left: 5.0, right: 50.0, bottom: 5.0),
                child: TextFormField(
                  textAlignVertical: TextAlignVertical.bottom,
                  style: TextStyle(
                    color: Colors.white
                  ),
                  cursorColor: Color(0xFF075E54),
                  decoration: InputDecoration(
                    contentPadding: EdgeInsets.only(bottom: 13.0),
                    prefixIcon: Icon(
                      Icons.add_photo_alternate,
                      color: Colors.white,
                      size: MediaQuery.of(context).size.width*0.07,
                    ),
                    border: InputBorder.none,
                    hintText: 'Add a caption...',
                    hintStyle: TextStyle(
                      color: Colors.grey[400],
                      fontSize: MediaQuery.of(context).size.width*0.042,
                    )
                  ),
                ),
              ),

【问题讨论】:

    标签: android ios flutter dart


    【解决方案1】:

    将所需的分隔线小部件传递给输入装饰的prefix 选项,或者我相信您可以将prefixIconContainer 与boxdecoration 和一个右边框作为分隔线一起包装

    【讨论】:

      【解决方案2】:

      您可以将您的图标包装在 Container 中并为其添加边框,这是一个示例代码:

      class MyWidget extends StatelessWidget {
        @override
        Widget build(BuildContext context) {
          return Container(
            color: Colors.black45,
            width: MediaQuery.of(context).size.width,
            padding: EdgeInsets.only(left: 5.0, right: 50.0),
            child: TextFormField(
              textAlignVertical: TextAlignVertical.bottom,
              style: TextStyle(color: Colors.white),
              cursorColor: Color(0xFF075E54),
              decoration: InputDecoration(
                contentPadding: EdgeInsets.only(bottom: 13.0),
                prefixIcon: Container(
                  margin: EdgeInsets.only(right: 8),
                  decoration: BoxDecoration(
                    border: Border(right: BorderSide(color: Colors.white)),
                  ),
                  child: Icon(
                    Icons.add_photo_alternate,
                    color: Colors.white,
                    size: MediaQuery.of(context).size.width * 0.07,
                  ),
                ),
                border: InputBorder.none,
                hintText: 'Add a caption...',
                hintStyle: TextStyle(
                  color: Colors.grey[400],
                  fontSize: MediaQuery.of(context).size.width * 0.042,
                ),
              ),
            ),
          );
        }
      }
      

      Try the full code on DartPad

      截图

      【讨论】:

      • 是的,它有效,但我能做些什么来减少它的一些部分从顶部和底部也是
      • 因为它是一个Container,你可以使用它的margin属性margin: EdgeInsets.symetric(vertical: 4)
      猜你喜欢
      • 1970-01-01
      • 2021-08-10
      • 1970-01-01
      • 2019-08-29
      • 2020-04-02
      • 2020-06-06
      • 2020-08-21
      • 2021-10-05
      • 2020-01-13
      相关资源
      最近更新 更多