Files
fcs/lib/pages/report_user_list.dart

221 lines
6.4 KiB
Dart
Raw Normal View History

2020-05-29 07:45:27 +06:30
import 'package:provider/provider.dart';
import 'package:fcs/model/report_user_model.dart';
import 'package:fcs/vo/report.dart';
import 'package:fcs/vo/report_user.dart';
import 'package:fcs/widget/local_text.dart';
import 'package:flutter/material.dart';
import 'package:fcs/widget/progress.dart';
2020-09-04 15:30:10 +06:30
import '../fcs/common/helpers/theme.dart';
2020-05-29 07:45:27 +06:30
import 'report_user_editor.dart';
2020-09-04 15:30:10 +06:30
import '../fcs/common/pages/util.dart';
2020-05-29 07:45:27 +06:30
class ReportUserList extends StatefulWidget {
final Report report;
const ReportUserList({Key key, this.report}) : super(key: key);
@override
_ReportUserListState createState() => _ReportUserListState();
}
class _ReportUserListState extends State<ReportUserList> {
Report _report = new Report();
final double dotSize = 15.0;
bool _isLoading = false;
List<ReportUser> _users = [];
bool isForAllUsers = false;
@override
void initState() {
super.initState();
if (widget.report != null) {
this._report = widget.report;
var reportUserModel =
Provider.of<ReportUserModel>(context, listen: false);
reportUserModel.getUsersForReport(this._report.id).then((users) {
if (mounted) {
setState(() {
this._users = users;
});
}
});
this.isForAllUsers =
widget.report.forAllUser == null ? false : widget.report.forAllUser;
}
}
@override
Widget build(BuildContext context) {
final allUserBox = Container(
child: new ListTile(
title: new Row(
children: <Widget>[
new Checkbox(
value: isForAllUsers,
activeColor: primaryColor,
onChanged: (bool value) async {
setState(() {
_isLoading = true;
});
try {
setState(() {
this.isForAllUsers = value;
});
this._report.forAllUser = this.isForAllUsers;
var reportUserModel = Provider.of<ReportUserModel>(context);
await reportUserModel.updateReportForAllUsers(this._report);
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
});
}
}),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Text(
'All Users',
style: TextStyle(
fontSize: 15.0,
),
),
],
),
],
)));
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
appBar: AppBar(
backgroundColor: primaryColor,
title: LocalText(
context,
'report.users.title',
translationVariables: [this._report.display],
color: Colors.white,
fontSize: 18,
),
),
floatingActionButton: FloatingActionButton(
backgroundColor: primaryColor,
child: Icon(Icons.add),
onPressed: () async {
List<ReportUser> _us = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
ReportUserEditor(report: widget.report)),
);
if (_us == null) return;
setState(() {
_users.clear();
_users.addAll(_us);
});
},
),
body: ListView(
shrinkWrap: true,
children: <Widget>[
allUserBox,
Container(
padding: EdgeInsets.only(left: 10, right: 10),
child: Column(
children: _getUserRow(context),
),
),
SizedBox(height: 15)
],
),
),
);
}
List<Widget> _getUserRow(BuildContext context) {
_users.sort((a, b) => a.userName.compareTo(b.userName));
return _users.map((u) {
return Container(
child: Card(
elevation: 10,
color: Colors.white,
child: InkWell(
onTap: () {},
child: Row(
children: <Widget>[
Expanded(
child: new Padding(
padding: const EdgeInsets.symmetric(vertical: 10.0),
child: new Row(
children: <Widget>[
new Padding(
padding: new EdgeInsets.symmetric(
horizontal: 32.0 - dotSize / 2),
child: Icon(
Icons.account_circle,
color: primaryColor,
size: 50,
),
),
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
u.userName == null ? "" : u.userName,
style: new TextStyle(
fontSize: 17.0, color: Colors.black),
),
],
),
],
),
),
),
IconButton(
padding: EdgeInsets.only(right: 10),
icon: Icon(
Icons.delete,
color: Colors.grey[700],
),
onPressed: () {
showConfirmDialog(context, "report.user_delete_confirm",
() {
_delete(context, u);
});
})
],
),
),
));
}).toList();
}
void _delete(BuildContext context, ReportUser reportUser) async {
setState(() {
_isLoading = true;
});
try {
var reportUserModel = Provider.of<ReportUserModel>(context);
await reportUserModel.deleteReportUser(reportUser);
reportUserModel.getUsersForReport(widget.report.id).then((users) {
if (mounted) {
setState(() {
this._users = users;
});
}
});
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
});
}
}
}