GPH
GPH

Reputation: 1171

flutter setstatus but screen didn't update

Flutter app create a Widget List (wList) and show screen properly. If user presses a button it will add a divider() to the wList and update the screen by setState(). However, the screen didn't update. I think I may not understand well the logic of setState. If I update the wList and call the setState() function, I think it should update the screen. But It didn't.

        @override
          Widget build(BuildContext context) {
            return Scaffold(
                backgroundColor: Colors.white,
                appBar: AppBar(
                  title: Text('檯號: ${widget.inputTableNumber}'),
                  centerTitle: true,
                  backgroundColor: Colors.black,
                  actions: <Widget>[
                    IconButton(icon: Icon(Icons.edit), onPressed: () => _showButtons(), color: Colors.white,)
                  ],
                ),
                body: RepaintBoundary(
                    key: _renderInvoice,
                    child: Padding(
                      padding: EdgeInsets.all(15.0),
                      child: ListView(
                        children: wList,
                      ),
                    )
                )
            );
          }

      _showButtons() {
        showModalBottomSheet<void>(
            context: context,
            builder: (BuildContext context) {
              return Container(
                      color: Colors.white54,
                      height: 500.0,
                      child: GridView.count(
                        primary: false,
                        padding: const EdgeInsets.all(20.0),
                        crossAxisSpacing: 30.0,
                        mainAxisSpacing: 30.0,
                        crossAxisCount: 3,
                        children: <Widget>[
                          FloatingActionButton(
                            onPressed: () {_addPercentage(0.1);},
                            heroTag: null,
                            backgroundColor: Colors.purpleAccent,
                            child: Text('+10%', style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w500)),
                            foregroundColor: Colors.black,
                          ),

                        ],
                      )
              );
            });
      }

  _addPercentage(double d) {
    Navigator.pop(context);
    setState(() {
      wList.add(Divider(color: Colors.black,));
    });
  }

enter image description here

enter image description here

Upvotes: 0

Views: 316

Answers (1)

Ringil
Ringil

Reputation: 6537

So the reason this fails is because the standard Listview constructor expects a const children parameter. Obviously, your wList is not a const value and changes when your button is pressed.

Instead, you should use Listview.builder like this:

ListView.builder(
      itemCount: wList.length,
      itemBuilder: (context, index) {
        return wList[index];
      }
    )

Upvotes: 2

Related Questions