Home > Blockchain >  How do I stop user from swiping off loading screen in Flutter?
How do I stop user from swiping off loading screen in Flutter?

Time:02-18

I have a loading screen in my flutter app that is displayed using a ternary operator when a loading boolean is true. When this screen shows, the user can just swipe to get off it (on iOS) and I would imagine they could use the back button on Android to achieve the same effect.

What is the best practice to make it so the user cannot leave a loading screen in flutter?

CodePudding user response:

you can use the WillPopScope widget provided by Flutter.

As the docs says in here, WillPopScope:

Creates a widget that registers a callback to veto attempts by the user to dismiss the enclosing

It basically creates a widget that can prevent user from returnig to another screen, the user still can use the home button and close the application, but at least prevent the go back.

Example code:

import 'package:flutter/material.dart';

class ExamplePop extends StatefulWidget {
  const ExamplePop({Key? key}) : super(key: key);

  @override
  State<ExamplePop> createState() => _ExamplePopState();
}

class _ExamplePopState extends State<ExamplePop> {
  bool canPop = false;

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        return canPop;
      },
      child: Scaffold(
        appBar: AppBar(
          automaticallyImplyLeading: true,
        ),
      ),
    );
  }
}

If the onWillPop return false the user can´t go back, if it return true he goes back.

  • Related