【问题标题】:How to run functions of a screen in the next screen With flutter?如何在下一个屏幕中使用颤动运行屏幕的功能?
【发布时间】:2022-11-16 21:34:04
【问题描述】:

我正在用 flutter 创建一个移动应用程序。首先,我必须连接到蓝牙。连接到特定设备后,我必须重定向到下一页,即主页。此屏幕不显示设备的任何信息,但我必须在此屏幕中通过蓝牙接收数据。 我做这样的事情:

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

BluetoothServiceListBluetoothDeviceStateHandler 这两个函数存在于另一个屏幕中,但我不应该再显示这个屏幕了。

所有的功能都应该在这个类中执行而不显示任何东西。我只想在从其他设备接收数据时显示警报。

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,
              )),
            ),
          ),
        ],
      )),
    );
  }
}


这里有人可以帮助我吗?我想知道我的逻辑是否正确,是否有针对我所想的解决方案。

【问题讨论】:

    标签: flutter function dart triggers execute


    【解决方案1】:

    你可以通过Callback Function调用上一屏功能到下一屏。

    【讨论】:

      【解决方案2】:

      有一些方法:

      1. 使用回调函数
      2. 在下一个屏幕上使用 initState 并在那里重写上一个屏幕代码的功能。
      3. 如果蓝牙已连接,则在下一步中写入 if 的条件,然后运行该函数。如果您有任何问题或没有得到它,请在此处发表评论我在线

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-24
        • 1970-01-01
        • 1970-01-01
        • 2021-04-21
        • 2021-01-24
        • 2019-07-18
        • 2022-01-25
        • 1970-01-01
        相关资源
        最近更新 更多