简体   繁体   English

如何在 flutter 的 TextFormField 中的图标和文本之间添加分隔线

[英]How can i add a divider between icon and text in TextFormField in flutter

I am using a TextFormField in flutter.我在 flutter 中使用 TextFormField。 Can i put a divider between prefixIcon and text in it?我可以在 prefixIcon 和 text 之间放置一个分隔符吗? Is there an icon which looks like a divider (in any package)??.是否有一个看起来像分隔线的图标(在任何包中)?? Here is my code -这是我的代码 -

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,
                    )
                  ),
                ),
              ),

Pass desirable divider widget to prefix option of input decoration, or i believe you can wrap prefixIcon with Container with boxdecoration and one right border as divider将所需的分隔器小部件传递给输入装饰的prefix选项,或者我相信您可以将prefixIcon与带有 boxdecoration 的Container和一个右边框作为分隔器包装

You wan wrap your Icon inside a Container and applying a border to it, here is a sample code:你想把你的 Icon 包装在一个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 在 DartPad 上试用完整代码

Screenshot截屏

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter:如何在代码中的每个列表项之间添加分隔符? - Flutter : How can I add divider between each List Item in my code? 如何在工具栏中的操作项之间添加分隔线 - How can I add divider between action Items in Toolbar 如何在 Flutter 的 TextFormField 中添加提示? - How to add hint in the TextFormField in Flutter? 如何在 Flutter 中的 Column 上的 Widget 之间添加垂直分隔线? - How to add a Vertical Divider between Widget on Column in Flutter? 如何将图标放在我的 textFormField/ 文本字段的左上角? - How can i put the icon on the left top in my textFormField/ textfield? 如何将图标添加到 TextFormField 下面的 errorText? - How to add icon to errorText below TextFormField? 如何在操作栏中显示图标和标题之间的分隔线 - how to show divider between icon and title in actionbar 如何在 flutter 中添加 twitter 标志作为图标? - How can I add twitter logo as icon in flutter? 如何在 Flutter 中将 TextFormField 的文本对齐到中心、垂直? - How to align text of TextFormField to center, vertical in Flutter? Flutter:KeyboardType 属性在 TextFormField 中没有按预期工作,仍然可以粘贴文本。如何更改输入类型?(颤振) - Flutter: KeyboardType attribute not working as expected in TextFormField ,still can paste text.how to change input type?(Flutter)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM