kumar
kumar

Reputation: 111

Listview with Checkbox using StatefulWidget(setState)

I am trying to develop an app in flutter, that has topics that the user can select and check box state will change when i scroll on listview check box state will not collapse and finally user give the submit the value are will bring out.i tried i am not able do that.

the error message shows: The method 'setState' isn't defined for the class 'ItemDepletionList'. Try correcting the name to the name of an existing method, or defining a method named 'setState'

class ItemDepletion extends StatefulWidget {
  @override
  _GetShaftsState createState() => _GetShaftsState();

}

class _GetShaftsState extends State<ItemDepletion> {
  ItemDepletionBloc _bloc;
  String json =
      '{"RECORD_ID": "0", "REQTYPE": "ITEMDEPELTION", "CLINIC_ID": "1012"}';

  @override
  void initState() {
    super.initState();
    _bloc = ItemDepletionBloc(json);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0.0,
        automaticallyImplyLeading: false,
        title: Text('Chucky Categories',
            style: TextStyle(color: Colors.white, fontSize: 20)),
        backgroundColor: Color(0xFF333333),
      ),
      backgroundColor: Color(0xFF333333),
      body: RefreshIndicator(
        onRefresh: () => _bloc.fetchCategories(json),
        child: StreamBuilder<Response<List<Idepeltion>>>(
          stream: _bloc.chuckListStream,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              switch (snapshot.data.status) {
                case Status.LOADING:
                  return Loading(loadingMessage: snapshot.data.message);
                  break;
                case Status.COMPLETED:
                  return ItemDepletionList(
                      itemdepletionlst: snapshot.data.data);
                  break;
                case Status.ERROR:
                  return Error(
                    errorMessage: snapshot.data.message,
                    onRetryPressed: () => _bloc.fetchCategories(json),
                  );
                  break;
              }
            }
            return Container();
          },
        ),
      ),
    );
  }

  @override
  void dispose() {
    _bloc.dispose();
    super.dispose();
  }
}



class ItemDepletionList extends StatelessWidget {
  // final Itemdepeltion categoryList;
  final List<Idepeltion> itemdepletionlst;
  const ItemDepletionList({Key key, this.itemdepletionlst}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new Myappbar(title: new Text("Home Page")),
        body: Column(children: [
          Expanded(
            child: ListView.builder(
              itemCount: itemdepletionlst.length,
              itemBuilder: (context, index) {
                return ListTile(
                    title: new Container(
                        child: Row(
                  children: <Widget>[
                    new Checkbox(
                        value: itemdepletionlst[index].isCheck,
                        onChanged: (bool value) {
                         setState(() {
                            itemdepletionlst[index].isCheck = value;
                          });
                        }),
                    new Expanded(
                      child: new Container(
                        padding: new EdgeInsets.only(left: 8.0, right: 8.0),
                        child: new Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: <Widget>[
                            new Text(
                              '${itemdepletionlst[index].itemName}',
                              style: new TextStyle(
                                color: Colors.black,
                                fontWeight: FontWeight.w600,
                                fontSize: 16.0,
                              ),
                            ),
                            new Text(
                              '${itemdepletionlst[index].category}',
                              style: new TextStyle(color: Colors.grey),
                            ),
                          ],
                        ),
                      ),
                    ),
                    new Expanded(
                        child: GestureDetector(
                      onTap: () {
                        selectedItem(
                            context, itemdepletionlst[index].suggQtyUnit);
                      },
                      child: new Container(
                        padding: new EdgeInsets.only(left: 8.0, right: 8.0),
                        child: new Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: <Widget>[
                            new Text(
                              '${itemdepletionlst[index].suggReorderQty} ${itemdepletionlst[index].suggQtyUnit}',
                              style: new TextStyle(
                                color: Colors.black,
                                fontWeight: FontWeight.w600,
                                fontSize: 16.0,
                              ),
                            ),
                            new Text(
                              '${itemdepletionlst[index].manuf}',
                              style: new TextStyle(color: Colors.grey),
                            ),
                          ],
                        ),
                      ),
                    )),
                  ],
                )));
              },
            ),
          ),
          RaisedButton(
           // onPressed: getCheckboxItems,
            textColor: Colors.white,
            padding: const EdgeInsets.all(0.0),
            child: Container(
              decoration: const BoxDecoration(
                gradient: LinearGradient(
                  colors: <Color>[
                    Color(0xFF09a3c8),
                    Color(0xFF39B9B4),
                    Color(0xFF0fb188),
                  ],
                ),
              ),
              padding: const EdgeInsets.all(10.0),
              child: const Text('Submit',
                  style: TextStyle(fontSize: 20, color: Colors.white)),
            ),
          ),
        ])

        );
  }

}

Upvotes: 0

Views: 149

Answers (1)

Viren V Varasadiya
Viren V Varasadiya

Reputation: 27137

Your ItemDepletionList class is stateless and You are trying to call setstate in it because of that you are getting that error. make it Stateful then it will work.

replace Following line.

class ItemDepletionList extends StatelessWidget {

With this

class ItemDepletionList extends StatefulWidget {
  final List<Idepeltion> itemdepletionlst;
  ItemDepletionList({this.itemdepletionlst});
  @override
  _ItemDepletionListState createState() => _ItemDepletionListState();
}

class _ItemDepletionListState extends State<ItemDepletionList> {

And now to access itemdepletionlst you have use widget.

 widget.itemdepletionlst

Upvotes: 1

Related Questions