add structure
This commit is contained in:
276
lib/pages/user_editor.dart
Normal file
276
lib/pages/user_editor.dart
Normal file
@@ -0,0 +1,276 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
import 'package:fcs/model/main_model.dart';
|
||||
import 'package:fcs/model/user_model.dart';
|
||||
import 'package:fcs/pages/util.dart';
|
||||
import 'package:fcs/theme/theme.dart';
|
||||
import 'package:fcs/vo/role.dart';
|
||||
import 'package:fcs/vo/user.dart';
|
||||
import 'package:fcs/widget/local_text.dart';
|
||||
import 'package:fcs/widget/progress.dart';
|
||||
|
||||
class UserEditor extends StatefulWidget {
|
||||
final User user;
|
||||
final bool viewOnly;
|
||||
const UserEditor({this.user, this.viewOnly = false});
|
||||
@override
|
||||
_UserEditorState createState() => _UserEditorState();
|
||||
}
|
||||
|
||||
class _UserEditorState extends State<UserEditor> {
|
||||
TextEditingController _name = new TextEditingController();
|
||||
TextEditingController _phone = new TextEditingController();
|
||||
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
bool _isLoading = false;
|
||||
String currentLevelId;
|
||||
List<Privilege> privileges = [];
|
||||
User _user = new User();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
var userModel = Provider.of<UserModel>(context, listen: false);
|
||||
|
||||
privileges = Provider.of<UserModel>(context, listen: false).getPrivileges;
|
||||
if (widget.user != null) {
|
||||
userModel.getUserList().forEach((u) {
|
||||
if (widget.user.docID == u.docID) {
|
||||
_user = u;
|
||||
}
|
||||
});
|
||||
_name.text = _user.name;
|
||||
_phone.text = _user.phone;
|
||||
this.currentLevelId = _user.userLevelID;
|
||||
privileges.forEach((p) => _user.privilegeIds.contains(p.id)
|
||||
? p.isChecked = true
|
||||
: p.isChecked = false);
|
||||
}
|
||||
}
|
||||
|
||||
Widget showUserLevelList(BuildContext context, UserModel userModel) {
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.max,
|
||||
children: <Widget>[
|
||||
Icon(
|
||||
Icons.security,
|
||||
color: primaryColor,
|
||||
),
|
||||
SizedBox(
|
||||
width: 20,
|
||||
),
|
||||
new Flexible(
|
||||
child: Container(
|
||||
width: 150.0,
|
||||
child: DropdownButton<String>(
|
||||
value: currentLevelId,
|
||||
isExpanded: true,
|
||||
hint: Text(
|
||||
'Select User Level',
|
||||
),
|
||||
onChanged: changedDropDownItem,
|
||||
items: userModel.userLevels
|
||||
.map<DropdownMenuItem<String>>((UserLevel userLevel) {
|
||||
return new DropdownMenuItem<String>(
|
||||
value: userLevel.id,
|
||||
child: new Text(userLevel.name,
|
||||
style:
|
||||
new TextStyle(color: Colors.black87, fontSize: 17)),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
void changedDropDownItem(selected) {
|
||||
setState(() {
|
||||
currentLevelId = selected;
|
||||
});
|
||||
}
|
||||
|
||||
Widget showprivilegeList(BuildContext context, UserModel userModel) {
|
||||
return Container(
|
||||
width: 300,
|
||||
height: 300,
|
||||
child: ListView.builder(
|
||||
itemCount: privileges.length,
|
||||
itemBuilder: (BuildContext context, int index) {
|
||||
return new ListTile(
|
||||
title: new Row(
|
||||
children: <Widget>[
|
||||
new Checkbox(
|
||||
value: privileges[index].isChecked == null
|
||||
? false
|
||||
: privileges[index].isChecked,
|
||||
activeColor: primaryColor,
|
||||
onChanged: (bool value) {
|
||||
setState(() {
|
||||
privileges[index].isChecked = value;
|
||||
});
|
||||
}),
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
new Text(
|
||||
userModel.getPrivileges[index].name,
|
||||
style: TextStyle(
|
||||
fontSize: 15.0,
|
||||
),
|
||||
),
|
||||
Container(
|
||||
width: MediaQuery.of(context).size.width * 0.5,
|
||||
child: new Text(
|
||||
userModel.getPrivileges[index].desc,
|
||||
style:
|
||||
TextStyle(fontSize: 12.0, color: Colors.grey[600]),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
));
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var userModel = Provider.of<UserModel>(context);
|
||||
|
||||
final namebox = TextFormField(
|
||||
controller: _name,
|
||||
autofocus: false,
|
||||
readOnly: true,
|
||||
cursorColor: primaryColor,
|
||||
decoration: new InputDecoration(
|
||||
border: InputBorder.none,
|
||||
focusedBorder: InputBorder.none,
|
||||
icon: Icon(
|
||||
Icons.person,
|
||||
color: primaryColor,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
final phoneNoBox = TextFormField(
|
||||
controller: _phone,
|
||||
autofocus: false,
|
||||
readOnly: true,
|
||||
cursorColor: primaryColor,
|
||||
decoration: new InputDecoration(
|
||||
border: InputBorder.none,
|
||||
focusedBorder: InputBorder.none,
|
||||
icon: Icon(
|
||||
Icons.phone,
|
||||
color: primaryColor,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
final saveButton = Container(
|
||||
padding: EdgeInsets.only(
|
||||
left: 24.0,
|
||||
right: 24.0,
|
||||
),
|
||||
child: Container(
|
||||
height: 45.0,
|
||||
color: primaryColor,
|
||||
child: ButtonTheme(
|
||||
minWidth: 900.0,
|
||||
height: 100.0,
|
||||
child: FlatButton(
|
||||
onPressed: () {
|
||||
_save(context);
|
||||
},
|
||||
child: LocalText(
|
||||
context,
|
||||
'user.save',
|
||||
color: Colors.white,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
return LocalProgress(
|
||||
inAsyncCall: _isLoading,
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: primaryColor,
|
||||
title: LocalText(context, 'user.title',
|
||||
color: Colors.white, fontSize: 20),
|
||||
),
|
||||
body: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
Expanded(
|
||||
child: ListView(
|
||||
shrinkWrap: true,
|
||||
padding: EdgeInsets.only(left: 24.0, right: 24.0),
|
||||
children: <Widget>[
|
||||
namebox,
|
||||
Row(
|
||||
children: <Widget>[
|
||||
Expanded(child: phoneNoBox),
|
||||
Expanded(
|
||||
child: InkWell(
|
||||
onTap: () => call(context, _phone.text),
|
||||
child: Icon(
|
||||
Icons.open_in_new,
|
||||
color: Colors.grey,
|
||||
size: 15,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
widget.viewOnly
|
||||
? Container()
|
||||
: showUserLevelList(context, userModel),
|
||||
widget.viewOnly
|
||||
? Container()
|
||||
: showprivilegeList(context, userModel),
|
||||
],
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: 15,
|
||||
),
|
||||
widget.viewOnly ? Container() : saveButton,
|
||||
SizedBox(
|
||||
height: 20,
|
||||
)
|
||||
],
|
||||
),
|
||||
)),
|
||||
);
|
||||
}
|
||||
|
||||
List<String> privilegesIDs() {
|
||||
return this.privileges.where((p) => p.isChecked).map((p) => p.id).toList();
|
||||
}
|
||||
|
||||
_save(BuildContext context) async {
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
try {
|
||||
var userModel = Provider.of<UserModel>(context);
|
||||
await userModel.addLevel(
|
||||
_phone.text, this.currentLevelId, privilegesIDs());
|
||||
Navigator.pop(context);
|
||||
} catch (e) {
|
||||
showMsgDialog(context, "Error", e.toString());
|
||||
}
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user