簡體   English   中英

flutter如何在下一個屏幕運行一個屏幕的功能?

[英]How to run functions of a screen in the next screen With flutter?

我正在使用 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 中執行,而不顯示任何內容。 我只想在從其他設備接收數據時顯示警報。

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


這里有人可以幫助我嗎? 我想知道我的邏輯是否正確,是否有針對我所想的解決方案。

您可以通過Callback Function將上一屏的 function 調用到下一屏。

有一些方法:

  1. 使用回調 function
  2. 在下一個屏幕上使用 initState 並在那里重寫上一個屏幕代碼的 function。
  3. 如果藍牙已連接,請在下一步中寫入 if 的條件運行 function。如果您有任何問題或沒有得到它,請在此處評論我在線

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM