flutter-radio icon indicating copy to clipboard operation
flutter-radio copied to clipboard

newbie asking for guidance

Open rumbis opened this issue 3 years ago • 1 comments

thanks for your effort I decided to learn on this project to create apps it was motivational and helpful but i got an issue that will bring me a lot of frustration to discover it alone while I'm starting the app the back button its going to background instead of an popup for exit `can anyone help me with quick fix for him and hard to learn for me ? thanks in advance @amangautam1

rumbis avatar Aug 13 '21 12:08 rumbis

@rumbis this is the way can confirm exit on the click of the back button.

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  
  DateTime? backButtonPressTime;

  Future<bool> handleWillPop(BuildContext context) async {
    final now = DateTime.now();
    final backButtonHasNotBeenPressedOrSnackBarHasBeenClosed =
        backButtonPressTime == null || now.difference(backButtonPressTime!) > const Duration(seconds: 3);

    if (backButtonHasNotBeenPressedOrSnackBarHasBeenClosed) {
      backButtonPressTime = now;
      ShowSnackBar().showSnackBar(
        context,
        'Press Back Again to Exit App',
        duration: const Duration(seconds: 2),
        noAction: true,
      );
      return false;
    }
    return true;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      backgroundColor: Colors.transparent,
    
      body: WillPopScope(
        onWillPop: () => handleWillPop(context),
        child: Container(),
      ),
    );
  }
}

HaardikBhagtani avatar Jul 17 '22 21:07 HaardikBhagtani