-1

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())),
                          );
                        }
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
rania
  • 63
  • 6
  • can you add more code and explain more? what do you mean by `Just after changing the value of a button` ? – eamirho3ein Nov 13 '22 at 16:31
  • @eamirho3ein I edited my question. I want to redirect to the next screen just after the button text is Connected – rania Nov 13 '22 at 16:37

2 Answers2

0

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()));
  });
}
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
0

You can try this:

if (snapshot.data == BluetoothDeviceState.connected) {
  WidgetsBinding.instance.addPostFrameCallback((_) {
        Navigator.of(context).push(
            MaterialPageRoute(builder: (context) => MainScreen()));
  });
  return Text('CONNECTED');
}
eamirho3ein
  • 16,619
  • 2
  • 12
  • 23