Home > other >  Flutter Visibility widget not working third time
Flutter Visibility widget not working third time

Time:11-12

I have wrapped ListView.builder inside Visible widget, and the button for its visible property is in a ListTile widget with variable _currencyVisible. The widget Visible works 2 times i.e. false/hidden(default), then changes to visible when clicked, and again hides on the second click, but it doesn't work after that. Printing on console _currencyVisible shows correct data.

Here's my code:

menuItems(BuildContext context) {
  bool _currencyVisible = false;

return StatefulBuilder(
    builder: (BuildContext context, void Function(void Function()) setState) {
      return ListView(
        children: [
          ListTile(
            title: FutureBuilder<dynamic>(
                future: getUserCurrencySymbol(),
                builder:(BuildContext context, AsyncSnapshot<dynamic> snapshot) {
                    return Text("Currency "   snapshot.data.toString());
                }),
            trailing: IconButton(
              icon: Icon(Icons.refresh),
              onPressed: () { setState(() { _currencyVisible = !_currencyVisible; }); },
            ),
          ),

          Visibility(
            visible: _currencyVisible,
            child: ListView.builder(
              shrinkWrap: true,
              itemCount:
                  currency.allCurrenciesList.length,
              itemBuilder: (context, index) {
                for (Currency c in currency.allCurrenciesList) {
                  currency.allCurrenciesList.removeAt(0);
                  return Card(
                    child: ListTile(
                      title: Text(c.country),
                      subtitle: Text(c.shortCurrency),
                      trailing: Text(c.symbol),
                      onTap: () {
                        saveUserCurrency(c.country, context);
                      },
                    ),
                  );
                }
                return Text("Not Null");
              },
            ),
          ),
        ],
      );
    },
  );
}

CodePudding user response:

You are removing all of the data from your currency list. The widget is showing correctly, but there is no data to display.

Remove this line

currency.allCurrenciesList.removeAt(0);

Don't loop through the currencies in itemBuilder. Use index instead.

Visibility(
  visible: _currencyVisible,
  child: ListView.builder(
    shrinkWrap: true,
    itemCount: currency.allCurrenciesList.length,
    itemBuilder: (context, index) {
      final c = currency.allCurrenciesList[index];
      return Card(
        child: ListTile(
        title: Text(.country),
        subtitle: Text(c.shortCurrency),
        trailing: Text(c.symbol),
        onTap: () {
          saveUserCurrency(c.country, context);
        },
      );
    }
    return Text("Not Null");
  ),
),
  • Related