使用StatefulWidget(setState)的Listview与Checkbox。

问题描述 投票:0回答:1

我试图在flutter中开发一个应用程序,有用户可以选择的主题和复选框状态将改变,当我在列表视图上滚动时,复选框状态不会折叠,最后用户给提交的值将被带出。

错误信息显示:"setState "方法不正确。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)),
            ),
          ),
        ])

        );
  }

}
flutter flutter-layout
1个回答
1
投票

你的 项目消耗列表 类是 无国籍 而你试图调用setstate,因此你得到了这个错误。有状态的 那么它将会工作。

替换下面的行。

class ItemDepletionList extends StatelessWidget {

有了这个

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

class _ItemDepletionListState extends State<ItemDepletionList> {

现在要访问 itemdepletionlst 你必须使用小部件。

 widget.itemdepletionlst
© www.soinside.com 2019 - 2024. All rights reserved.