import 'package:fcs/domain/entities/user.dart'; import 'package:fcs/domain/vo/privilege.dart'; import 'package:fcs/helpers/theme.dart'; import 'package:fcs/pages/staff/model/staff_model.dart'; import 'package:fcs/pages/widgets/local_app_bar.dart'; import 'package:fcs/pages/widgets/progress.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../main/util.dart'; import '../widgets/local_text.dart'; class StaffPrivilegeEditor extends StatefulWidget { final User staff; const StaffPrivilegeEditor({required this.staff}); @override _StaffPrivilegeEditorState createState() => _StaffPrivilegeEditorState(); } class _StaffPrivilegeEditorState extends State { bool _isLoading = false; late User _staff; List _privileges = []; @override void initState() { _staff = widget.staff; _privileges = Provider.of(context, listen: false).privileges; _privileges.forEach((p) => _staff.privileges.contains(p.id) ? p.isChecked = true : p.isChecked = false); super.initState(); } List showprivilegeList(BuildContext context) { return _privileges.map((p) { return new ListTile( title: InkWell( onTap: () { setState(() { p.isChecked = p.isChecked == null ? true : !p.isChecked!; }); }, child: new Row( children: [ new Checkbox( value: p.isChecked == null ? false : p.isChecked, activeColor: primaryColor, side: BorderSide(color: Colors.black38, width: 2), onChanged: (bool? value) { if (value != null) setState(() { p.isChecked = value; }); }), Padding( padding: const EdgeInsets.only(right: 8.0), child: Icon(p.iconData, size: 30, color: Colors.black38), ), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ new Text( p.name ?? "", style: TextStyle(fontSize: 15.0, color: Colors.black), ), Text(p.desc ?? "", style: TextStyle(fontSize: 13, color: labelColor)) ], ), ), ], ), )); }).toList(); } @override Widget build(BuildContext context) { final saveButton = Padding( padding: const EdgeInsets.symmetric(horizontal: 30), child: fcsButton( context, getLocalString(context, 'btn.save'), callack: _save, ), ); return LocalProgress( inAsyncCall: _isLoading, child: Scaffold( appBar: LocalAppBar( labelKey: "staff.privilege.title", backgroundColor: Colors.white, labelColor: primaryColor, arrowColor: primaryColor), body: Padding( padding: const EdgeInsets.only(left: 12.0, right: 12), child: ListView( children: [ Column( children: [ Text(_staff.name ?? "", style: TextStyle(color: Colors.black, fontSize: 18)), Text(_staff.fcsID ?? "", style: TextStyle(color: Colors.black, fontSize: 15)), ], ), const SizedBox(height: 10), LocalText(context, 'staff.privilege.title', color: Colors.black54, fontSize: 15), Column( children: showprivilegeList(context), ), saveButton, SizedBox(height: 20) ], ), ), )); } List privilegesIDs() { return this ._privileges .where((p) => p.isChecked!) .map((p) => p.id) .toList(); } _save() async { setState(() { _isLoading = true; }); StaffModel staffModel = Provider.of(context, listen: false); try { await staffModel.updatePrivileges(widget.staff.id!, privilegesIDs()); Navigator.pop(context, true); } catch (e) { showMsgDialog(context, "Error", e.toString()); } finally { setState(() { _isLoading = false; }); } } }