refactor(router): 改变了路由方式
从路由配置文件改为了路由表
This commit is contained in:
153
lib/main.dart
153
lib/main.dart
@@ -1,28 +1,47 @@
|
||||
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';
|
||||
import 'router/router.dart';
|
||||
|
||||
void main() {
|
||||
// 保证核心已启动
|
||||
WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
|
||||
FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
|
||||
|
||||
//
|
||||
SystemChrome.setEnabledSystemUIMode(
|
||||
SystemUiMode.edgeToEdge,
|
||||
overlays: [SystemUiOverlay.bottom, SystemUiOverlay.top],
|
||||
);
|
||||
FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
|
||||
runApp(const Bistro());
|
||||
|
||||
// 只允许竖屏
|
||||
SystemChrome.setPreferredOrientations(
|
||||
[
|
||||
DeviceOrientation.portraitUp, // 竖屏 Portrait 模式
|
||||
DeviceOrientation.portraitDown,
|
||||
// DeviceOrientation.landscapeLeft, // 横屏 Landscape 模式
|
||||
// DeviceOrientation.landscapeRight,
|
||||
],
|
||||
);
|
||||
|
||||
// web 使用 path(history)路由
|
||||
usePathUrlStrategy();
|
||||
|
||||
runApp(Bistro());
|
||||
}
|
||||
|
||||
class Bistro extends StatelessWidget {
|
||||
const Bistro({Key? key}) : super(key: key);
|
||||
final GlobalKey<NavigatorState> navigationKey = GlobalKey<NavigatorState>();
|
||||
|
||||
Bistro({Key? key}) : super(key: key);
|
||||
|
||||
// This widget is the root of your application.
|
||||
@override
|
||||
@@ -33,37 +52,32 @@ class Bistro extends StatelessWidget {
|
||||
fontFamily: "SongTiHeavy",
|
||||
primaryColor: const Color(0xFFD56937),
|
||||
primaryColorDark: const Color(0xFFD56937),
|
||||
scaffoldBackgroundColor: const Color(0xFF939D99),
|
||||
appBarTheme: const AppBarTheme(color: Color(0xff384444)),
|
||||
// appBarTheme: const AppBarTheme(color: Color(0xff384444)),
|
||||
navigationBarTheme: const NavigationBarThemeData(
|
||||
surfaceTintColor: Color(0xffd56937),
|
||||
backgroundColor:Color(0xff2a2a22),
|
||||
indicatorColor: Color(0x00000000),
|
||||
labelTextStyle: MaterialStatePropertyAll(TextStyle(
|
||||
color: Color(0xffd56937),
|
||||
)),
|
||||
indicatorColor: Color(0xFFD56937),
|
||||
labelTextStyle: MaterialStatePropertyAll(
|
||||
TextStyle(
|
||||
// color: Color(0xffd56937),
|
||||
),
|
||||
),
|
||||
),
|
||||
colorScheme: const ColorScheme.dark(
|
||||
primary: Color(0xFFD56937),
|
||||
secondary: Color(0xFFC8C2B4),
|
||||
background: Color(0xff384444),
|
||||
tertiary: Color(0xff384444),
|
||||
background: Color(0xff2a2a22),
|
||||
),
|
||||
useMaterial3: true,
|
||||
),
|
||||
themeMode: ThemeMode.system,
|
||||
home: const BistroFrame(
|
||||
title: '小酒馆',
|
||||
),
|
||||
// 国际化
|
||||
// localizationsDelegates: [
|
||||
// GlobalMaterialLocalizations.delegate,
|
||||
// GlobalWidgetsLocalizations.delegate,
|
||||
// ],
|
||||
// supportedLocales: const [
|
||||
// Locale('zh', 'CN'),
|
||||
// ],
|
||||
// locale: const Locale('zh'),
|
||||
// 路由
|
||||
routes: router,
|
||||
// routes: RouterTable.routeTables,
|
||||
navigatorKey: navigationKey,
|
||||
onGenerateRoute: RouterTable.onGenerateRoute,
|
||||
initialRoute: RouterTable.homePath,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -79,31 +93,29 @@ class BistroFrame extends StatefulWidget {
|
||||
|
||||
class BistroFrameState extends State<BistroFrame> {
|
||||
late Widget _body;
|
||||
int _index = 3;
|
||||
late int _index = 2;
|
||||
final List<int> loginRequired = [3];
|
||||
|
||||
static const List<Map<String, dynamic>> _iconList = [
|
||||
{
|
||||
"title": "满席",
|
||||
"icon": Icons.storefront_outlined,
|
||||
"display": true,
|
||||
"selectedIcon": Icons.storefront_rounded,
|
||||
},
|
||||
{
|
||||
"title": "道听",
|
||||
"icon": Icons.forum_outlined,
|
||||
"display": true,
|
||||
},
|
||||
{
|
||||
"display": false,
|
||||
"selectedIcon": Icons.forum_rounded,
|
||||
},
|
||||
{
|
||||
"title": "途说",
|
||||
"icon": Icons.supervisor_account_outlined,
|
||||
"display": true,
|
||||
"selectedIcon": Icons.supervisor_account_rounded,
|
||||
},
|
||||
{
|
||||
"title": "在下",
|
||||
"icon": Icons.person_outline,
|
||||
"display": true,
|
||||
"selectedIcon": Icons.person_rounded,
|
||||
},
|
||||
];
|
||||
|
||||
@@ -113,6 +125,7 @@ class BistroFrameState extends State<BistroFrame> {
|
||||
super.initState();
|
||||
|
||||
initialization();
|
||||
Global.init(context);
|
||||
}
|
||||
|
||||
void initData() {
|
||||
@@ -121,7 +134,6 @@ class BistroFrameState extends State<BistroFrame> {
|
||||
children: const <Widget>[
|
||||
App(),
|
||||
News(),
|
||||
News(),
|
||||
Life(),
|
||||
Mine(),
|
||||
],
|
||||
@@ -140,8 +152,8 @@ class BistroFrameState extends State<BistroFrame> {
|
||||
// print('ready in 2...');
|
||||
// }
|
||||
// await Future.delayed(const Duration(seconds: 1));
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
printWhenDebug('go!');
|
||||
// await Future.delayed(const Duration(seconds: 1));
|
||||
// printWhenDebug('go!');
|
||||
FlutterNativeSplash.remove();
|
||||
}
|
||||
|
||||
@@ -149,25 +161,20 @@ class BistroFrameState extends State<BistroFrame> {
|
||||
Widget build(BuildContext context) {
|
||||
initData();
|
||||
|
||||
List<Widget> bottomNavigationBarData = [];
|
||||
for (var i = 0; i < 5; i++) {
|
||||
Widget itemWidget = bottomAppBarItem(index: i);
|
||||
List<NavigationDestination> bottomNavigationBarData = [];
|
||||
for (var i = 0; i < 4; i++) {
|
||||
NavigationDestination itemWidget = bottomAppBarItem(index: i);
|
||||
bottomNavigationBarData.add(itemWidget);
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
body: _body,
|
||||
bottomNavigationBar: NavigationBar(
|
||||
selectedIndex: _index,
|
||||
destinations: bottomNavigationBarData,
|
||||
// backgroundColor: Theme.of(context).colorScheme.primary,
|
||||
onDestinationSelected: (index) => {
|
||||
setState(() {
|
||||
_index = index;
|
||||
})
|
||||
},
|
||||
labelBehavior: NavigationDestinationLabelBehavior.onlyShowSelected,
|
||||
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,
|
||||
@@ -177,40 +184,34 @@ class BistroFrameState extends State<BistroFrame> {
|
||||
color: Theme.of(context).primaryColor,
|
||||
),
|
||||
),
|
||||
floatingActionButtonLocation:
|
||||
FloatingActionButtonLocation.miniCenterDocked,
|
||||
bottomNavigationBar: NavigationBar(
|
||||
selectedIndex: _index,
|
||||
destinations: bottomNavigationBarData,
|
||||
onDestinationSelected: (int index) {
|
||||
setState(() {
|
||||
_index = index;
|
||||
});
|
||||
|
||||
loginRequired.forEach((index) {
|
||||
if (_index == index) {
|
||||
Navigator.of(context).pushNamed(RouterTable.loginPath);
|
||||
}
|
||||
});
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget bottomAppBarItem({
|
||||
NavigationDestination bottomAppBarItem({
|
||||
required int index, // 序列
|
||||
}) {
|
||||
Map<String, dynamic> item = _iconList[index];
|
||||
|
||||
//设置默认未选中的状态
|
||||
// TextStyle style = TextStyle(
|
||||
// fontSize: size,
|
||||
// color: color,
|
||||
// );
|
||||
|
||||
bool isShow = item["display"];
|
||||
|
||||
Widget child;
|
||||
if (!isShow) {
|
||||
child = Container();
|
||||
} else {
|
||||
child = NavigationDestination(
|
||||
icon: Icon(
|
||||
item['icon'],
|
||||
color: Theme.of(context).colorScheme.secondary,
|
||||
),
|
||||
label: item["title"],
|
||||
selectedIcon: Icon(
|
||||
item['icon'],
|
||||
color: Theme.of(context).primaryColor,
|
||||
),
|
||||
);
|
||||
}
|
||||
NavigationDestination child = NavigationDestination(
|
||||
icon: Icon(item['icon']),
|
||||
label: item["title"],
|
||||
selectedIcon: Icon(item['selectedIcon']),
|
||||
);
|
||||
|
||||
return child;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user