If i click on device back button it should redirect to the previous page but it directly goes to the homepage of the application

Viewed 866

Page skips while navigating through device back button.

import 'package:flutter/material.dart';
import 'package:modal_progress_hud/modal_progress_hud.dart';

class Attendance extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return AttendanceCalendar();
  }
}

class AttendanceCalendar extends State<Attendance> {
  bool isAsync = false;

  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _onBackButtonPressed,
      child: ModalProgressHUD(
        inAsyncCall: isAsync,
        child: Scaffold(
          appBar: AppBar(
              title: Text("Attendance");
          ),
          body: Container(),
        ),
      ),
    );
  }

  void _onBackButtonPressed() {
    Navigator.pop(context);
  }
}
1 Answers

If you use MaterialApp() as root widget on this page as well as previous pages so pages were loading in the same Material App Space. Then back button press navigating skips between the same material apps. So recommend you to use Scaffold() as a root widget.

Related