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.