如何在有扑动的下一个屏幕中运行屏幕的功能?



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

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

这里有人能帮我吗?我想知道我的逻辑是否正确,是否有解决我正在思考的问题的方法。

有一些方法:

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

您可以通过回调函数将上一屏幕的函数调用到下一屏幕。

最新更新