WGShare.Mobile.Flutter/wgshare/lib/pages/start_page.dart

150 lines
4.3 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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/utils/storage.dart';
import 'package:wgshare/utils/toast_utils.dart';
import 'package:wgshare/routes/app_routes.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(),
];
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(
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
label: '首页',
icon: getItemIcon('assets/images/ic_home_normal.png'),
activeIcon: getItemIcon('assets/images/ic_home_press.png'),
),
BottomNavigationBarItem(
label: '我的',
icon: getItemIcon('assets/images/ic_mine_normal.png'),
activeIcon: getItemIcon('assets/images/ic_mine_press.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());
}
}