Fix popup null context as it was using a dismissed (unmounted) context [skip ci]

OmarHatem committed Feb 8, 2023 at 17:01 UTC fa54ebe855bcb2d18cd47df70aff3394374bbb7c
2 files changed +126 -125
lib/src/screens/exchange_trade/exchange_trade_page.dart
+118 -114
@@ -112,7 +112,7 @@ class ExchangeTradeState extends State<ExchangeTradeForm> {
112 width: 16,
113 color: Theme.of(context).primaryTextTheme!.overline!.color!);
114
115 - _setEffects(context);
115 + _setEffects();
116
117 return Container(
118 child: ScrollableWithBottomSection(
@@ -234,7 +234,7 @@ class ExchangeTradeState extends State<ExchangeTradeForm> {
234 );
235 }
236
237 - void _setEffects(BuildContext context) {
237 + void _setEffects() {
238 if (_effectsInstalled) {
239 return;
240 }
@@ -245,12 +245,12 @@ class ExchangeTradeState extends State<ExchangeTradeForm> {
245 WidgetsBinding.instance.addPostFrameCallback((_) {
246 showPopUp<void>(
247 context: context,
248 - builder: (BuildContext context) {
248 + builder: (BuildContext popupContext) {
249 return AlertWithOneAction(
250 - alertTitle: S.of(context).error,
250 + alertTitle: S.of(popupContext).error,
251 alertContent: state.error,
252 - buttonText: S.of(context).ok,
253 - buttonAction: () => Navigator.of(context).pop());
252 + buttonText: S.of(popupContext).ok,
253 + buttonAction: () => Navigator.of(popupContext).pop());
254 });
255 });
256 }
@@ -259,118 +259,24 @@ class ExchangeTradeState extends State<ExchangeTradeForm> {
259 WidgetsBinding.instance.addPostFrameCallback((_) {
260 showPopUp<void>(
261 context: context,
262 - builder: (BuildContext context) {
262 + builder: (BuildContext popupContext) {
263 return ConfirmSendingAlert(
264 - alertTitle: S.of(context).confirm_sending,
265 - amount: S.of(context).send_amount,
264 + alertTitle: S.of(popupContext).confirm_sending,
265 + amount: S.of(popupContext).send_amount,
266 amountValue: widget.exchangeTradeViewModel.sendViewModel
267 .pendingTransaction!.amountFormatted,
268 - fee: S.of(context).send_fee,
268 + fee: S.of(popupContext).send_fee,
269 feeValue: widget.exchangeTradeViewModel.sendViewModel
270 .pendingTransaction!.feeFormatted,
271 - rightButtonText: S.of(context).ok,
272 - leftButtonText: S.of(context).cancel,
271 + rightButtonText: S.of(popupContext).ok,
272 + leftButtonText: S.of(popupContext).cancel,
273 actionRightButton: () async {
274 - Navigator.of(context).pop();
274 + Navigator.of(popupContext).pop();
275 await widget.exchangeTradeViewModel.sendViewModel
276 .commitTransaction();
277 - await showPopUp<void>(
278 - context: context,
279 - builder: (BuildContext context) {
280 - return Observer(builder: (_) {
281 - final state = widget
282 - .exchangeTradeViewModel.sendViewModel.state;
283 -
284 - if (state is TransactionCommitted) {
285 - return Stack(
286 - children: <Widget>[
287 - Container(
288 - color: Theme.of(context).backgroundColor,
289 - child: Center(
290 - child: Image.asset(
291 - 'assets/images/birthday_cake.png'),
292 - ),
293 - ),
294 - Center(
295 - child: Padding(
296 - padding: EdgeInsets.only(
297 - top: 220, left: 24, right: 24),
298 - child: Text(
299 - S.of(context).send_success(widget
300 - .exchangeTradeViewModel
301 - .wallet
302 - .currency
303 - .toString()),
304 - textAlign: TextAlign.center,
305 - style: TextStyle(
306 - fontSize: 22,
307 - fontWeight: FontWeight.bold,
308 - color: Theme.of(context)
309 - .primaryTextTheme!
310 - .headline6!
311 - .color,
312 - decoration: TextDecoration.none,
313 - ),
314 - ),
315 - ),
316 - ),
317 - Positioned(
318 - left: 24,
319 - right: 24,
320 - bottom: 24,
321 - child: PrimaryButton(
322 - onPressed: () =>
323 - Navigator.of(context).pop(),
324 - text: S.of(context).send_got_it,
325 - color: Theme.of(context)
326 - .accentTextTheme!
327 - .bodyText1!
328 - .color!,
329 - textColor: Colors.white))
330 - ],
331 - );
332 - }
333 -
334 - return Stack(
335 - children: <Widget>[
336 - Container(
337 - color: Theme.of(context).backgroundColor,
338 - child: Center(
339 - child: Image.asset(
340 - 'assets/images/birthday_cake.png'),
341 - ),
342 - ),
343 - BackdropFilter(
344 - filter: ImageFilter.blur(
345 - sigmaX: 3.0, sigmaY: 3.0),
346 - child: Container(
347 - decoration: BoxDecoration(
348 - color: Theme.of(context)
349 - .backgroundColor
350 - .withOpacity(0.25)),
351 - child: Center(
352 - child: Padding(
353 - padding: EdgeInsets.only(top: 220),
354 - child: Text(
355 - S.of(context).send_sending,
356 - textAlign: TextAlign.center,
357 - style: TextStyle(
358 - fontSize: 22,
359 - fontWeight: FontWeight.bold,
360 - color: Theme.of(context).primaryTextTheme!.headline6!.color!,
361 - decoration: TextDecoration.none,
362 - ),
363 - ),
364 - ),
365 - ),
366 - ),
367 - )
368 - ],
369 - );
370 - });
371 - });
277 + transactionStatePopup();
278 },
373 - actionLeftButton: () => Navigator.of(context).pop(),
279 + actionLeftButton: () => Navigator.of(popupContext).pop(),
280 feeFiatAmount: widget.exchangeTradeViewModel
281 .pendingTransactionFeeFiatAmountFormatted,
282 fiatAmountValue: widget.exchangeTradeViewModel
@@ -385,12 +291,12 @@ class ExchangeTradeState extends State<ExchangeTradeForm> {
291 WidgetsBinding.instance.addPostFrameCallback((_) {
292 showPopUp<void>(
293 context: context,
388 - builder: (BuildContext context) {
294 + builder: (BuildContext popupContext) {
295 return AlertWithOneAction(
390 - alertTitle: S.of(context).sending,
391 - alertContent: S.of(context).transaction_sent,
392 - buttonText: S.of(context).ok,
393 - buttonAction: () => Navigator.of(context).pop());
296 + alertTitle: S.of(popupContext).sending,
297 + alertContent: S.of(popupContext).transaction_sent,
298 + buttonText: S.of(popupContext).ok,
299 + buttonAction: () => Navigator.of(popupContext).pop());
300 });
301 });
302 }
@@ -398,4 +304,102 @@ class ExchangeTradeState extends State<ExchangeTradeForm> {
304
305 _effectsInstalled = true;
306 }
307 +
308 + transactionStatePopup() {
309 + showPopUp<void>(
310 + context: context,
311 + builder: (BuildContext popupContext) {
312 + return Observer(builder: (_) {
313 + final state = widget
314 + .exchangeTradeViewModel.sendViewModel.state;
315 +
316 + if (state is TransactionCommitted) {
317 + return Stack(
318 + children: <Widget>[
319 + Container(
320 + color: Theme.of(popupContext).backgroundColor,
321 + child: Center(
322 + child: Image.asset(
323 + 'assets/images/birthday_cake.png'),
324 + ),
325 + ),
326 + Center(
327 + child: Padding(
328 + padding: EdgeInsets.only(
329 + top: 220, left: 24, right: 24),
330 + child: Text(
331 + S.of(popupContext).send_success(widget
332 + .exchangeTradeViewModel
333 + .wallet
334 + .currency
335 + .toString()),
336 + textAlign: TextAlign.center,
337 + style: TextStyle(
338 + fontSize: 22,
339 + fontWeight: FontWeight.bold,
340 + color: Theme.of(popupContext)
341 + .primaryTextTheme!
342 + .headline6!
343 + .color,
344 + decoration: TextDecoration.none,
345 + ),
346 + ),
347 + ),
348 + ),
349 + Positioned(
350 + left: 24,
351 + right: 24,
352 + bottom: 24,
353 + child: PrimaryButton(
354 + onPressed: () =>
355 + Navigator.of(popupContext).pop(),
356 + text: S.of(popupContext).send_got_it,
357 + color: Theme.of(popupContext)
358 + .accentTextTheme!
359 + .bodyText1!
360 + .color!,
361 + textColor: Colors.white))
362 + ],
363 + );
364 + }
365 +
366 + return Stack(
367 + children: <Widget>[
368 + Container(
369 + color: Theme.of(popupContext).backgroundColor,
370 + child: Center(
371 + child: Image.asset(
372 + 'assets/images/birthday_cake.png'),
373 + ),
374 + ),
375 + BackdropFilter(
376 + filter: ImageFilter.blur(
377 + sigmaX: 3.0, sigmaY: 3.0),
378 + child: Container(
379 + decoration: BoxDecoration(
380 + color: Theme.of(popupContext)
381 + .backgroundColor
382 + .withOpacity(0.25)),
383 + child: Center(
384 + child: Padding(
385 + padding: EdgeInsets.only(top: 220),
386 + child: Text(
387 + S.of(popupContext).send_sending,
388 + textAlign: TextAlign.center,
389 + style: TextStyle(
390 + fontSize: 22,
391 + fontWeight: FontWeight.bold,
392 + color: Theme.of(popupContext).primaryTextTheme!.headline6!.color!,
393 + decoration: TextDecoration.none,
394 + ),
395 + ),
396 + ),
397 + ),
398 + ),
399 + )
400 + ],
401 + );
402 + });
403 + });
404 + }
405 }
lib/utils/show_pop_up.dart
+8 -11
@@ -9,15 +9,12 @@ Future<T?> showPopUp<T>({
9 bool useRootNavigator = true,
10 RouteSettings? routeSettings
11 }) async {
12 - if (context.mounted) {
13 - return showDialog<T>(
14 - context: context,
15 - builder: builder,
16 - barrierDismissible: barrierDismissible,
17 - barrierColor: barrierColor,
18 - useSafeArea: useSafeArea,
19 - useRootNavigator: useRootNavigator,
20 - routeSettings: routeSettings);
21 - }
22 - return null;
12 + return showDialog<T>(
13 + context: context,
14 + builder: builder,
15 + barrierDismissible: barrierDismissible,
16 + barrierColor: barrierColor,
17 + useSafeArea: useSafeArea,
18 + useRootNavigator: useRootNavigator,
19 + routeSettings: routeSettings);
20 }