Files
fcs/lib/pages/test_list.dart
2020-09-04 15:30:10 +06:30

158 lines
4.7 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:fcs/model/test_model.dart';
import 'package:fcs/fcs/common/helpers/theme.dart';
import 'package:fcs/vo/popup_menu.dart';
class TestList extends StatefulWidget {
@override
_TestListState createState() => _TestListState();
}
class _TestListState extends State<TestList> {
bool isLoading = false;
@override
void initState() {
super.initState();
var testModel = Provider.of<TestModel>(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<TestModel>(context);
return Scaffold(
appBar: AppBar(
backgroundColor: primaryColor,
title: Text("Tests"),
actions: <Widget>[
PopupMenuButton<PopupMenu>(
elevation: 3.2,
onSelected: (s) {
if (s.index == 0) Provider.of<TestModel>(context).populate();
if (s.index == 1) Provider.of<TestModel>(context).add();
if (s.index == 2) Provider.of<TestModel>(context).update();
if (s.index == 3) Provider.of<TestModel>(context).remove();
},
itemBuilder: (BuildContext context) {
return [
PopupMenuItem<PopupMenu>(
value: PopupMenu(status: "Populate", index: 0),
child: Text("Populate"),
),
PopupMenuItem<PopupMenu>(
value: PopupMenu(status: "Add", index: 1),
child: Text("Add"),
),
PopupMenuItem<PopupMenu>(
value: PopupMenu(status: "Update", index: 2),
child: Text("Update"),
),
PopupMenuItem<PopupMenu>(
value: PopupMenu(status: "Delete", index: 3),
child: Text("Delete"),
)
];
})
],
),
body: Column(
children: <Widget>[
Expanded(
child: NotificationListener<ScrollNotification>(
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: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Icon(
Icons.account_circle,
color: primaryColor,
size: 55,
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
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;
});
});
}
}