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');
}