change name to delivery address
This commit is contained in:
240
lib/pages/delivery_address/delivery_address_editor.dart
Normal file
240
lib/pages/delivery_address/delivery_address_editor.dart
Normal file
@@ -0,0 +1,240 @@
|
||||
import 'package:fcs/domain/vo/delivery_address.dart';
|
||||
import 'package:fcs/helpers/theme.dart';
|
||||
import 'package:fcs/pages/delivery_address/model/delivery_address_model.dart';
|
||||
import 'package:fcs/pages/main/util.dart';
|
||||
import 'package:fcs/pages/widgets/input_text.dart';
|
||||
import 'package:fcs/pages/widgets/local_text.dart';
|
||||
import 'package:fcs/pages/widgets/progress.dart';
|
||||
import 'package:flutter_icons/flutter_icons.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class DeliveryAddressEditor extends StatefulWidget {
|
||||
final DeliveryAddress deliveryAddress;
|
||||
DeliveryAddressEditor({this.deliveryAddress});
|
||||
|
||||
@override
|
||||
_DeliveryAddressEditorState createState() => _DeliveryAddressEditorState();
|
||||
}
|
||||
|
||||
class _DeliveryAddressEditorState extends State<DeliveryAddressEditor> {
|
||||
TextEditingController _nameController = new TextEditingController();
|
||||
TextEditingController _address1Controller = new TextEditingController();
|
||||
TextEditingController _address2Controller = new TextEditingController();
|
||||
TextEditingController _cityController = new TextEditingController();
|
||||
TextEditingController _stateController = new TextEditingController();
|
||||
TextEditingController _countryController = new TextEditingController();
|
||||
TextEditingController _phoneController = new TextEditingController();
|
||||
|
||||
DeliveryAddress _deliveryAddress = new DeliveryAddress();
|
||||
|
||||
bool _isLoading = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
if (widget.deliveryAddress != null) {
|
||||
_deliveryAddress = widget.deliveryAddress;
|
||||
_nameController.text = _deliveryAddress.fullName;
|
||||
_address1Controller.text = _deliveryAddress.addressLine1;
|
||||
_address2Controller.text = _deliveryAddress.addressLine2;
|
||||
_cityController.text = _deliveryAddress.city;
|
||||
_stateController.text = _deliveryAddress.state;
|
||||
_countryController.text = _deliveryAddress.country;
|
||||
_phoneController.text = _deliveryAddress.phoneNumber;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final usaAddress = InputText(
|
||||
labelTextKey: 'delivery_address.full_name',
|
||||
iconData: Icons.text_format,
|
||||
controller: _nameController);
|
||||
|
||||
final addressLine1 = InputText(
|
||||
labelTextKey: 'delivery_address.address_line1',
|
||||
iconData: Icons.location_on,
|
||||
controller: _address1Controller);
|
||||
|
||||
final addressLine2 = InputText(
|
||||
labelTextKey: 'delivery_address.address_line2',
|
||||
iconData: Icons.location_on,
|
||||
controller: _address2Controller);
|
||||
|
||||
final cityBox = InputText(
|
||||
labelTextKey: 'delivery_address.city',
|
||||
iconData: Icons.location_city,
|
||||
controller: _cityController);
|
||||
|
||||
final regionBox = InputText(
|
||||
labelTextKey: 'delivery_address.state_region',
|
||||
iconData: Entypo.location,
|
||||
controller: _stateController);
|
||||
|
||||
final countryBox = InputText(
|
||||
labelTextKey: 'delivery_address.country',
|
||||
iconData: Entypo.flag,
|
||||
controller: _countryController);
|
||||
|
||||
final phoneNumberBox = InputText(
|
||||
labelTextKey: 'delivery_address.phonenumber',
|
||||
iconData: Icons.phone,
|
||||
controller: _phoneController);
|
||||
|
||||
final createBtn = fcsButton(
|
||||
context,
|
||||
getLocalString(context, "delivery_address.create"),
|
||||
callack: _create,
|
||||
);
|
||||
|
||||
final updateBtn = fcsButton(
|
||||
context,
|
||||
getLocalString(context, "delivery_address.update"),
|
||||
callack: _update,
|
||||
);
|
||||
|
||||
return LocalProgress(
|
||||
inAsyncCall: _isLoading,
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
centerTitle: true,
|
||||
leading: new IconButton(
|
||||
icon: new Icon(Icons.close),
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
),
|
||||
backgroundColor: primaryColor,
|
||||
title: LocalText(
|
||||
context,
|
||||
'user.form.shipping_address',
|
||||
color: Colors.white,
|
||||
fontSize: 20,
|
||||
),
|
||||
),
|
||||
body: Card(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(left: 10.0, right: 10),
|
||||
child: ListView(children: <Widget>[
|
||||
usaAddress,
|
||||
SizedBox(height: 5),
|
||||
addressLine1,
|
||||
SizedBox(height: 5),
|
||||
addressLine2,
|
||||
SizedBox(height: 5),
|
||||
cityBox,
|
||||
SizedBox(height: 5),
|
||||
regionBox,
|
||||
SizedBox(height: 5),
|
||||
countryBox,
|
||||
SizedBox(height: 5),
|
||||
phoneNumberBox,
|
||||
SizedBox(height: 10),
|
||||
]),
|
||||
)),
|
||||
widget.deliveryAddress == null ? createBtn : updateBtn,
|
||||
SizedBox(height: 10)
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
DeliveryAddress _getPayload() {
|
||||
DeliveryAddress deliveryAddress = DeliveryAddress();
|
||||
deliveryAddress.id = _deliveryAddress.id;
|
||||
|
||||
try {
|
||||
deliveryAddress.fullName = _nameController.text;
|
||||
deliveryAddress.addressLine1 = _address1Controller.text;
|
||||
deliveryAddress.addressLine2 = _address2Controller.text;
|
||||
deliveryAddress.city = _cityController.text;
|
||||
deliveryAddress.state = _stateController.text;
|
||||
deliveryAddress.country = _countryController.text;
|
||||
deliveryAddress.phoneNumber = _phoneController.text;
|
||||
} catch (e) {
|
||||
showMsgDialog(context, "Error", e.toString()); // shold never happen
|
||||
}
|
||||
return deliveryAddress;
|
||||
}
|
||||
|
||||
Future<bool> _validate(DeliveryAddress deliveryAddress) async {
|
||||
if (deliveryAddress.addressLine1 == "") {
|
||||
await showMsgDialog(context, "Error", "Invalid address line 1!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (deliveryAddress.city == null) {
|
||||
await showMsgDialog(context, "Error", "Invalid city!");
|
||||
return false;
|
||||
}
|
||||
if (deliveryAddress.state == null) {
|
||||
await showMsgDialog(context, "Error", "Invalid state!");
|
||||
return false;
|
||||
}
|
||||
if (deliveryAddress.country == null) {
|
||||
await showMsgDialog(context, "Error", "Invalid country!");
|
||||
return false;
|
||||
}
|
||||
if (deliveryAddress.phoneNumber == null) {
|
||||
await showMsgDialog(context, "Error", "Invalid phone number!");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<void> _create() async {
|
||||
DeliveryAddress deliveryAddress = _getPayload();
|
||||
bool valid = await _validate(deliveryAddress);
|
||||
if (!valid) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
var deliveryAddressModel =
|
||||
Provider.of<DeliveryAddressModel>(context, listen: false);
|
||||
try {
|
||||
await deliveryAddressModel.createDeliveryAddress(deliveryAddress);
|
||||
Navigator.pop(context);
|
||||
} catch (e) {
|
||||
showMsgDialog(context, "Error", e.toString());
|
||||
} finally {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _update() async {
|
||||
DeliveryAddress deliveryAddress = _getPayload();
|
||||
print('deliveryAddress => ${deliveryAddress.country}');
|
||||
bool valid = await _validate(deliveryAddress);
|
||||
if (!valid) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
var deliveryAddressModel =
|
||||
Provider.of<DeliveryAddressModel>(context, listen: false);
|
||||
try {
|
||||
await deliveryAddressModel.updateDeliveryAddress(deliveryAddress);
|
||||
Navigator.pop(context);
|
||||
} catch (e) {
|
||||
showMsgDialog(context, "Error", e.toString());
|
||||
} finally {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
105
lib/pages/delivery_address/delivery_address_list.dart
Normal file
105
lib/pages/delivery_address/delivery_address_list.dart
Normal file
@@ -0,0 +1,105 @@
|
||||
import 'package:fcs/domain/vo/delivery_address.dart';
|
||||
import 'package:fcs/helpers/theme.dart';
|
||||
import 'package:fcs/pages/delivery_address/delivery_address_editor.dart';
|
||||
import 'package:fcs/pages/main/util.dart';
|
||||
import 'package:fcs/pages/widgets/bottom_up_page_route.dart';
|
||||
import 'package:fcs/pages/widgets/local_text.dart';
|
||||
import 'package:fcs/pages/widgets/progress.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'model/delivery_address_model.dart';
|
||||
import 'delivery_address_row.dart';
|
||||
|
||||
class DeliveryAddressList extends StatefulWidget {
|
||||
final DeliveryAddress deliveryAddress;
|
||||
|
||||
const DeliveryAddressList({Key key, this.deliveryAddress}) : super(key: key);
|
||||
@override
|
||||
_DeliveryAddressListState createState() => _DeliveryAddressListState();
|
||||
}
|
||||
|
||||
class _DeliveryAddressListState extends State<DeliveryAddressList> {
|
||||
bool _isLoading = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var shipmentModel = Provider.of<DeliveryAddressModel>(context);
|
||||
|
||||
return LocalProgress(
|
||||
inAsyncCall: _isLoading,
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
centerTitle: true,
|
||||
leading: new IconButton(
|
||||
icon: new Icon(Icons.close),
|
||||
onPressed: () => Navigator.pop(context, widget.deliveryAddress),
|
||||
),
|
||||
backgroundColor: primaryColor,
|
||||
title: LocalText(
|
||||
context,
|
||||
"delivery_addresses",
|
||||
fontSize: 20,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
body: Column(
|
||||
children: <Widget>[
|
||||
Expanded(
|
||||
child: Column(
|
||||
children:
|
||||
getAddressList(context, shipmentModel.deliveryAddresses),
|
||||
),
|
||||
),
|
||||
Container(
|
||||
padding: EdgeInsets.only(top: 20, bottom: 15, right: 15),
|
||||
child: Align(
|
||||
alignment: Alignment.bottomRight,
|
||||
child: Container(
|
||||
width: 130,
|
||||
height: 40,
|
||||
child: FloatingActionButton.extended(
|
||||
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
BottomUpPageRoute(DeliveryAddressEditor()),
|
||||
);
|
||||
},
|
||||
icon: Icon(Icons.add),
|
||||
label: Text(
|
||||
getLocalString(context, 'delivery_address.new_address'),
|
||||
style: TextStyle(fontSize: 12),
|
||||
),
|
||||
backgroundColor: primaryColor,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
)),
|
||||
);
|
||||
}
|
||||
|
||||
List<Widget> getAddressList(
|
||||
BuildContext context, List<DeliveryAddress> addresses) {
|
||||
return addresses.asMap().entries.map((s) {
|
||||
return InkWell(
|
||||
onTap: () {
|
||||
// Navigator.pop(context, s.value);
|
||||
},
|
||||
child: DeliveryAddressRow(shippingAddress: s.value, index: s.key),
|
||||
);
|
||||
}).toList();
|
||||
}
|
||||
}
|
||||
146
lib/pages/delivery_address/delivery_address_row.dart
Normal file
146
lib/pages/delivery_address/delivery_address_row.dart
Normal file
@@ -0,0 +1,146 @@
|
||||
import 'package:fcs/domain/vo/delivery_address.dart';
|
||||
import 'package:fcs/pages/delivery_address/model/delivery_address_model.dart';
|
||||
import 'package:fcs/pages/widgets/bottom_up_page_route.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'delivery_address_editor.dart';
|
||||
|
||||
class DeliveryAddressRow extends StatelessWidget {
|
||||
final DeliveryAddress shippingAddress;
|
||||
final int index;
|
||||
|
||||
const DeliveryAddressRow({Key key, this.shippingAddress, this.index})
|
||||
: super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var deliveryAddressModel = Provider.of<DeliveryAddressModel>(context);
|
||||
return Container(
|
||||
padding: EdgeInsets.only(left: 10, right: 10),
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
Row(
|
||||
children: <Widget>[
|
||||
Expanded(
|
||||
child: new Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 10.0),
|
||||
child: Row(
|
||||
children: <Widget>[
|
||||
InkWell(
|
||||
onTap: () {
|
||||
Navigator.pop(context, shippingAddress);
|
||||
},
|
||||
child: new Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 8.0),
|
||||
child: new Text(
|
||||
shippingAddress.fullName == null
|
||||
? ''
|
||||
: shippingAddress.fullName,
|
||||
style: new TextStyle(
|
||||
fontSize: 15.0,
|
||||
color: Colors.black,
|
||||
fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 8.0),
|
||||
child: new Text(
|
||||
shippingAddress.addressLine1 == null
|
||||
? ''
|
||||
: shippingAddress.addressLine1,
|
||||
style: new TextStyle(
|
||||
fontSize: 14.0, color: Colors.grey),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 8.0),
|
||||
child: new Text(
|
||||
shippingAddress.addressLine2 == null
|
||||
? ''
|
||||
: shippingAddress.addressLine2,
|
||||
style: new TextStyle(
|
||||
fontSize: 14.0, color: Colors.grey),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 8.0),
|
||||
child: new Text(
|
||||
shippingAddress.city == null
|
||||
? ''
|
||||
: shippingAddress.city,
|
||||
style: new TextStyle(
|
||||
fontSize: 14.0, color: Colors.grey),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 8.0),
|
||||
child: new Text(
|
||||
shippingAddress.state == null
|
||||
? ''
|
||||
: shippingAddress.state,
|
||||
style: new TextStyle(
|
||||
fontSize: 14.0, color: Colors.grey),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 8.0),
|
||||
child: new Text(
|
||||
shippingAddress.country == null
|
||||
? ''
|
||||
: shippingAddress.country,
|
||||
style: new TextStyle(
|
||||
fontSize: 14.0, color: Colors.grey),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 8.0),
|
||||
child: new Text(
|
||||
shippingAddress.phoneNumber == null
|
||||
? ''
|
||||
: "Phone:${shippingAddress.phoneNumber}",
|
||||
style: new TextStyle(
|
||||
fontSize: 14.0, color: Colors.grey),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
padding: EdgeInsets.only(right: 30),
|
||||
icon: Icon(Icons.edit, color: Colors.black45),
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
BottomUpPageRoute(DeliveryAddressEditor(
|
||||
deliveryAddress: shippingAddress)),
|
||||
);
|
||||
}),
|
||||
IconButton(
|
||||
padding: EdgeInsets.only(right: 30),
|
||||
icon: Icon(Icons.delete, color: Colors.black45),
|
||||
onPressed: () async {
|
||||
await deliveryAddressModel
|
||||
.deleteDeliveryAddress(shippingAddress);
|
||||
})
|
||||
],
|
||||
),
|
||||
index == null
|
||||
? Container()
|
||||
: index == deliveryAddressModel.deliveryAddresses.length - 1
|
||||
? Container()
|
||||
: Divider(color: Colors.black)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
64
lib/pages/delivery_address/model/delivery_address_model.dart
Normal file
64
lib/pages/delivery_address/model/delivery_address_model.dart
Normal file
@@ -0,0 +1,64 @@
|
||||
import 'dart:async';
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:fcs/data/services/services.dart';
|
||||
import 'package:fcs/domain/vo/delivery_address.dart';
|
||||
import 'package:fcs/pages/main/model/base_model.dart';
|
||||
import 'package:fcs/domain/constants.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
|
||||
class DeliveryAddressModel extends BaseModel {
|
||||
final log = Logger('FcsShipmentModel');
|
||||
List<DeliveryAddress> deliveryAddresses = [];
|
||||
|
||||
StreamSubscription<QuerySnapshot> listener;
|
||||
|
||||
@override
|
||||
void privilegeChanged() {
|
||||
super.privilegeChanged();
|
||||
_loadDeliveryAddresses();
|
||||
}
|
||||
|
||||
Future<void> _loadDeliveryAddresses() async {
|
||||
if (user == null) return;
|
||||
String path = "$delivery_address_collection/";
|
||||
if (listener != null) listener.cancel();
|
||||
deliveryAddresses = [];
|
||||
try {
|
||||
listener = Firestore.instance
|
||||
.collection('users')
|
||||
.document("${user.id}")
|
||||
.collection("$path")
|
||||
.snapshots()
|
||||
.listen((QuerySnapshot snapshot) {
|
||||
deliveryAddresses.clear();
|
||||
deliveryAddresses = snapshot.documents.map((documentSnapshot) {
|
||||
var s = DeliveryAddress.fromMap(
|
||||
documentSnapshot.data, documentSnapshot.documentID);
|
||||
return s;
|
||||
}).toList();
|
||||
notifyListeners();
|
||||
});
|
||||
} catch (e) {
|
||||
log.warning("Error!! $e");
|
||||
}
|
||||
}
|
||||
|
||||
void initUser(user) {
|
||||
super.initUser(user);
|
||||
}
|
||||
|
||||
Future<void> createDeliveryAddress(DeliveryAddress deliveryAddress) {
|
||||
return Services.instance.deliveryAddressService
|
||||
.createDeliveryAddress(deliveryAddress);
|
||||
}
|
||||
|
||||
Future<void> updateDeliveryAddress(DeliveryAddress deliveryAddress) {
|
||||
return Services.instance.deliveryAddressService
|
||||
.updateDeliveryAddress(deliveryAddress);
|
||||
}
|
||||
|
||||
Future<void> deleteDeliveryAddress(DeliveryAddress deliveryAddress) {
|
||||
return Services.instance.deliveryAddressService
|
||||
.deleteDeliveryAddress(deliveryAddress);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user