From eb82f35a340e1650ac6555a815be13c0caeb80b1 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 2 Jun 2024 15:43:42 +0800 Subject: [PATCH] :bug: Fix call lagging issue --- lib/screens/channel/call/call.dart | 33 +++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/screens/channel/call/call.dart b/lib/screens/channel/call/call.dart index 201c06f..9d632d4 100644 --- a/lib/screens/channel/call/call.dart +++ b/lib/screens/channel/call/call.dart @@ -16,6 +16,9 @@ class CallScreen extends StatefulWidget { } class _CallScreenState extends State { + Timer? timer; + String currentDuration = '00:00:00'; + String parseDuration() { final ChatCallProvider provider = Get.find(); if (provider.current.value == null) return '00:00:00'; @@ -27,17 +30,21 @@ class _CallScreenState extends State { "${twoDigits(duration.inMinutes.remainder(60))}:" "${twoDigits(duration.inSeconds.remainder(60))}"; - Timer.periodic(const Duration(seconds: 1), (_) { - setState(() {}); - }); - return formattedTime; } + void updateDuration() { + setState(() { + currentDuration = parseDuration(); + }); + } + @override void initState() { Get.find().setupRoom(); super.initState(); + + timer = Timer.periodic(const Duration(seconds: 1), (_) => updateDuration()); } @override @@ -57,7 +64,7 @@ class _CallScreenState extends State { ), const TextSpan(text: "\n"), TextSpan( - text: parseDuration(), + text: currentDuration, style: Theme.of(context).textTheme.bodySmall, ), ]), @@ -137,4 +144,20 @@ class _CallScreenState extends State { ), ); } + + @override + void deactivate() { + timer?.cancel(); + timer = null; + super.deactivate(); + } + + @override + void activate() { + timer ??= Timer.periodic( + const Duration(seconds: 1), + (_) => updateDuration(), + ); + super.activate(); + } }