import 'package:flutter/material.dart'; import 'Page/display_page.dart'; import 'Page/message_page.dart'; import 'Page/log_page.dart'; import 'device_managge.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'DMK-RP-01', theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.lightBlue), ), home: const MyHomePage(title: 'DMK-RP-01 上位机'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); // 这个 widget 是应用程序的主页。它是有状态的,这意味着它有一个 State 对象(在下面定义),其中包含影响其外观的字段。 // 这个类是状态的配置。它保存着由父级(在这种情况下是 App widget)提供的值(在这种情况下是标题), // 并由 State 的 build 方法使用。Widget 子类中的字段总是标记为 "final"。 final String title; @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { int _counter = 0; // 添加状态变量记录当前页面 int _currentPageIndex = 0; // 添加 GlobalKey final GlobalKey _scaffoldKey = GlobalKey(); void _incrementCounter() { setState(() { _counter++; }); } // 添加页面切换方法 void _changePage(int index) { setState(() { _currentPageIndex = index; }); Navigator.pop(context); } @override Widget build(BuildContext context) { Widget currentBody; switch (_currentPageIndex) { case 0: currentBody = Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text('You have pushed the button this many times:'), Text( '$_counter', style: Theme.of(context).textTheme.headlineMedium, ), ], ); break; case 1: currentBody = DisplayPage(); break; case 2: currentBody = MessagePage(); break; case 3: currentBody = LogPage(); break; default: currentBody = Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text('You have pushed the button this many times:'), Text( '$_counter', style: Theme.of(context).textTheme.headlineMedium, ), ], ); } return Scaffold( // 将 GlobalKey 赋值给 Scaffold key: _scaffoldKey, appBar: AppBar( leading: IconButton( icon: const Icon(Icons.menu), onPressed: () { // 使用 GlobalKey 打开导航栏 _scaffoldKey.currentState?.openDrawer(); }, ), actions: [ IconButton( icon: const Icon(Icons.settings), onPressed: () { showDialog( context: context, builder: (BuildContext context) { return const DeviceManagementDialog(); }, ); }, ), ], backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(widget.title), ), body: Center( child: currentBody, ), floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Increment', child: const Icon(Icons.add), ), // This trailing comma makes auto-formatting nicer for build methods. // 如果需要使用导航栏,需要添加 drawer 属性 drawer: Drawer( child: ListView( padding: EdgeInsets.zero, children: [ ListTile( title: Text('主页'), onTap: () => _changePage(0), ), ListTile( title: Text('展示'), onTap: () => _changePage(1), ), ListTile( title: Text('报文'), onTap: () => _changePage(2), ), ListTile( title: Text('日志'), onTap: () => _changePage(3), ), ListTile( title: Text('设置'), ) ], ), ), ); } }