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'; import '../theme/theme.dart'; import 'report_user_editor.dart'; import 'util.dart'; class ReportUserList extends StatefulWidget { final Report report; const ReportUserList({Key key, this.report}) : super(key: key); @override _ReportUserListState createState() => _ReportUserListState(); } class _ReportUserListState extends State { Report _report = new Report(); final double dotSize = 15.0; bool _isLoading = false; List _users = []; bool isForAllUsers = false; @override void initState() { super.initState(); if (widget.report != null) { this._report = widget.report; var reportUserModel = Provider.of(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: [ 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(context); await reportUserModel.updateReportForAllUsers(this._report); } catch (e) { showMsgDialog(context, "Error", e.toString()); } finally { setState(() { _isLoading = false; }); } }), Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ 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 _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: [ allUserBox, Container( padding: EdgeInsets.only(left: 10, right: 10), child: Column( children: _getUserRow(context), ), ), SizedBox(height: 15) ], ), ), ); } List _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: [ Expanded( child: new Padding( padding: const EdgeInsets.symmetric(vertical: 10.0), child: new Row( children: [ 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: [ 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(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; }); } } }