add structure
This commit is contained in:
195
lib/pages/banks/bank_edit.dart
Normal file
195
lib/pages/banks/bank_edit.dart
Normal file
@@ -0,0 +1,195 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:fcs/model/main_model.dart';
|
||||
import 'package:fcs/pages/util.dart';
|
||||
import 'package:fcs/theme/theme.dart';
|
||||
import 'package:fcs/vo/bank_account.dart';
|
||||
import 'package:fcs/widget/local_text.dart';
|
||||
import 'package:fcs/widget/local_text_field.dart';
|
||||
import 'package:fcs/widget/progress.dart';
|
||||
|
||||
class BankEdit extends StatefulWidget {
|
||||
final BankAccount bankAccount;
|
||||
|
||||
const BankEdit({Key key, this.bankAccount}) : super(key: key);
|
||||
|
||||
@override
|
||||
_BankEditState createState() => _BankEditState();
|
||||
}
|
||||
|
||||
class _BankEditState extends State<BankEdit> {
|
||||
TextEditingController bankNameController = new TextEditingController();
|
||||
TextEditingController accountNameController = new TextEditingController();
|
||||
TextEditingController accountNumberController = new TextEditingController();
|
||||
|
||||
bool _isLoading;
|
||||
bool _isEdit;
|
||||
File image;
|
||||
BankAccount bankAccount;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_isLoading = false;
|
||||
_isEdit = widget.bankAccount != null;
|
||||
bankAccount = BankAccount();
|
||||
if (_isEdit) {
|
||||
bankAccount = widget.bankAccount;
|
||||
bankNameController.text = bankAccount.bankName;
|
||||
accountNameController.text = bankAccount.accountName;
|
||||
accountNumberController.text = bankAccount.accountNumber;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return LocalProgress(
|
||||
inAsyncCall: _isLoading,
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: LocalText(context, 'banks.edit.title',
|
||||
color: Colors.white, fontSize: 20),
|
||||
backgroundColor: primaryColor,
|
||||
actions: <Widget>[
|
||||
IconButton(
|
||||
icon: Icon(Icons.save),
|
||||
onPressed: () {
|
||||
_save();
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
icon: Icon(Icons.delete),
|
||||
onPressed: () {
|
||||
_delete();
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
body: Column(
|
||||
children: <Widget>[
|
||||
Expanded(
|
||||
child: ListView(
|
||||
shrinkWrap: true,
|
||||
padding: EdgeInsets.only(left: 24.0, right: 24.0),
|
||||
children: <Widget>[
|
||||
LocalTextField(
|
||||
labelKey: "banks.name",
|
||||
textEditingController: bankNameController),
|
||||
LocalTextField(
|
||||
labelKey: "banks.account.name",
|
||||
textEditingController: accountNameController),
|
||||
LocalTextField(
|
||||
labelKey: "banks.account.number",
|
||||
textEditingController: accountNumberController,
|
||||
textInputType: TextInputType.number,
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(18.0),
|
||||
child: Center(
|
||||
child: Stack(
|
||||
children: <Widget>[
|
||||
Container(
|
||||
width: 80,
|
||||
height: 80,
|
||||
padding: const EdgeInsets.all(2.0),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(
|
||||
color: primaryColor,
|
||||
width: 1.0,
|
||||
),
|
||||
),
|
||||
child: image == null
|
||||
? Image.network(
|
||||
_isEdit ? widget.bankAccount.bankLogo : "",
|
||||
height: 80,
|
||||
width: 80,
|
||||
)
|
||||
: Image.file(image),
|
||||
),
|
||||
Positioned(
|
||||
bottom: -10,
|
||||
right: -10,
|
||||
child: IconButton(
|
||||
color: primaryColor,
|
||||
icon: const Icon(
|
||||
Icons.edit,
|
||||
color: Colors.grey,
|
||||
),
|
||||
onPressed: () async {
|
||||
File _image = await ImagePicker.pickImage(
|
||||
source: ImageSource.gallery,
|
||||
maxWidth: 300,
|
||||
maxHeight: 300,
|
||||
imageQuality: 80);
|
||||
if (_image != null) {
|
||||
setState(() {
|
||||
this.image = _image;
|
||||
});
|
||||
}
|
||||
}))
|
||||
],
|
||||
),
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
_save() async {
|
||||
if (!_isEdit && image == null) {
|
||||
showMsgDialog(context, "Error", "Need bank logo!");
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
try {
|
||||
bankAccount.bankName = bankNameController.text;
|
||||
bankAccount.accountName = accountNameController.text;
|
||||
bankAccount.accountNumber = accountNumberController.text;
|
||||
MainModel mainModel = Provider.of<MainModel>(context, listen: false);
|
||||
if (_isEdit) {
|
||||
await mainModel.updateBankAccount(bankAccount, image);
|
||||
} else {
|
||||
await mainModel.addBankAccount(bankAccount, image);
|
||||
}
|
||||
} catch (e) {
|
||||
showMsgDialog(context, "Error", e.toString());
|
||||
return;
|
||||
} finally {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
Navigator.pop(context);
|
||||
}
|
||||
|
||||
_delete() async {
|
||||
showConfirmDialog(context, "banks.account.delete.confirmation", () async {
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
try {
|
||||
MainModel mainModel = Provider.of<MainModel>(context, listen: false);
|
||||
if (_isEdit) {
|
||||
await mainModel.deleteBankAccount(bankAccount);
|
||||
}
|
||||
} catch (e) {
|
||||
showMsgDialog(context, "Error", e.toString());
|
||||
} finally {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
Navigator.pop(context);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user