151 lines
4.3 KiB
Dart
151 lines
4.3 KiB
Dart
import 'dart:async';
|
||
|
||
import 'package:flutter/material.dart';
|
||
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:wgshare/common/mixins/request_tool_mixin.dart';
|
||
import 'package:wgshare/common/models/user_info_detail.dart';
|
||
import 'package:wgshare/common/store/user_store.dart';
|
||
import 'package:wgshare/pages/homePage/home_logic.dart';
|
||
import 'package:wgshare/pages/minePage/mine_page.dart';
|
||
import 'package:wgshare/utils/toast_utils.dart';
|
||
|
||
import 'homePage/home_view.dart';
|
||
import 'minePage/mine_logic.dart';
|
||
|
||
class StartPage extends StatefulWidget {
|
||
const StartPage({super.key});
|
||
|
||
@override
|
||
State<StartPage> createState() => _StartPageState();
|
||
}
|
||
|
||
class _StartPageState extends State<StartPage> with RequestToolMixin {
|
||
DateTime? lastPopTime;
|
||
final _pageController = Get.find<PageIndexController>();
|
||
|
||
late final List<Widget> _bodyList;
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
Get.put(HomeLogic());
|
||
Get.put(MineLogic());
|
||
|
||
_bodyList = [
|
||
const HomePage(),
|
||
const MinePage(),
|
||
];
|
||
|
||
String? token = UserStore.to.token;
|
||
UserInfoDetail? userInfoDetail = UserStore.to.userDetailInfo.value;
|
||
|
||
if ((token?.isNotEmpty ?? false) && userInfoDetail != null) {
|
||
UserStore.to.updateUserInfo(); // 更新用户信息
|
||
} else {
|
||
/// 没有登录返回登录页
|
||
Future.delayed(const Duration(milliseconds: 100)).then((e) {
|
||
// UserStore.to.erase();
|
||
// StorageService.to.erase();
|
||
// Get.offAllNamed(Routes.login);
|
||
});
|
||
}
|
||
}
|
||
|
||
@override
|
||
void dispose() {
|
||
Get.delete<PageIndexController>();
|
||
super.dispose();
|
||
}
|
||
|
||
/// 获取项目 icon
|
||
Widget getItemIcon(String icon) {
|
||
return Image.asset(
|
||
icon,
|
||
fit: BoxFit.contain,
|
||
width: 24.w,
|
||
height: 24.h,
|
||
);
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return WillPopScope(
|
||
child: Scaffold(
|
||
body: PageView(
|
||
controller: _pageController._pageIndexState.pageController,
|
||
physics: const BouncingScrollPhysics(),
|
||
onPageChanged: (index) {
|
||
_pageController._pageIndexState.pageIndex.value = index;
|
||
},
|
||
children: _bodyList,
|
||
),
|
||
bottomNavigationBar: Obx(() {
|
||
return BottomNavigationBar(
|
||
iconSize: 24.sp,
|
||
items: <BottomNavigationBarItem>[
|
||
BottomNavigationBarItem(
|
||
label: '首页',
|
||
icon: getItemIcon('assets/images/home_no_active_icon.png'),
|
||
activeIcon: getItemIcon('assets/images/home_icon.png'),
|
||
),
|
||
BottomNavigationBarItem(
|
||
label: '我的',
|
||
icon: getItemIcon('assets/images/mine_no_active_icon.png'),
|
||
activeIcon: getItemIcon('assets/images/mine_icon.png'),
|
||
),
|
||
],
|
||
//设置显示的模式
|
||
type: BottomNavigationBarType.fixed,
|
||
//设置当前的索引
|
||
currentIndex: _pageController._pageIndexState.pageIndex.value,
|
||
//tabBottom的点击监听
|
||
onTap: (index) {
|
||
print('appbar下标:${index}');
|
||
_pageController._pageIndexState.pageController.jumpToPage(index);
|
||
},
|
||
);
|
||
}),
|
||
),
|
||
onWillPop: () async {
|
||
if (lastPopTime == null || DateTime.now().difference(lastPopTime!) > const Duration(seconds: 1)) {
|
||
lastPopTime = DateTime.now();
|
||
ToastUtils.getFluttertoast(context: context, msg: '连续两次返回就退出');
|
||
return Future.value(false);
|
||
} else {
|
||
lastPopTime = DateTime.now();
|
||
// 退出app
|
||
return Future.value(true);
|
||
}
|
||
},
|
||
);
|
||
}
|
||
}
|
||
|
||
class PageIndexState {
|
||
PageIndexState({required this.pageController, this.showUpgrade = false});
|
||
|
||
RxInt pageIndex = 0.obs;
|
||
bool showUpgrade;
|
||
|
||
//文本输入框控制器
|
||
final PageController pageController;
|
||
}
|
||
|
||
class PageIndexController extends GetxController with RequestToolMixin {
|
||
late PageIndexState _pageIndexState;
|
||
|
||
@override
|
||
void onInit() {
|
||
_pageIndexState = PageIndexState(pageController: PageController());
|
||
super.onInit();
|
||
}
|
||
}
|
||
|
||
class StartPageIndexBinding extends Bindings {
|
||
@override
|
||
void dependencies() {
|
||
Get.lazyPut(() => PageIndexController());
|
||
}
|
||
}
|