Home > Software design >  How to run functions of a screen in the next screen With flutter?
How to run functions of a screen in the next screen With flutter?

Time:11-16

I'm on creating a mobile app with flutter. At first, I have to connect to bluetooth. after connecting to a specific device I have to redirect to the next page which is the home page. This screen doesn't show any information for the device but I have to recieve data via bluetooth in this screen. I do something like this :

 if (snapshot.data == BluetoothDeviceState.connected) {
                          WidgetsBinding.instance.addPostFrameCallback(
                            (_) {
                              Navigator.of(context).push(MaterialPageRoute(
                                  builder: (context) => MainScreen()));
                              BluetoothServiceList(device: d);
                              BluetoothDeviceStateHandler(
                                device: d,
                              );
                            },
                          );
                          return ElevatedButton(
                              child: const Text('CONNECTED'),
                              onPressed: () {
                                Navigator.of(context).push(MaterialPageRoute(
                                    builder: (context) => MainScreen()));
                                BluetoothServiceList(device: d);
                                BluetoothDeviceStateHandler(
                                  device: d,
                                );
                              });
                        }

the two functions BluetoothServiceList and BluetoothDeviceStateHandler existed in another screen but I shouldn't show this screen anymore.

all the functions should be executed in this class without showing anything. I want just to display alerts while recieving data from the other device.

class MainScreen extends StatelessWidget {
  const MainScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
          body: new Stack(
        children: <Widget>[
          new Container(
            decoration: new BoxDecoration(
              image: new DecorationImage(
                image: new AssetImage("assets/Fond.png"),
                fit: BoxFit.cover,
              ),
            ),
          ),
          Align(
            alignment: Alignment.bottomLeft,
            child: FloatingActionButton(
              onPressed: () {},
              child: Image.asset('assets/Alerte notif.png'),
              elevation: 30,
            ),
          ),
          Align(
            alignment: Alignment.bottomRight,
            child: FloatingActionButton(
              onPressed: () {},
              child: Image.asset('assets/Panneau distance rouge.png'),
              elevation: 15,
            ),
          ),
          Align(
            alignment: Alignment.bottomCenter,
            child: TextButton(
              onPressed: () {},
              child: (Image.asset(
                'assets/camion2.png',
                height: 200,
                width: 500,
              )),
            ),
          ),
        ],
      )),
    );
  }
}


Anyone here can help me please ? I want to know if my logic is right and iof there are solutions for what I'm thinking about.

CodePudding user response:

You can call function of previous screen to next screen via Callback Function.

CodePudding user response:

there are some ways:

  1. use call back function
  2. use initState on next screen and rewrite the function of previous screen code there.
  3. write the condition of if in next if bluethooth is connected run the function. if you have any questions or you did not get it comment here I'm online
  • Related