Home > Software design >  How to redirect to the next page without pressing a button with flutter?
How to redirect to the next page without pressing a button with flutter?

Time:11-14

I want to move to the next page without clicking on button with flutter. Juste after changing the value of a button, I have to redirect to the next page after some delay without any self interaction. this is the code :

   initialData: BluetoothDeviceState.disconnected,
                      builder: (c, snapshot) {
                        if (snapshot.data == BluetoothDeviceState.connected) {
                          return ElevatedButton(
                            child: const Text('CONNECTED'),
                            onPressed: () => Navigator.of(context).push(
                                MaterialPageRoute(
                                    builder: (context) => MainScreen())),
                          );
                        }

CodePudding user response:

While the connection is depend on bool, you can do

if (snapshot.data == BluetoothDeviceState.connected) {
     // a frame delay
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
    Navigator.of(context)
        .push(MaterialPageRoute(builder: (context) => MainScreen()));
  });
}

CodePudding user response:

You can try this:

if (snapshot.data == BluetoothDeviceState.connected) {
  WidgetsBinding.instance.addPostFrameCallback((_) {
        Navigator.of(context).push(
            MaterialPageRoute(builder: (context) => MainScreen()));
  });
  return Text('CONNECTED');
}
  • Related