84 lines
1.9 KiB
Dart
84 lines
1.9 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:app/utils/time.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:remixicon/remixicon.dart';
|
|
|
|
import '../viewmodel/stu_room_vm.dart';
|
|
|
|
class TopBar extends StatefulWidget implements PreferredSizeWidget {
|
|
final bool showOther;
|
|
final void Function()? onOther;
|
|
|
|
const TopBar({
|
|
super.key,
|
|
this.showOther = false,
|
|
this.onOther,
|
|
});
|
|
|
|
@override
|
|
State<TopBar> createState() => _TopBarState();
|
|
|
|
@override
|
|
Size get preferredSize => const Size.fromHeight(kToolbarHeight);
|
|
}
|
|
|
|
class _TopBarState extends State<TopBar> {
|
|
Timer? _timer;
|
|
int seconds = 0;
|
|
late DateTime startTime;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
final vm = context.read<StuRoomVM>();
|
|
startTime = parseTime(vm.roomInfo.startTime);
|
|
|
|
_timer = Timer.periodic(const Duration(seconds: 1), (_) {
|
|
final diff = DateTime.now().difference(startTime).inSeconds;
|
|
setState(() {
|
|
seconds = diff < 0 ? 0 : diff;
|
|
});
|
|
});
|
|
}
|
|
|
|
/// 你若想外面主动停,可以暴露这个方法
|
|
void stopTimer() {
|
|
_timer?.cancel();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_timer?.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final vm = context.read<StuRoomVM>();
|
|
|
|
return AppBar(
|
|
foregroundColor: Colors.white,
|
|
titleTextStyle: const TextStyle(color: Colors.white, fontSize: 18),
|
|
backgroundColor: const Color(0xff232426),
|
|
centerTitle: true,
|
|
title: Column(
|
|
children: [
|
|
Text(vm.roomInfo.roomName),
|
|
Text(
|
|
formatSeconds(seconds),
|
|
style: const TextStyle(fontSize: 12, color: Colors.white24),
|
|
),
|
|
],
|
|
),
|
|
actions: [
|
|
IconButton(
|
|
onPressed: widget.onOther,
|
|
icon: Icon(widget.showOther ? RemixIcons.team_fill : RemixIcons.team_line),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|