pile_nav_new/lib/main.dart
2024-11-15 17:42:52 +08:00

296 lines
10 KiB
Dart

import 'package:cpnav/controllers/appcontroller.dart';
import 'package:cpnav/pages/task/task_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:scence_map/controllers/controller.dart';
import 'appbar.dart';
import 'pages/aim_point/aimpoint_controller.dart';
import 'pages/history/history_record.dart';
import 'pages/login/login_page.dart';
import 'pages/login/my_routes.dart';
import 'pages/pass_track/view.dart';
import 'pages/real/index.dart';
import 'pages/real/real_controller.dart';
import 'pages/setting/child_pages/antenna/antenna_setting.dart';
import 'pages/setting/setting_controller.dart';
import 'pages/setting/setting_page.dart';
import 'pages/setting/child_pages/XyChange/connect.dart';
import 'pages/task/taskcontroller.dart';
import 'service/user/loginprefs.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:scence_map/controllers/plum_controller.dart';
import '../../controllers/gnss_controller.dart';
final AppController appcontroller = Get.put(AppController());
final GlobalKey<ScaffoldMessengerState> scaffoldMessengerKey =
GlobalKey<ScaffoldMessengerState>();
Connect connect = Connect();
void main() async {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []); //设置全屏
Get.put(SettingController());
Get.put(LoginPageController());
Get.put(AccountLoginController());
Get.put(PhoneLoginController());
Get.put(ScenceMapController());
Get.put(AimPointerController());
LoginPrefs loginPrefs = LoginPrefs();
String value =
await loginPrefs.init(); // await 关键字必须用在异步方法中 await等待异步方法执行完毕 异步方法必须用变量接收
if ('ok' == value) {
// connect.init();
// loginPrefs.removeToken(); //注释掉即能实现短时间二次登录不需要再重新登录
runApp(const MyApp());
}
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return Obx(() {
return MaterialApp(
scaffoldMessengerKey: scaffoldMessengerKey,
// title: 'Flutter Demo',
theme: appcontroller.isDarkMode.value
? ThemeData.dark()
: ThemeData.light(),
debugShowCheckedModeBanner: false,
onGenerateRoute: (settings) {
String str = routeBeforeHook(settings);
Widget page = routes[str]!;
return MaterialPageRoute(builder: (BuildContext context) => page);
},
home: const MyHomePage(),
initialRoute: "home",
localizationsDelegates: GlobalMaterialLocalizations.delegates,
supportedLocales: const [
Locale('zh', ''),
Locale('en', ''),
],
);
});
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
super.key,
});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late bool isPortrait;
late double appBarHeight = 34.0;
bool isInitialized = false;
@override
void initState() {
super.initState();
SettingController settingController = Get.put(SettingController());
SchedulerBinding.instance.addPostFrameCallback((_) async {
await loadData(settingController);
setState(() {
isInitialized = settingController.isInitialized;
Get.put(PlumDataController());
Get.put(AntennaController());
Get.put(TaskController());
Get.put(RealController());
Get.put(GnssController());
});
});
}
@override
Widget build(BuildContext context) {
if (!isInitialized) {
return const Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
}
final List<Widget> pages = [
// const Real(),
PassTrack(),
const TaskManagePage(),
const HistoryRecord(),
const SettingPortrait()
];
return Scaffold(
resizeToAvoidBottomInset: false,
drawerEdgeDragWidth: 0.0, // 禁止通过滑动打开drawer
appBar: PreferredSize(
preferredSize: Size.fromHeight(appBarHeight),
child: CustomAppBar(
appBarHeight: 56,
currentIndex: appcontroller.currentIndex,
),
),
body: OrientationBuilder(
builder: (context, orientation) {
// final size = MediaQuery.of(context).size;
appBarHeight = Orientation.portrait == orientation ? 56.0 : 34.0;
isPortrait = Orientation.portrait == orientation ? true : false;
appBarHeight = Orientation.portrait == orientation ? 56.0 : 34.0;
return Obx(() {
appcontroller.currentIndex.value;
if (!isPortrait) {
return Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: pages[appcontroller.currentIndex.value],
),
const VerticalDivider(
width: 1, // 设置高度为1
thickness: 1,
),
SizedBox(
width: 48,
child: SafeArea(
child: LayoutBuilder(builder: (context, constraints) {
// 根据屏幕宽度判断横屏或竖屏
// if (!isPortrait) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// buildIconButton(
// context: context,
// index: 0,
// icon: Icons.date_range_rounded,
// label: '实时',
// ),
// const Divider(),
buildIconButton(
context: context,
index: 0,
icon: Icons.map_outlined,
label: '桩点',
),
const Divider(),
buildIconButton(
context: context,
index: 1,
icon: Icons.my_location_sharp,
label: '任务',
),
const Divider(),
buildIconButton(
context: context,
index: 2,
icon: Icons.table_chart_outlined,
label: '历史',
),
const Divider(),
buildIconButton(
context: context,
index: 3,
icon: Icons.settings,
label: '设置',
),
]);
//
}),
),
)
]);
} //
else {
// 竖屏布局,保持原有底部导航栏样式
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
flex: 12,
child: pages[appcontroller.currentIndex.value],
),
// VerticalDivider(),
Expanded(
flex: 1,
child: SizedBox(
height: 48,
child: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: appcontroller.currentIndex.value,
onTap: (index) {
setState(() {
appcontroller.currentIndex.value = index;
});
},
items: const [
// BottomNavigationBarItem(
// icon: Icon(Icons.date_range_rounded),
// label: "实时"),
BottomNavigationBarItem(
icon: Icon(Icons.map_outlined), label: "计划"),
BottomNavigationBarItem(
icon: Icon(Icons.my_location_sharp),
label: "任务"),
BottomNavigationBarItem(
icon: Icon(Icons.table_chart_outlined),
label: "历史"),
BottomNavigationBarItem(
icon: Icon(Icons.settings), label: "设置"),
]),
),
)
]);
}
});
},
),
);
}
Widget buildIconButton({
required BuildContext context,
required int index,
required IconData icon,
required String label,
}) {
return InkWell(
onTap: () {
setState(() {
appcontroller.currentIndex.value = index;
});
},
child: Column(
children: [
Icon(
(icon),
color: appcontroller.currentIndex.value == index
? const Color.fromARGB(255, 60, 95, 123)
: Colors.grey,
),
Align(
alignment: Alignment.center,
child: Text(
label,
style: TextStyle(
fontSize: 12,
color: appcontroller.currentIndex.value == index
? const Color.fromARGB(255, 60, 95, 123)
: Colors.grey,
),
),
),
],
),
);
}
}