flutter-woocommerce-app/LabelStoreMax/lib/resources/pages/account_shipping_details.dart
2022-08-29 15:34:56 +08:00

259 lines
9.7 KiB
Dart

// Label StoreMax
//
// Created by Anthony Gordon.
// 2022, WooSignal Ltd. All rights reserved.
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import 'package:flutter/material.dart';
import 'package:flutter_app/bootstrap/helpers.dart';
import 'package:flutter_app/bootstrap/shared_pref/sp_auth.dart';
import 'package:flutter_app/resources/widgets/app_loader_widget.dart';
import 'package:flutter_app/resources/widgets/buttons.dart';
import 'package:flutter_app/resources/widgets/safearea_widget.dart';
import 'package:flutter_app/resources/widgets/woosignal_ui.dart';
import 'package:nylo_framework/nylo_framework.dart';
import 'package:wp_json_api/models/responses/wc_customer_info_response.dart';
import 'package:wp_json_api/models/responses/wc_customer_updated_response.dart';
import 'package:wp_json_api/wp_json_api.dart';
class AccountShippingDetailsPage extends StatefulWidget {
AccountShippingDetailsPage();
@override
_AccountShippingDetailsPageState createState() =>
_AccountShippingDetailsPageState();
}
class _AccountShippingDetailsPageState
extends State<AccountShippingDetailsPage> {
_AccountShippingDetailsPageState();
// BILLING TEXT CONTROLLERS
final TextEditingController _txtShippingFirstName = TextEditingController(),
_txtShippingLastName = TextEditingController(),
_txtShippingAddressLine = TextEditingController(),
_txtShippingCity = TextEditingController(),
_txtShippingPostalCode = TextEditingController(),
_txtShippingState = TextEditingController(),
_txtShippingCountry = TextEditingController();
bool _isLoading = true, _isUpdating = false;
@override
void initState() {
super.initState();
_fetchUserDetails();
}
_fetchUserDetails() async {
String? userToken = await readAuthToken();
WCCustomerInfoResponse? wcCustomerInfoResponse;
try {
wcCustomerInfoResponse = await WPJsonAPI.instance
.api((request) => request.wcCustomerInfo(userToken!));
} on Exception catch (_) {
showToastNotification(
context,
title: trans("Oops!"),
description: trans("Something went wrong"),
style: ToastNotificationStyleType.DANGER,
);
Navigator.pop(context);
return;
} finally {
setState(() {
_isLoading = false;
});
}
if (wcCustomerInfoResponse != null &&
wcCustomerInfoResponse.status == 200) {
Shipping shipping = wcCustomerInfoResponse.data!.shipping!;
_txtShippingFirstName.text = shipping.firstName!;
_txtShippingLastName.text = shipping.lastName!;
_txtShippingAddressLine.text = shipping.address1!;
_txtShippingCity.text = shipping.city!;
_txtShippingState.text = shipping.state!;
_txtShippingPostalCode.text = shipping.postcode!;
_txtShippingCountry.text = shipping.country!;
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
title: Text(trans("Shipping Details")),
centerTitle: true,
),
body: SafeAreaWidget(
child: GestureDetector(
onTap: () {
FocusScope.of(context).requestFocus(FocusNode());
},
child: _isLoading
? AppLoaderWidget()
: LayoutBuilder(
builder: (context, constraints) => Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
SizedBox(
child: Container(
margin: EdgeInsets.only(top: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Row(
children: <Widget>[
Flexible(
child: TextEditingRow(
heading: trans("First Name"),
controller: _txtShippingFirstName,
shouldAutoFocus: true,
),
),
Flexible(
child: TextEditingRow(
heading: trans("Last Name"),
controller: _txtShippingLastName,
),
),
],
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment:
MainAxisAlignment.spaceEvenly,
),
TextEditingRow(
heading: trans("Address Line"),
controller: _txtShippingAddressLine,
),
Row(children: <Widget>[
Flexible(
child: TextEditingRow(
heading: trans("City"),
controller: _txtShippingCity,
),
),
Flexible(
child: TextEditingRow(
heading: trans("State"),
controller: _txtShippingState,
),
),
]),
Row(
children: <Widget>[
Flexible(
child: TextEditingRow(
heading: trans("Postal code"),
controller: _txtShippingPostalCode,
),
),
Flexible(
child: TextEditingRow(
heading: trans("Country"),
controller: _txtShippingCountry,
),
),
],
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment:
MainAxisAlignment.spaceEvenly,
)
],
),
decoration: BoxDecoration(
color: ThemeColor.get(context).surfaceBackground,
borderRadius: BorderRadius.circular(10),
boxShadow: (Theme.of(context).brightness ==
Brightness.light)
? wsBoxShadow()
: null,
),
padding: EdgeInsets.all(8),
),
height:
(constraints.maxHeight - constraints.minHeight) *
0.6,
),
Column(
children: <Widget>[
PrimaryButton(
title: trans("UPDATE DETAILS"),
isLoading: _isUpdating,
action: _updateShippingDetails,
),
],
),
],
),
),
),
),
);
}
_updateShippingDetails() async {
String firstName = _txtShippingFirstName.text;
String lastName = _txtShippingLastName.text;
String addressLine = _txtShippingAddressLine.text;
String city = _txtShippingCity.text;
String state = _txtShippingState.text;
String postalCode = _txtShippingPostalCode.text;
String country = _txtShippingCountry.text;
String? userToken = await readAuthToken();
if (_isUpdating == true) {
return;
}
setState(() {
_isUpdating = true;
});
WCCustomerUpdatedResponse? wcCustomerUpdatedResponse;
try {
wcCustomerUpdatedResponse = await WPJsonAPI.instance.api(
(request) => request.wcUpdateCustomerInfo(
userToken,
shippingFirstName: firstName,
shippingLastName: lastName,
shippingAddress1: addressLine,
shippingCity: city,
shippingState: state,
shippingPostcode: postalCode,
shippingCountry: country,
),
);
} on Exception catch (_) {
showToastNotification(context,
title: trans("Oops!"),
description: trans("Something went wrong"),
style: ToastNotificationStyleType.DANGER);
} finally {
setState(() {
_isUpdating = true;
});
}
if (wcCustomerUpdatedResponse != null &&
wcCustomerUpdatedResponse.status == 200) {
showToastNotification(context,
title: trans("Success"),
description: trans("Account updated"),
style: ToastNotificationStyleType.SUCCESS);
Navigator.pop(context);
}
}
}