RP-01_Flutter/lib/main.dart
2025-04-30 17:01:25 +08:00

165 lines
4.5 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
// 添加状态变量记录当前页面
int _currentPageIndex = 0;
// 添加 GlobalKey
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
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: <Widget>[
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: <Widget>[
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: <Widget>[
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('设置'),
)
],
),
),
);
}
}