【问题标题】:Listview with Checkbox using StatefulWidget(setState)Listview 和 Checkbox 使用 StatefulWidget(setState)
【发布时间】:2020-08-16 04:14:37
【问题描述】:

我正在尝试在 Flutter 中开发一个应用程序,该应用程序具有用户可以选择的主题,并且当我在列表视图上滚动时复选框状态将发生变化,复选框状态不会折叠,最后用户提交的值将显示出来.我试过我做不到。

错误信息显示: 没有为类“ItemDepletionList”定义方法“setState”。 尝试将名称更正为现有方法的名称,或定义名为“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】:

    您的 ItemDepletionList 类是 stateless 并且您正尝试在其中调用 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
    

    【讨论】:

    • 上述解决方案工作正常,但我还有一个查询在哪里实现延迟加载 ScrollController() 请帮助我实现 _GetShaftsState 类,但使用此 ItemDepletionList 加载列表视图。
    • 延迟加载是什么意思?你到底想要什么。
    • 在列表视图再次滚动结束时加载更多数据服务调用示例,当列表视图末尾第一次加载记录计数 30 时再次调用下一个 30,因此列表视图中的总计数加载为 60 条记录
    • 我不太清楚,因为我还没有实现,但我认为它应该在 _GetShaftsState 因为你的刷新指示器在那个类中,你可以在 ItemDepletionList 类中传递数据,所以你可以做出改变。
    • 你能检查一下吗?请帮帮忙stackoverflow.com/questions/61675045/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 2020-04-10
    • 2021-11-27
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    相关资源
    最近更新 更多