English 中文(简体)
2. 彩票
原标题:Flutter text field clear button

I m trying to build a search field with a clear button who appear when i type some text in. But i tried a ternary condition to display my button, it never appear.

预 收

这里是我的班子:

class SearchField extends StatefulWidget {
    @override
    _SearchFieldState createState() => _SearchFieldState();
}

class _SearchFieldState extends State<SearchField> {
    TextEditingController _controller = TextEditingController();

    @override
    Widget build(BuildContext context) {
        return TextField(
            controller: _controller,
            style: const TextStyle(
                color: Colors.white,
            ),
            decoration: InputDecoration(
                labelText:  Search ,
                labelStyle: TextStyle(
                    color: Color.fromARGB(255, 255, 255, 255).withOpacity(0.4),
                    fontFamily:  Inter ,
                ),
                border: InputBorder.none,
                suffixIcon: _controller.text.isEmpty //my clear button
                                ? null
                                : IconButton(
                                        icon: Icon(Icons.clear),
                                        onPressed: () {
                                            setState(() {
                                                _controller.clear();
                                            });
                                        },
                                    )
            ),
            onEditingComplete: () {
                // TODO: search contact with _controller.text
                FocusScope.of(context).unfocus();
            },
        );
    }
}
问题回答

我在问问题后找到答案:

I had to add onChanged on my text like this and it s work:

class SearchField extends StatefulWidget {
  @override
  _SearchFieldState createState() => _SearchFieldState();
}

class _SearchFieldState extends State<SearchField> {
  TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return TextField(
      controller: _controller,
      style: const TextStyle(
        color: Colors.white,
      ),
      decoration: InputDecoration(
        labelText:  Search ,
        labelStyle: TextStyle(
          color: Color.fromARGB(255, 255, 255, 255).withOpacity(0.4),
          fontFamily:  Inter ,
        ),
        border: InputBorder.none,
        suffixIcon: _controller.text.isEmpty
            ? null
            : IconButton(
                icon: Icon(Icons.clear),
                onPressed: () {
                  setState(() {
                    _controller.clear();
                  });
                },
              ),
      ),
      onChanged: (text) {
        setState(() {
          // This will rebuild the widget and update the suffixIcon accordingly
        });
      },
      onEditingComplete: () {
        // TODO: search contact with _controller.text
        FocusScope.of(context).unfocus();
      },
    );
  }
}

这可以帮助你,我只增加了一个新的领域,以交出外地空洞的国家,并在<代码>Text Field<>/code>上提供。 我在Changed更新了前var,并更新了返回初始价值的明确方法。

import  package:flutter/material.dart ;

class SearchField extends StatefulWidget {
  const SearchField({super.key});

  @override
  State<SearchField> createState() => _SearchFieldState();
}

class _SearchFieldState extends State<SearchField> {
  final TextEditingController _controller = TextEditingController();

  // New var to handler whether field is empty
  bool isFieldEmpty = false;

  @override
  Widget build(BuildContext context) {
    return TextField(
      controller: _controller,
      style: const TextStyle(
        color: Colors.white,
      ),
      decoration: InputDecoration(
        labelText:  Search ,
        labelStyle: TextStyle(
          color: const Color.fromARGB(255, 255, 255, 255).withOpacity(0.4),
          fontFamily:  Inter ,
        ),
        border: InputBorder.none,
        suffixIcon: isFieldEmpty //my clear button
            ? null
            : IconButton(
                icon: const Icon(Icons.clear),
                onPressed: () {
                  _controller.clear();
                  isFieldEmpty = true;
                  setState(() {});
                },
              ),
      ),
      onEditingComplete: () {
        // TODO: search contact with _controller.text
        FocusScope.of(context).unfocus();
      },
      // Update field on every field change
      onChanged: (value) {
        isFieldEmpty = value.trim().isEmpty;
        setState(() {});
      },
    );
  }
}




相关问题
C# Building An Array with Button Entries

I have a set of buttons that I want to add into an array so that they are ordered. The buttons I have are: Monday0700Button Monday0730Button Monday0800Button Monday0830Button and so on. How do I ...

Text in disabled and css styled button shows movement in IE

I have disabled and css styled buttons. In Firefox, disabled buttons do not move at all when pressed (which is what is expected), in IE, the text in the buttons still moves a few pixels. Why does ...

Where is Button.DialogResult in WPF?

In System.Windows.Forms.Button there is a property DialogResult, where is this property in the System.Windows.Controls.Button (WPF)?

How to create a submit button template in Oracle APEX?

I m trying to create a template for a button in Oracle APEX but I don t seem to have access to the appropriate substitution strings to make it work. For non-templated buttons APEX seems to insert a ...

Removing text from HTML buttons on all browsers?

We have buttons of many sizes and colors that use background images. There is a label on the background image itself, but we need to keep the button s text in the HTML for usability/accessibility. How ...

Back button loop with IFRAMES

In my (school) website we use Iframes to display class blogs (on blogger). This works well EXCEPT if the user then clicks on (say) a photo inside the iframe. Blogger (in this case) then displays the ...

ruby on rails on click events

I am so sorry if this question seems too easy but I can t seem to find it anywhere. I am creating a ruby in steel project. I have created a html.erb and rb file in a vs project (ruby on rails). My ...

热门标签