kauemurakami / getx_pattern

Design pattern designed to standardize your projects with GetX on Flutter.
https://kauemurakami.github.io/getx_pattern
1k stars 235 forks source link

Detect back button event in android #6

Closed wallace7souza closed 4 years ago

wallace7souza commented 4 years ago

There is a way to handle back button event?

My class extends GetView but I don't know how to implement it when the user tap the back button on the device.

kauemurakami commented 4 years ago

Good morning Wallace. The use of Get.back () serves this purpose. But you have to make sure that the previous page is still accessible, that is, you have gone to the current one with Get.toNamed () or Get.to () for example.

An example of a button that I use globally when I need a backbutton in my apps.

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xxx/theme/colors_theme.dart';

class CustomButtonBack extends Container {
  @override
  Widget build(BuildContext context) {
    return IconButton(
        icon: Icon(
          Icons.arrow_back_ios,
          color: mainColor,
          size: 40,
        ),
        onPressed: () => Get.back());
  }
}

It wouldn't work If you used Get.offAllNamed () for example, which removes previous pages from the stack, Now if you’re talking about the phone’s native back button, you should look for something like this .

wallace7souza commented 4 years ago

Hi Kaue! Thanks for helping! I meant when the user uses the device back button. I would like to alert the user that is leaving the scene with possible lost data and confirm if he really wants this action.

kauemurakami commented 4 years ago

Got it, forgive the mistake. This can certainly be controlled by get, even using the get dialog, but the event must be captured by the flutter. including in the video I sent you or in some suggested, I saw just that. very quiet to do. Any questions we are here, thanks for contributing.

kauemurakami commented 4 years ago

https://youtu.be/fYBCzgBRkb4 that explanation addresses just that. you can control this decision state with get, even using the get dialog. But there is no function or method for this controlle in get.