import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:fcs/model/language_model.dart'; import 'package:fcs/model/report_user_model.dart'; import 'package:fcs/fcs/common/helpers/theme.dart'; import 'package:fcs/vo/report.dart'; import 'package:fcs/vo/report_user.dart'; import 'package:fcs/vo/user.dart'; import 'package:fcs/widget/local_text.dart'; import 'package:fcs/widget/localization/app_translations.dart'; import 'package:fcs/widget/progress.dart'; import '../fcs/common/pages/util.dart'; typedef void FindCallBack(); class ReportUserEditor extends StatefulWidget { final Report report; const ReportUserEditor({this.report}); @override _ReportUserEditorState createState() => _ReportUserEditorState(); } class _ReportUserEditorState extends State { TextEditingController _name = new TextEditingController(); TextEditingController _searchInput = new TextEditingController(); final GlobalKey _scaffoldKey = new GlobalKey(); bool _isLoading = false; bool isSend = false; User selectedUser; List _users = []; int selectedIndex; @override void initState() { super.initState(); } Widget searchInputBox(BuildContext context, FindCallBack findCallBack) { var languageModel = Provider.of(context); return Container( padding: EdgeInsets.only(top: 10, left: 15, right: 15), child: Stack( alignment: const Alignment(1.2, 1.0), children: [ TextFormField( controller: _searchInput, autofocus: false, cursorColor: primaryColor, style: textStyle, decoration: new InputDecoration( labelText: AppTranslations.of(context).text('report.user.search'), labelStyle: languageModel.isEng ? labelStyle : labelStyleMM, enabledBorder: UnderlineInputBorder( borderSide: BorderSide(color: primaryColor, width: 1.0)), focusedBorder: UnderlineInputBorder( borderSide: BorderSide(color: primaryColor, width: 1.0)), ), ), new FlatButton( onPressed: () { findCallBack(); }, child: new Icon( Icons.search, size: 25, )) ], )); } @override Widget build(BuildContext context) { final namebox = Container( padding: EdgeInsets.only(left: 10, top: 10), child: TextFormField( controller: _name, autofocus: false, readOnly: true, cursorColor: primaryColor, decoration: new InputDecoration( border: InputBorder.none, focusedBorder: InputBorder.none, icon: Icon( Icons.person, color: primaryColor, ), ), ), ); return LocalProgress( inAsyncCall: _isLoading, child: Scaffold( key: _scaffoldKey, appBar: AppBar( backgroundColor: primaryColor, title: LocalText(context, "user.title", fontSize: 20, color: Colors.white), actions: [ IconButton( icon: Icon( Icons.save, color: Colors.white, ), onPressed: () { _save(context); }) ], ), body: Column( children: [ Expanded( child: ListView( shrinkWrap: true, children: [ searchInputBox(context, () => _findUser(context)), this.isSend ? namebox : Container(), Container( padding: EdgeInsets.only( top: this.isSend ? 10 : 20, left: 20, right: 20), child: Column( children: _getUsers(context), ), ), ], ), ), SizedBox( height: 20, ) ], )), ); } _findUser(BuildContext context) async { var reportUserModel = Provider.of(context); if (_searchInput.text == '') { showMsgDialog(context, "Error", 'Please fill the search field'); } setState(() { _isLoading = true; }); try { List users = await reportUserModel.findUser(_searchInput.text); if (users.isEmpty) return; setState(() { this._users = users; }); } catch (e) { setState(() { this.isSend = false; }); showMsgDialog(context, "Error", e.toString()); } finally { setState(() { _isLoading = false; }); } } List _getUsers(BuildContext context) { return _users.asMap().entries.map((u) { return Container( child: Card( elevation: 10, color: Colors.white, child: InkWell( onTap: () { setState(() { this.selectedUser = u.value; isSend = true; _name.text = selectedUser.name; selectedIndex = u.key; }); }, child: Row( children: [ Expanded( child: new Padding( padding: const EdgeInsets.symmetric(vertical: 5.0), child: new Row( children: [ new Padding( padding: new EdgeInsets.symmetric( horizontal: 32.0 - 15.0 / 2), child: Icon( Icons.account_circle, color: Colors.grey, size: 50, ), ), Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( u.value.name == null ? "" : u.value.name, style: new TextStyle( fontSize: 15.0, color: Colors.black), ), ], ), ], ), ), ), selectedIndex != null && selectedIndex == u.key ? Container( padding: EdgeInsets.only(right: 25), child: Icon(Icons.check)) : Container() ], ), ), )); }).toList(); } _save(BuildContext context) async { var reportUserModel = Provider.of(context); if (selectedUser == null) return; setState(() { _isLoading = true; }); reportUserModel.getUsersForReport(widget.report.id).then((users) async { if (users.any((u) => u.userID == this.selectedUser.docID)) { setState(() { _isLoading = false; }); showMsgDialog(context, "Error", 'Duplicate User'); } else { try { ReportUser _reportUser = ReportUser( userID: this.selectedUser.docID, userName: this.selectedUser.name, reportID: widget.report.id, reportName: widget.report.display); await reportUserModel.assignUser(_reportUser); reportUserModel.getUsersForReport(widget.report.id).then((users) { Navigator.pop>(context, users); }); } catch (e) { showMsgDialog(context, "Error", e.toString()); } finally { setState(() { _isLoading = false; }); } } }); } }