Skip to content

Commit cfb37fe

Browse files
committed
pre loading example combined with required args in widget/view
1 parent 0132c18 commit cfb37fe

4 files changed

Lines changed: 104 additions & 210 deletions

File tree

lib/pages/shopinbit/shopinbit_offer_view.dart

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
44
import '../../models/shopinbit/shopinbit_order_model.dart';
55
import '../../providers/global/shopin_bit_service_provider.dart';
66
import '../../themes/stack_colors.dart';
7+
import '../../utilities/show_loading.dart';
78
import '../../utilities/text_styles.dart';
89
import '../../utilities/util.dart';
910
import '../../widgets/background.dart';
@@ -14,6 +15,7 @@ import '../../widgets/desktop/primary_button.dart';
1415
import '../../widgets/desktop/secondary_button.dart';
1516
import '../../widgets/dialogs/s_dialog.dart';
1617
import '../../widgets/rounded_white_container.dart';
18+
import '../../widgets/stack_dialog.dart';
1719
import 'shopinbit_shipping_view.dart';
1820

1921
class ShopInBitOfferView extends ConsumerStatefulWidget {
@@ -145,13 +147,59 @@ class _ShopInBitOfferViewState extends ConsumerState<ShopInBitOfferView> {
145147
label: "Accept offer",
146148
buttonHeight: Util.isDesktop ? ButtonHeight.l : null,
147149
enabled: !_loading,
148-
onPressed: () {
150+
onPressed: () async {
149151
// TODO verify this is ok to stay set to accepted if the next route pops back and then decline is tapped
150152
model.status = ShopInBitOrderStatus.accepted;
151153

152-
Navigator.of(
153-
context,
154-
).pushNamed(ShopInBitShippingView.routeName, arguments: model);
154+
final shopinBitApi = ref.read(pShopinBitService).client;
155+
final response = await showLoading(
156+
context: context,
157+
rootNavigator: true,
158+
message: "Updating available countries",
159+
whileFuture: shopinBitApi.getCountries(),
160+
delay: const Duration(
161+
seconds: 1,
162+
), // at least 1 sec to prevent ui flashing
163+
);
164+
165+
if (!context.mounted) return;
166+
167+
String? errorMessage;
168+
169+
if (response?.value == null) {
170+
errorMessage =
171+
response?.exception?.toString() ??
172+
"Failed to fetch countries data";
173+
} else if (response!.value!
174+
.where((c) => c['iso'] == model.deliveryCountry)
175+
.length !=
176+
1) {
177+
errorMessage =
178+
"Delivery country code \""
179+
"${model.deliveryCountry}"
180+
"\" is invalid";
181+
}
182+
183+
if (errorMessage != null) {
184+
await showDialog<dynamic>(
185+
context: context,
186+
useRootNavigator: Util.isDesktop,
187+
builder: (context) => StackOkDialog(
188+
title: "ShopinBit API error",
189+
maxWidth: Util.isDesktop ? 500 : null,
190+
message: errorMessage,
191+
desktopPopRootNavigator: Util.isDesktop,
192+
),
193+
);
194+
return;
195+
}
196+
197+
if (context.mounted) {
198+
await Navigator.of(context).pushNamed(
199+
ShopInBitShippingView.routeName,
200+
arguments: (model: model, countries: response!.value!),
201+
);
202+
}
155203
},
156204
),
157205
SecondaryButton(

0 commit comments

Comments
 (0)