Flutter — как условно перейти на главную страницу при отключении (пример flutter_reactive_ble)

#flutter #navigation #bluetooth-lowenergy

Вопрос:

Я использую пример flutter_reactive_ble для своего проекта,

У меня есть в :-

главная.дротик

   runApp(
    MultiProvider(
      providers: [
        StreamProvider<ConnectionStateUpdate>(
          create: (_) => _connector.state,
          initialData: const ConnectionStateUpdate(
            deviceId: 'Unknown device',
            connectionState: DeviceConnectionState.disconnected,
            failure: null,
          ),
        ),
     ],
     child:MaterialApp(
        home:HomePage(),
     ), //MaterialApp
    
.
..
...

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) => Consumer<BleStatus?>(
        builder: (_, status, __) {
          if (status == BleStatus.ready) {
            //if bluetooth is ready, but not connected
            return Page1();
          } else {
            //handles errors and display solution if bluetooth is not ready
            return BleStatusScreen(status: status ?? BleStatus.unknown);
          }
        },
      );
}

 

страница 1.дротик

 ElevatedButton(
   child:Text('go'),
   onPressed: ()
   { 
      Navigator.push(
                context,
                 MaterialPageRoute(
                  builder: (context) => Page2(),
                 ),
                );
   }
 

страница 2.дротик

 Class _Page2State extends state<_Page2> {
 @override
  Widget build(BuildContext context) {
    // Consumer<DeviceConnectionState?>(
    //     builder: (_, connection, __) {
    //       if (connection == DeviceConnectionState.disconnected) {
    //         //if not connected, navigate back to Page1();?
    //         return Page1(); ???
    //         or navigatorpush???
    //             Navigator.pushReplacement(
    //                      context,
    //                      MaterialPageRoute(
    //                        builder: (context) => Page1(),
    //                      ),
    //                    );
    //       } else {
    //         // display this page
    //         return Page2();
    //       }
    //     },
    //   );
 }
}
 

so my question is:-

if I uncommented navigator push, of course it will not compile,
but if I uncomment the line return Page1();, what will happen to the widget page stack?
I would prefer for it to be the only page left, as if I used Navigator.pushReplacement() there.