简体   繁体   中英

Flutter - StatefulBuilder - The declaration setState isn't referenced

I'm writing an alertDialog where the user can type a name. The alertDialog has a "OK" button and a "Annulla" button. I want the "OK" button to be disabled while the textField is empty, and then enabled when the user types something.

I'm using a statefulBuilder as recommended by some answers here on StackOverflow, but clearly my implementation is not working.

  // Function to display a dialog to insert a new item to the list
  Future<void> _displayItemAddDialog(BuildContext context, provider) async {
    String itemName;

    // clear the textField and add the item to the list
    Future<void> onOKPressed() {
      _textFieldController.clear();
      Navigator.pop(context);
      provider.addItem(itemName);
    }

    showDialog(
        context: context,
        builder: (context) {
          return StatefulBuilder(
              builder: (BuildContext context, StateSetter setState) {
            // used to check if to enable the OK button
            bool okButtonEnabled = false;

            return AlertDialog(
              title: Text('Inserisci Nome Oggetto'),
              content: TextField(
                onChanged: (value) {
                  itemName = value;
                  print(value);
                  // if the TextField is not empty then enable the button
                  if (value != "") {
                    // not working :(
                    setState() => okButtonEnabled = true;
                  }
                },
                controller: _textFieldController,
                decoration: InputDecoration(hintText: 'Nome'),
              ),
              actions: <Widget>[
                TextButton(
                  onPressed: () {
                    _textFieldController.clear();
                    Navigator.pop(context);
                  },
                  child: Text('Annulla'),
                ),
                TextButton(
                    // if button enabled then I change the assigned function
                    onPressed: okButtonEnabled ? onOKPressed : null,
                    child: Text('OK')),
              ],
            );
          });
        });
  }

You should move your okButtonEnabled outside StatefulBuilder, so right above it.

showDialog(
  context: context,
  builder: (context) {
    // Move okButtonEnabled here
    bool okButtonEnabled = false;

    return StatefulBuilder(
      builder: (BuildContext context, StateSetter setState) {
        return AlertDialog(
          title: Text('Inserisci Nome Oggetto'),
          content: TextField(
            onChanged: (value) {
              itemName = value;
              print(value);
              // if the TextField is not empty then enable the button
              if (value != "") {
                 setState(() => okButtonEnabled = true);
              }
            },
            controller: _textFieldController,
            decoration: InputDecoration(hintText: 'Nome'),
          ),
          actions: <Widget>[
            TextButton(
              onPressed: () {
                _textFieldController.clear();
                Navigator.pop(context);
              },
              child: Text('Annulla'),
            ),
            TextButton(
              // if button enabled then I change the assigned function
              onPressed: okButtonEnabled ? onOKPressed : null,
              child: Text('OK')),
          ],
        );
      },
    );
  },
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM