Files
BistroFrontend/lib/main.dart

219 lines
5.8 KiB
Dart
Raw Normal View History

import 'package:bistro/common/global.dart';
import 'package:bistro/router/router_table.dart';
2022-02-18 18:44:57 +08:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
2023-03-20 15:52:49 +08:00
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:flutter_web_plugins/url_strategy.dart';
2022-02-22 22:10:18 +08:00
// import 'package:flutter_localizations/flutter_localizations.dart';
import 'app.dart';
import 'life.dart';
import 'mine.dart';
import 'news.dart';
2022-02-18 18:44:57 +08:00
2022-02-22 22:10:18 +08:00
void main() {
// 保证核心已启动
2022-02-22 22:10:18 +08:00
WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
//
SystemChrome.setEnabledSystemUIMode(
SystemUiMode.edgeToEdge,
overlays: [SystemUiOverlay.bottom, SystemUiOverlay.top],
);
// 只允许竖屏
SystemChrome.setPreferredOrientations(
[
DeviceOrientation.portraitUp, // 竖屏 Portrait 模式
DeviceOrientation.portraitDown,
// DeviceOrientation.landscapeLeft, // 横屏 Landscape 模式
// DeviceOrientation.landscapeRight,
],
);
// web 使用 pathhistory路由
usePathUrlStrategy();
runApp(Bistro());
2022-02-22 22:10:18 +08:00
}
2022-02-18 18:44:57 +08:00
class Bistro extends StatelessWidget {
final GlobalKey<NavigatorState> navigationKey = GlobalKey<NavigatorState>();
Bistro({Key? key}) : super(key: key);
2022-02-18 18:44:57 +08:00
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '小酒馆',
2022-02-18 18:44:57 +08:00
theme: ThemeData(
fontFamily: "SongTiHeavy",
primaryColor: const Color(0xFFD56937),
primaryColorDark: const Color(0xFFD56937),
// appBarTheme: const AppBarTheme(color: Color(0xff384444)),
navigationBarTheme: const NavigationBarThemeData(
indicatorColor: Color(0xFFD56937),
labelTextStyle: MaterialStatePropertyAll(
TextStyle(
// color: Color(0xffd56937),
),
),
),
colorScheme: const ColorScheme.dark(
2023-03-27 16:48:10 +08:00
primary: Color(0xFFD56937),
secondary: Color(0xFFC8C2B4),
tertiary: Color(0xff384444),
background: Color(0xff2a2a22),
2023-03-27 16:48:10 +08:00
),
useMaterial3: true,
),
themeMode: ThemeMode.system,
home: const BistroFrame(
title: '小酒馆',
),
// 路由
// routes: RouterTable.routeTables,
navigatorKey: navigationKey,
onGenerateRoute: RouterTable.onGenerateRoute,
initialRoute: RouterTable.homePath,
2022-02-18 18:44:57 +08:00
);
}
}
class BistroFrame extends StatefulWidget {
const BistroFrame({Key? key, required this.title}) : super(key: key);
2022-02-18 18:44:57 +08:00
final String title;
@override
2023-03-20 15:52:49 +08:00
BistroFrameState createState() => BistroFrameState();
2022-02-18 18:44:57 +08:00
}
2023-03-20 15:52:49 +08:00
class BistroFrameState extends State<BistroFrame> {
late Widget _body;
late int _index = 2;
final List<int> loginRequired = [3];
static const List<Map<String, dynamic>> _iconList = [
{
2022-02-21 20:20:31 +08:00
"title": "满席",
"icon": Icons.storefront_outlined,
"selectedIcon": Icons.storefront_rounded,
},
{
"title": "道听",
"icon": Icons.forum_outlined,
"selectedIcon": Icons.forum_rounded,
},
{
"title": "途说",
"icon": Icons.supervisor_account_outlined,
"selectedIcon": Icons.supervisor_account_rounded,
},
{
2022-02-21 20:20:31 +08:00
"title": "在下",
"icon": Icons.person_outline,
"selectedIcon": Icons.person_rounded,
},
];
2022-02-18 18:44:57 +08:00
2022-02-22 22:10:18 +08:00
@override
initState() {
//页面初始化时要干的事
2022-02-22 22:10:18 +08:00
super.initState();
initialization();
Global.init(context);
2022-02-22 22:10:18 +08:00
}
2022-02-22 22:10:18 +08:00
void initData() {
_body = IndexedStack(
2023-03-20 15:52:49 +08:00
index: _index,
children: const <Widget>[
App(),
News(),
2023-03-20 15:52:49 +08:00
Life(),
Mine(),
],
);
2022-02-18 18:44:57 +08:00
}
2022-02-22 22:10:18 +08:00
void initialization() async {
// This is where you can initialize the resources needed by your app while
// the splash screen is displayed. Remove the following example because
// delaying the user experience is a bad design practice!
2023-03-20 15:52:49 +08:00
// if (kDebugMode) {
// print('ready in 3...');
// }
// await Future.delayed(const Duration(seconds: 1));
// if (kDebugMode) {
// print('ready in 2...');
// }
// await Future.delayed(const Duration(seconds: 1));
// await Future.delayed(const Duration(seconds: 1));
// printWhenDebug('go!');
FlutterNativeSplash.remove();
2022-02-22 22:10:18 +08:00
}
2022-02-18 18:44:57 +08:00
@override
Widget build(BuildContext context) {
initData();
List<NavigationDestination> bottomNavigationBarData = [];
for (var i = 0; i < 4; i++) {
NavigationDestination itemWidget = bottomAppBarItem(index: i);
bottomNavigationBarData.add(itemWidget);
}
2022-02-18 18:44:57 +08:00
return Scaffold(
appBar: AppBar(
title: Text(_iconList[_index]['title']),
systemOverlayStyle: const SystemUiOverlayStyle(
statusBarColor: Colors.transparent, //设置为透明
),
),
body: _body,
floatingActionButton: FloatingActionButton(
enableFeedback: true,
2023-03-20 15:52:49 +08:00
backgroundColor: Theme.of(context).colorScheme.background,
onPressed: () => {},
child: Icon(
Icons.search,
color: Theme.of(context).primaryColor,
),
2022-02-18 18:44:57 +08:00
),
bottomNavigationBar: NavigationBar(
selectedIndex: _index,
destinations: bottomNavigationBarData,
onDestinationSelected: (int index) {
setState(() {
_index = index;
});
loginRequired.forEach((index) {
if (_index == index) {
Navigator.of(context).pushNamed(RouterTable.loginPath);
}
});
},
),
);
}
NavigationDestination bottomAppBarItem({
required int index, // 序列
}) {
Map<String, dynamic> item = _iconList[index];
NavigationDestination child = NavigationDestination(
icon: Icon(item['icon']),
label: item["title"],
selectedIcon: Icon(item['selectedIcon']),
);
return child;
2022-02-18 18:44:57 +08:00
}
}