219 lines
5.8 KiB
Dart
219 lines
5.8 KiB
Dart
import 'package:bistro/common/global.dart';
|
||
import 'package:bistro/router/router_table.dart';
|
||
import 'package:flutter/material.dart';
|
||
import 'package:flutter/services.dart';
|
||
import 'package:flutter_native_splash/flutter_native_splash.dart';
|
||
import 'package:flutter_web_plugins/url_strategy.dart';
|
||
|
||
// import 'package:flutter_localizations/flutter_localizations.dart';
|
||
import 'app.dart';
|
||
import 'life.dart';
|
||
import 'mine.dart';
|
||
import 'news.dart';
|
||
|
||
void main() {
|
||
// 保证核心已启动
|
||
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 使用 path(history)路由
|
||
usePathUrlStrategy();
|
||
|
||
runApp(Bistro());
|
||
}
|
||
|
||
class Bistro extends StatelessWidget {
|
||
final GlobalKey<NavigatorState> navigationKey = GlobalKey<NavigatorState>();
|
||
|
||
Bistro({Key? key}) : super(key: key);
|
||
|
||
// This widget is the root of your application.
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return MaterialApp(
|
||
title: '小酒馆',
|
||
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(
|
||
primary: Color(0xFFD56937),
|
||
secondary: Color(0xFFC8C2B4),
|
||
tertiary: Color(0xff384444),
|
||
background: Color(0xff2a2a22),
|
||
),
|
||
useMaterial3: true,
|
||
),
|
||
themeMode: ThemeMode.system,
|
||
home: const BistroFrame(
|
||
title: '小酒馆',
|
||
),
|
||
// 路由
|
||
// routes: RouterTable.routeTables,
|
||
navigatorKey: navigationKey,
|
||
onGenerateRoute: RouterTable.onGenerateRoute,
|
||
initialRoute: RouterTable.homePath,
|
||
);
|
||
}
|
||
}
|
||
|
||
class BistroFrame extends StatefulWidget {
|
||
const BistroFrame({Key? key, required this.title}) : super(key: key);
|
||
|
||
final String title;
|
||
|
||
@override
|
||
BistroFrameState createState() => BistroFrameState();
|
||
}
|
||
|
||
class BistroFrameState extends State<BistroFrame> {
|
||
late Widget _body;
|
||
late int _index = 2;
|
||
final List<int> loginRequired = [3];
|
||
|
||
static const List<Map<String, dynamic>> _iconList = [
|
||
{
|
||
"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,
|
||
},
|
||
{
|
||
"title": "在下",
|
||
"icon": Icons.person_outline,
|
||
"selectedIcon": Icons.person_rounded,
|
||
},
|
||
];
|
||
|
||
@override
|
||
initState() {
|
||
//页面初始化时要干的事
|
||
super.initState();
|
||
|
||
initialization();
|
||
Global.init(context);
|
||
}
|
||
|
||
void initData() {
|
||
_body = IndexedStack(
|
||
index: _index,
|
||
children: const <Widget>[
|
||
App(),
|
||
News(),
|
||
Life(),
|
||
Mine(),
|
||
],
|
||
);
|
||
}
|
||
|
||
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!
|
||
// 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();
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
initData();
|
||
|
||
List<NavigationDestination> bottomNavigationBarData = [];
|
||
for (var i = 0; i < 4; i++) {
|
||
NavigationDestination itemWidget = bottomAppBarItem(index: i);
|
||
bottomNavigationBarData.add(itemWidget);
|
||
}
|
||
|
||
return Scaffold(
|
||
appBar: AppBar(
|
||
title: Text(_iconList[_index]['title']),
|
||
systemOverlayStyle: const SystemUiOverlayStyle(
|
||
statusBarColor: Colors.transparent, //设置为透明
|
||
),
|
||
),
|
||
body: _body,
|
||
floatingActionButton: FloatingActionButton(
|
||
enableFeedback: true,
|
||
backgroundColor: Theme.of(context).colorScheme.background,
|
||
onPressed: () => {},
|
||
child: Icon(
|
||
Icons.search,
|
||
color: Theme.of(context).primaryColor,
|
||
),
|
||
),
|
||
bottomNavigationBar: NavigationBar(
|
||
selectedIndex: _index,
|
||
destinations: bottomNavigationBarData,
|
||
onDestinationSelected: (int index) {
|
||
setState(() {
|
||
_index = index;
|
||
});
|
||
|
||
for (var index in loginRequired) {
|
||
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;
|
||
}
|
||
}
|