自习室优化ok
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:app/utils/time.dart';
|
||||
import 'package:app/widgets/base/dialog/config_dialog.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../../../../widgets/room/core/count_down_vm.dart';
|
||||
import 'content_view.dart';
|
||||
import '../viewmodel/tch_room_vm.dart';
|
||||
|
||||
@@ -17,40 +17,12 @@ class StatusView extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _StatusViewState extends State<StatusView> {
|
||||
int _seconds = 0;
|
||||
Timer? _timer;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_timer?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _startCountDown(DateTime startTime) {
|
||||
// 避免重复计时器
|
||||
if (_timer != null) return;
|
||||
|
||||
final now = DateTime.now();
|
||||
int diff = startTime.difference(now).inSeconds;
|
||||
|
||||
if (diff <= 0) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_seconds = diff;
|
||||
});
|
||||
|
||||
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_seconds--;
|
||||
});
|
||||
|
||||
if (_seconds <= 0) {
|
||||
_timer?.cancel();
|
||||
_timer = null;
|
||||
}
|
||||
});
|
||||
void initState() {
|
||||
super.initState();
|
||||
final countVM = context.read<CountDownVM>();
|
||||
countVM.removeListener(_onCountDownEnd);
|
||||
countVM.addListener(_onCountDownEnd);
|
||||
}
|
||||
|
||||
///开播中返回拦截弹窗
|
||||
@@ -72,55 +44,65 @@ class _StatusViewState extends State<StatusView> {
|
||||
);
|
||||
}
|
||||
|
||||
///监听会议室倒计时结束的时候
|
||||
void _onCountDownEnd() {
|
||||
final countVM = context.read<CountDownVM>();
|
||||
if (countVM.endCountDown == 0) {
|
||||
EasyLoading.showToast("自习室已到结束时间,请记得关闭会议室");
|
||||
countVM.removeListener(_onCountDownEnd);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final vm = context.watch<TchRoomVM>();
|
||||
final tchVM = context.watch<TchRoomVM>();
|
||||
var roomStatus = tchVM.roomInfo.roomStatus;
|
||||
|
||||
/// 1. 未加载
|
||||
if (vm.roomStatus == -1) {
|
||||
if (roomStatus == -1) {
|
||||
return const Align(
|
||||
child: Text("加载中", style: TextStyle(color: Colors.white)),
|
||||
);
|
||||
}
|
||||
|
||||
/// 2. 未开始的房间
|
||||
if (vm.roomStatus == 0) {
|
||||
if (vm.canEnterRoom) {
|
||||
// 到时间了 → 自动开播
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
vm.toggleRoom(isOpen: true);
|
||||
});
|
||||
} else {
|
||||
// 没到时间 → 启动倒计时
|
||||
_startCountDown(parseTime(vm.roomInfo.startTime));
|
||||
}
|
||||
|
||||
return Align(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Text(
|
||||
"未到开播时间,到点后自动开播",
|
||||
style: TextStyle(color: Colors.white),
|
||||
),
|
||||
Container(
|
||||
margin: const EdgeInsets.symmetric(vertical: 10),
|
||||
child: Text(
|
||||
formatSeconds(_seconds),
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 26,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
if (roomStatus == 0) {
|
||||
return Consumer<CountDownVM>(
|
||||
builder: (_, countVM, __) {
|
||||
if (countVM.canEnterRoom) {
|
||||
tchVM.toggleRoom(isOpen: true);
|
||||
return SizedBox();
|
||||
} else {
|
||||
countVM.startStartCountdown();
|
||||
return Align(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Text(
|
||||
"未到开播时间,到点后自动开播",
|
||||
style: TextStyle(color: Colors.white),
|
||||
),
|
||||
Container(
|
||||
margin: const EdgeInsets.symmetric(vertical: 10),
|
||||
child: Text(
|
||||
formatSeconds(countVM.startCountDown),
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 26,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// 3. 已开播
|
||||
if (vm.roomStatus == 1) {
|
||||
if (roomStatus == 1) {
|
||||
return PopScope(
|
||||
canPop: false,
|
||||
onPopInvokedWithResult: (didPop, _) {
|
||||
|
||||
Reference in New Issue
Block a user