Load more data using Stream Builder in list view

岁酱吖の 提交于 2020-06-17 14:00:27

问题


I am trying to develop an app in flutter, that has topics that the user scroll the Listview end pull the data from api and reload into listview using StreamBuilder and onRefresh its not working.i tried i am not able do that.

i implemented scroll view in _GetShaftsState class its not working where do implement? Any one can explain?

Example loading more data when list view scroll end again call service api to load the data into listview when first time load record count 30 at end of list view reach again call next 30 so total count load in list view is 60 record

how do implement dynamically add section header row in listview where do add the logic and reflected the UI ? header value based on bucket "Order in Next 10 Days" here i mention model class also(Itemdepeltion). please help me out in this project this also major part.

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

}

class _GetShaftsState extends State<ItemDepletion> {
  ItemDepletionBloc _bloc;
  String iReq;
  ScrollController controller;
  int cnt=0;

  @override
  void initState() {
    super.initState();
    ItemReq itemReq = ItemReq(0,"ITEMDEPELTION" ,"1012");
  iReq= jsonEncode(itemReq);
    _bloc = ItemDepletionBloc(iReq);
      controller = new ScrollController()..addListener(_scrollListener);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new Myappbar(title: new Text("ITEM DEPELTION")),
      backgroundColor: Color(0xFF333333),
      body: RefreshIndicator(
        onRefresh: () => _bloc.fetchCategories(iReq),
        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);
                      cnt=snapshot.data.data.length;
                  break;
                case Status.ERROR:
                  return Error(
                    errorMessage: snapshot.data.message,
                    onRetryPressed: () => _bloc.fetchCategories(iReq),
                  );
                  break;
              }
            }
            return Container();
          },
        ),
      ),
    );
  }

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

  void _scrollListener() {
    if (controller.position.pixels == controller.position.maxScrollExtent) {
      startLoader();
    }
  }
  void startLoader() {
    setState(() {  
      ItemReq itemReq = ItemReq(cnt+1,"ITEMDEPELTION" ,"1012");
  iReq= jsonEncode(itemReq);     

        _bloc = ItemDepletionBloc(iReq);
    });
  }
}




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



}
class _ItemDepletionListState extends State<ItemDepletionList> {
  final List<Idepeltion> itemdepletionlst;

  _ItemDepletionListState(this.itemdepletionlst);

 @override
  Widget build(BuildContext context) {
    return new Scaffold(       
        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)),
            ),
          ),
        ])

        );
  }


import 'dart:convert';

Itemdepeltion productFromJson(String str) => Itemdepeltion.fromJson(json.decode(str));

String productToJson(Itemdepeltion data) => json.encode(data.toJson());


class Itemdepeltion {
  String datas;
  List<Idepeltion> idepeltion;
  Itemdepeltion({
    this.datas,
    this.idepeltion,
  });

  factory Itemdepeltion.fromJson(Map<String, dynamic> json) => Itemdepeltion(
        datas: json["DATAS"],
        idepeltion: List<Idepeltion>.from(
            json["IDEPELTION"].map((x) => Idepeltion.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "DATAS": datas,
        "IDEPELTION": List<dynamic>.from(idepeltion.map((x) => x.toJson())),
      };

}

class Idepeltion {

  String itemId,itemName,category,manfCode,manuf,suggReorderDt,suggQtyUnit,bucket;
  bool isCheck;
  int clinicId;
  int itemOrdersId;
  int suggReorderQty;
  int moduleFlagInd;
  int statusInd;
  int sysSuggDiff;
  int recordId;


  Idepeltion({
    this.clinicId,
    this.itemOrdersId,
    this.itemId,
    this.itemName,
    this.category,
    this.manfCode,  
    this.manuf,
    this.suggReorderDt,
    this.suggReorderQty,
    this.suggQtyUnit,
    this.moduleFlagInd,
    this.statusInd,
    this.sysSuggDiff,
    this.bucket,
    this.recordId,
    this.isCheck,
  });

  factory Idepeltion.fromJson(Map<String, dynamic> json) => Idepeltion(
        clinicId: json["Clinic_id"],
        itemOrdersId: json["ITEM_ORDERS_ID"],
        itemId: json["ITEM_ID"],
        itemName: json["ITEM_NAME"],
        category: json["CATEGORY"],
        manfCode: json["MANF_CODE"],
        manuf: json["MANUF"],
        suggReorderDt: json["SUGG_REORDER_DT"],
        suggReorderQty: json["SUGG_REORDER_QTY"],
        suggQtyUnit: json["SUGG_QTY_UNIT"],
        moduleFlagInd: json["MODULE_FLAG_IND"],
        statusInd: json["STATUS_IND"],
        sysSuggDiff: json["Sys_Sugg_Diff"],
        bucket: json["Bucket"],
        recordId: json["RECORD_ID"],
        isCheck:false

      );

  Map<String, dynamic> toJson() => {
       "Clinic_id":clinicId,
       "ITEM_ORDERS_ID":itemOrdersId,
       "ITEM_ID":itemId,
        "ITEM_NAME":itemName,
        "CATEGORY":category,
        "MANF_CODE":manfCode,
        "MANUF":manuf,
        "SUGG_REORDER_DT":suggReorderDt,
        "SUGG_REORDER_QTY":suggReorderQty,
        "SUGG_QTY_UNIT":suggQtyUnit,
        "MODULE_FLAG_IND":moduleFlagInd,
        "STATUS_IND":statusInd,
        "Sys_Sugg_Diff":sysSuggDiff,
        "Bucket":bucket,
        "RECORD_ID":recordId
      };
}

this is sample json data

{"DATAS":"1","IDEPELTION":[{"ITEM_NAME":"TRUEMETRIX SNGL PT (50)","CATEGORY":"STRIPS, BLD GLUC","Bucket":"Order in Next 10 Days","RECORD_ID":1},{"ITEM_NAME":"SPIRETTE (50/CS)  MGM141","CATEGORY":"MOUTHPIECE","Bucket":"Order in Next 10 Days","RECORD_ID":2},{"ITEM_NAME":"TRUEMETRIX SNGL PT (50)","CATEGORY":"STRIPS, BLD GLUC","Bucket":"Order in Next 20 Days","RECORD_ID":3},{"ITEM_NAME":"SPIRETTE (50/CS)  MGM141","CATEGORY":"MOUTHPIECE","Bucket":"Order in Next 20 Days","RECORD_ID":4}]

来源:https://stackoverflow.com/questions/61675045/load-more-data-using-stream-builder-in-list-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!