How to add clear button to TextField Widget

后端 未结 12 1390
南笙
南笙 2020-12-22 22:11

Is there a proper way to add a clear button to the TextField?

Just like this picture from Material design guidelines:

What I found is

相关标签:
12条回答
  • 2020-12-22 23:00

    To add icon inside the textfield. You must have to use suffixIcon or prefixIcon inside the Input decoration.

    TextFormField(
        autofocus: false,
        obscureText: true,
        decoration: InputDecoration(
           labelText: 'Password',
           suffixIcon: Icon(
                        Icons.clear,
                        size: 20.0,
                      ),
           border: OutlineInputBorder(
           borderRadius: BorderRadius.all(Radius.circular(0.0)),
         ),
          hintText: 'Enter Password',
          contentPadding: EdgeInsets.all(10.0),
        ),
      );
    
    0 讨论(0)
  • 2020-12-22 23:03

    Try this -

    final TextEditingController _controller = new TextEditingController();
    
    new Stack(
                alignment: const Alignment(1.0, 1.0),
                children: <Widget>[
                  new TextField(controller: _controller,),
                  new FlatButton(
                      onPressed: () {
                         _controller.clear();
                      },
                      child: new Icon(Icons.clear))
                ]
            )
    
    0 讨论(0)
  • 2020-12-22 23:05
            Container(
                margin: EdgeInsets.only(left: 16.0),
                child: TextFormField(
                  controller: _username,
                  decoration: InputDecoration(
                      hintText: '请输入工号',
                      filled: true,
                      prefixIcon: Icon(
                        Icons.account_box,
                        size: 28.0,
                      ),
                      suffixIcon: IconButton(
                          icon: Icon(Icons.remove),
                          onPressed: () {
                            debugPrint('222');
                          })),
                ),
              ),
    

    use iconButton

    0 讨论(0)
  • 2020-12-22 23:09

    Search TextField with icon and clear button

    import 'package:flutter/material.dart';
    
      class SearchTextField extends StatefulWidget{
        @override
        State<StatefulWidget> createState() {
          // TODO: implement createState
          return new SearchTextFieldState();
        }
      }
    
      class SearchTextFieldState extends State<SearchTextField>{
        final TextEditingController _textController = new TextEditingController();
    
        @override
        Widget build(BuildContext context) {
          // TODO: implement build
          return new Row(children: <Widget>[
            new Icon(Icons.search, color: _textController.text.length>0?Colors.lightBlueAccent:Colors.grey,),
            new SizedBox(width: 10.0,),
            new Expanded(child: new Stack(
                alignment: const Alignment(1.0, 1.0),
                children: <Widget>[
                  new TextField(
                    decoration: InputDecoration(hintText: 'Search'),
                    onChanged: (text){
                      setState(() {
                        print(text);
                      });
                    },
                    controller: _textController,),
                  _textController.text.length>0?new IconButton(icon: new Icon(Icons.clear), onPressed: () {
                    setState(() {
                      _textController.clear();
                    });
                  }):new Container(height: 0.0,)
                ]
            ),),
          ],);
        }
      }
    

    0 讨论(0)
  • 2020-12-22 23:13

    Here’s another answer expanding a bit on @Vilokan Lab’s answer, which wasn’t really doing it for me since FlatButton has a minimum width of 88.0, and thus the clear button was not appearing right-aligned with the TextField at all.

    So I went ahead and made my own button class, and applied that using a Stack, here is my process:

    Button class:

    class CircleIconButton extends StatelessWidget {
    final double size;
    final Function onPressed;
    final IconData icon;
    
    CircleIconButton({this.size = 30.0, this.icon = Icons.clear, this.onPressed});
    
    @override
    Widget build(BuildContext context) {
      return InkWell(
        onTap: this.onPressed,
        child: SizedBox(
            width: size,
            height: size,
            child: Stack(
              alignment: Alignment(0.0, 0.0), // all centered
              children: <Widget>[
                Container(
                  width: size,
                  height: size,
                  decoration: BoxDecoration(
                      shape: BoxShape.circle, color: Colors.grey[300]),
                ),
                Icon(
                  icon,
                  size: size * 0.6, // 60% width for icon
                )
              ],
            )));
      }
    }
    

    Then apply like so as InputDecoration to your TextField:

    var myTextField = TextField(
      controller: _textController,
      decoration: InputDecoration(
          hintText: "Caption",
          suffixIcon: CircleIconButton(
            onPressed: () {
              this.setState(() {
                _textController.clear();
              });
            },
          )),
      },
    );
    

    To get this:

    Unhighlighted state

    Highlighted / selected state.

    Note this colouring comes free when you use suffixIcon.


    Note you can also Stack it in your TextField like this, but you won't get the auto-colouring you get when you use suffixIcon:

    var myTextFieldView = Stack(
      alignment: Alignment(1.0,0.0), // right & center
      children: <Widget>[
        TextField(
          controller: _textController,
          decoration: InputDecoration(hintText: "Caption"),
        ),
        Positioned(
          child: CircleIconButton(
            onPressed: () {
              this.setState(() {
                _textController.clear();
              });
            },
          ),
        ),
      ],
    );
    
    0 讨论(0)
  • 2020-12-22 23:14
    TextFormField(
                      controller:_controller
                      decoration: InputDecoration(
                        suffixIcon: IconButton(
                          onPressed: (){
                            _controller.clear();
                          },
                          icon: Icon(
                          Icons.keyboard,
                          color: Colors.blue,
                        ),
                        ),
    
                      ),
                    )
    
    0 讨论(0)
提交回复
热议问题