151 lines
4.6 KiB
Dart
151 lines
4.6 KiB
Dart
import 'dart:async';
|
||
|
||
import 'package:flutter/material.dart';
|
||
import 'package:flutter/widgets.dart';
|
||
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:starcloud/sdk/entity/cloud_user_info.dart';
|
||
import 'package:starcloud/sdk/starcloud.dart';
|
||
import 'package:starwork_flutter/api/model/team/request/change_current_team_request.dart';
|
||
import 'package:starwork_flutter/api/model/team/response/team_info_response.dart';
|
||
import 'package:starwork_flutter/api/service/team_api_service.dart';
|
||
import 'package:starwork_flutter/base/app_logger.dart';
|
||
import 'package:starwork_flutter/base/base_controller.dart';
|
||
import 'package:starwork_flutter/common/constant/app_images.dart';
|
||
import 'package:starwork_flutter/routes/app_routes.dart';
|
||
import 'package:starwork_flutter/views/home/home_controller.dart';
|
||
import 'package:starwork_flutter/views/home/home_view.dart';
|
||
import 'package:starwork_flutter/views/messages/messages_view.dart';
|
||
import 'package:starwork_flutter/views/mine/mine_view.dart';
|
||
|
||
class MainController extends BaseController {
|
||
final teamApi = Get.find<TeamApiService>();
|
||
|
||
GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
|
||
|
||
// 当前选中的索引
|
||
var currentIndex = 0.obs;
|
||
|
||
// 页面列表
|
||
final pages = [
|
||
HomeView(),
|
||
MessagesView(),
|
||
MineView(),
|
||
];
|
||
|
||
// 我的团队列表
|
||
final myTeamList = <TeamInfoResponse>[].obs;
|
||
|
||
// 选中的团队
|
||
var selectedTeam = TeamInfoResponse().obs;
|
||
|
||
@override
|
||
void onInit() {
|
||
/// 请求团队信息
|
||
requestAllTeamInfoList();
|
||
super.onInit();
|
||
}
|
||
|
||
// 更新索引
|
||
void changeIndex(int index) {
|
||
currentIndex.value = index;
|
||
}
|
||
|
||
// 定义底部导航的标题和图标
|
||
List<BottomNavigationBarItem> get bottomNavItems => [
|
||
BottomNavigationBarItem(
|
||
icon: _buildTabIcon(AppImages.iconHome, AppImages.iconHomeSelected, 0),
|
||
label: '首页',
|
||
),
|
||
BottomNavigationBarItem(
|
||
icon: _buildTabIcon(AppImages.iconNotification, AppImages.iconNotificationSelected, 1),
|
||
label: '消息',
|
||
),
|
||
BottomNavigationBarItem(
|
||
icon: _buildTabIcon(AppImages.iconMine, AppImages.iconMineSelected, 2),
|
||
label: '我的',
|
||
),
|
||
];
|
||
|
||
// 构建自定义图标组件
|
||
Widget _buildTabIcon(String unselectedIcon, String selectedIcon, int index) {
|
||
return Obx(() => Image.asset(
|
||
currentIndex.value == index ? selectedIcon : unselectedIcon,
|
||
width: 24.w,
|
||
height: 24.w,
|
||
fit: BoxFit.contain,
|
||
));
|
||
}
|
||
|
||
// 打开抽屉的方法
|
||
void openDrawer() {
|
||
try {
|
||
final scaffoldState = scaffoldKey.currentState;
|
||
if (scaffoldState != null && scaffoldState.hasDrawer) {
|
||
scaffoldState.openDrawer();
|
||
} else {
|
||
print('Cannot open drawer: Scaffold state is null or has no drawer');
|
||
}
|
||
} catch (e) {
|
||
print('Error opening drawer: $e');
|
||
}
|
||
}
|
||
|
||
// 关闭抽屉的方法
|
||
void closeDrawer() {
|
||
try {
|
||
final scaffoldState = scaffoldKey.currentState;
|
||
if (scaffoldState != null && scaffoldState.isDrawerOpen) {
|
||
scaffoldState.closeDrawer();
|
||
}
|
||
} catch (e) {
|
||
print('Error closing drawer: $e');
|
||
}
|
||
}
|
||
|
||
/// 请求团队信息
|
||
void requestAllTeamInfoList() async {
|
||
var teamList = await teamApi.requestAllTeamInfoList();
|
||
if (teamList.isSuccess) {
|
||
// 我的团队信息
|
||
myTeamList.value = teamList.data?.myTeam ?? [];
|
||
myTeamList.refresh();
|
||
// 待加入团队信息
|
||
List<TeamInfoResponse> pendTeamList = teamList.data?.pendTeam ?? [];
|
||
if (myTeamList.isEmpty) {
|
||
// 如果是空的则自动创建并跳转到团队使用场景页面
|
||
Get.toNamed(AppRoutes.teamUseCaseSetting);
|
||
}
|
||
selectedTeam.value = myTeamList.first;
|
||
}
|
||
}
|
||
|
||
/// 切换当前团队
|
||
void requestChangeCurrentTeam(TeamInfoResponse teamInfo) async {
|
||
var teamNo = teamInfo.teamNo;
|
||
if (teamNo == null) {
|
||
return;
|
||
}
|
||
var changeCurrentTeamResponse = await teamApi.requestChangeCurrentTeam(
|
||
request: ChangeCurrentTeamRequest(
|
||
teamNo: teamNo,
|
||
),
|
||
);
|
||
if (changeCurrentTeamResponse.isSuccess) {
|
||
AppLogger.highlight('切换当前团队成功');
|
||
selectedTeam.value = teamInfo;
|
||
|
||
// 如果teamInfo中的cloudInfo不为空,则设置云账号
|
||
if (teamInfo.cloudInfo != null) {
|
||
StarCloudSDK.instance.setCloudAccounts([
|
||
CloudUserInfo(
|
||
username: teamInfo.cloudInfo!.username!,
|
||
password: teamInfo.cloudInfo!.password!,
|
||
uid: teamInfo.cloudInfo!.uid!,
|
||
)
|
||
]);
|
||
}
|
||
}
|
||
}
|
||
}
|