import 'dart:async'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:fcs/model/test_model.dart'; import 'package:fcs/theme/theme.dart'; import 'package:fcs/vo/popup_menu.dart'; class TestList extends StatefulWidget { @override _TestListState createState() => _TestListState(); } class _TestListState extends State { bool isLoading = false; @override void initState() { super.initState(); var testModel = Provider.of(context, listen: false); testModel.initData(); print("List initState!"); } @override void dispose() { print("List Dispose!"); super.dispose(); } @override Widget build(BuildContext context) { var testModel = Provider.of(context); return Scaffold( appBar: AppBar( backgroundColor: primaryColor, title: Text("Tests"), actions: [ PopupMenuButton( elevation: 3.2, onSelected: (s) { if (s.index == 0) Provider.of(context).populate(); if (s.index == 1) Provider.of(context).add(); if (s.index == 2) Provider.of(context).update(); if (s.index == 3) Provider.of(context).remove(); }, itemBuilder: (BuildContext context) { return [ PopupMenuItem( value: PopupMenu(status: "Populate", index: 0), child: Text("Populate"), ), PopupMenuItem( value: PopupMenu(status: "Add", index: 1), child: Text("Add"), ), PopupMenuItem( value: PopupMenu(status: "Update", index: 2), child: Text("Update"), ), PopupMenuItem( value: PopupMenu(status: "Delete", index: 3), child: Text("Delete"), ) ]; }) ], ), body: Column( children: [ Expanded( child: NotificationListener( onNotification: (ScrollNotification scrollInfo) { if (!isLoading && scrollInfo.metrics.pixels == scrollInfo.metrics.maxScrollExtent && !testModel.ended) { // start loading data _loadData(testModel); } return null; }, child: ListView.builder( padding: EdgeInsets.only(left: 15, right: 15, top: 15), shrinkWrap: true, itemCount: testModel.ended ? testModel.tests.length + 1 : testModel.tests.length, itemBuilder: (BuildContext context, int index) { return (testModel.ended && index == testModel.tests.length) ? Center(child: Text("No more item")) : buildCard(testModel.tests[index], index + 1); }), ), ), Container( height: isLoading ? 50.0 : 0, color: Colors.transparent, child: Center( child: new CircularProgressIndicator(), ), ), ], ), ); } Card buildCard(Test test, int index) { return Card( elevation: 10, color: Colors.white, child: new Padding( padding: const EdgeInsets.symmetric(vertical: 10.0), child: new Row( children: [ Padding( padding: const EdgeInsets.all(8.0), child: Icon( Icons.account_circle, color: primaryColor, size: 55, ), ), Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ new Text( "$index - ${test.name}", style: new TextStyle(fontSize: 17.0, color: Colors.black), ), new Text( test.age.toString(), style: new TextStyle(fontSize: 12.0, color: Colors.black), ), ], ), ], ), ), ); } _loadData(TestModel testModel) { setState(() { isLoading = true; }); Timer(Duration(seconds: 1), () { testModel.load(); setState(() { isLoading = false; }); }); } }