Browse Source

修复一些错误

heavyrain 3 months ago
parent
commit
b76d8cd413

+ 1 - 1
android/app/build.gradle

@@ -25,7 +25,7 @@ apply plugin: 'com.android.application'
 apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
 
 android {
-    compileSdkVersion 33
+    compileSdkVersion 34
     signingConfigs {
         release {
             storeFile file("sign/flutter_clock.jks")

+ 7 - 0
android/app/src/main/AndroidManifest.xml

@@ -1,5 +1,12 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="me.yoqi.flutter.flutter_clock">
+    <uses-permission android:name="android.permission.INTERNET" />
+    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
+    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
+    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
+    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
+    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
+    
    <application
         android:label="flutter_clock"
         android:icon="@mipmap/ic_launcher">

+ 3 - 3
lib/model/timer_settings.dart

@@ -6,10 +6,10 @@ class TimerSettings {
   static const String _vibrateKey = 'timer_vibrate';
   static const String _loopKey = 'timer_loop';
 
-  String sound;
+  String sound; 
   double volume;
-  bool vibrate;
-  bool loop;
+  bool vibrate; // 是否震动
+  bool loop;  // 是否循环
 
   TimerSettings({
     this.sound = 'Dripping',

+ 0 - 1
lib/pages/clock_page.dart

@@ -2,7 +2,6 @@ import 'dart:async';
 import 'dart:math' as math;
 
 import 'package:flutter/material.dart';
-import 'package:flutter_clock/model/config.dart';
 import 'package:intl/intl.dart';
 
 class ClockPage extends StatefulWidget {

+ 40 - 28
lib/pages/timer/timer_page.dart

@@ -4,6 +4,7 @@ import 'package:flutter_clock/model/timer_settings.dart';
 import 'package:flutter_clock/pages/timer/timer_settings_page.dart';
 import 'package:flutter_clock/utils/audio_manager.dart';
 import 'package:flutter_clock/utils/screen_manager.dart';
+
 /// Description: 倒计时页面
 /// Time       : 04/06/2025 Sunday
 /// Author     : liuyuqi.gov@msn.cn
@@ -12,6 +13,8 @@ class TimerPage extends StatefulWidget {
   _TimerPageState createState() => _TimerPageState();
 }
 
+enum TimerState { prepare, running, pause, finish }
+
 class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
   // Timer duration values
   int _hours = 0;
@@ -20,8 +23,7 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
 
   // For timer controller
   Timer? _timer;
-  bool _isRunning = false;
-  bool _isCompleted = false;
+  TimerState timerState = TimerState.prepare;
   int _remainingSeconds = 0;
 
   // Settings
@@ -60,7 +62,8 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
 
   @override
   void didChangeAppLifecycleState(AppLifecycleState state) {
-    if (state == AppLifecycleState.resumed && _isRunning) {
+    if (state == AppLifecycleState.resumed &&
+        timerState == TimerState.running) {
       _syncTimer();
     }
   }
@@ -72,22 +75,27 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
     });
   }
 
-  void _startTimer() {
-    final totalSeconds = _hours * 3600 + _minutes * 60 + _seconds;
-    if (totalSeconds <= 0) return;
-
-    setState(() {
-      _isRunning = true;
-      _isCompleted = false;
-      _remainingSeconds = totalSeconds;
-    });
+  /// resumed 暂停, 恢复
+  void _startTimer(bool resumed) {
+    if (resumed) {
+      setState(() {
+        timerState = TimerState.running;
+      });
+    } else {
+      final totalSeconds = _hours * 3600 + _minutes * 60 + _seconds;
+      if (totalSeconds <= 0) return;
 
+      setState(() {
+        timerState = TimerState.running;
+        _remainingSeconds = totalSeconds;
+      });
+    }
     _timer = Timer.periodic(Duration(seconds: 1), (timer) {
       setState(() {
         if (_remainingSeconds > 0) {
           _remainingSeconds--;
         } else {
-          _isCompleted = true;
+          timerState = TimerState.finish;
           _timerCompleted();
         }
       });
@@ -97,7 +105,7 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
   void _pauseTimer() {
     _timer?.cancel();
     setState(() {
-      _isRunning = false;
+      timerState = TimerState.pause;
     });
   }
 
@@ -106,18 +114,22 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
     _audioManager.stopSound();
     _audioManager.stopVibration();
     setState(() {
-      _isRunning = false;
-      _isCompleted = false;
+      timerState = TimerState.prepare;
     });
   }
 
   void _syncTimer() {
     // Recalculate elapsed time if app was in background
+    // final elapsedSeconds = _hours * 3600 + _minutes * 60 + _seconds -
+    //     _remainingSeconds;
+    // setState(() {
+    //   _remainingSeconds = elapsedSeconds;
+    // });
   }
 
   Future<void> _timerCompleted() async {
     _timer?.cancel();
-    _isRunning = false;
+    timerState = TimerState.finish;
 
     // Play sound and vibrate
     if (_settings.vibrate) {
@@ -130,8 +142,8 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
       Future.delayed(Duration(seconds: 5), () {
         _audioManager.stopSound();
         _audioManager.stopVibration();
-        _startTimer();
       });
+      _startTimer(false);
     }
   }
 
@@ -147,8 +159,8 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
     if (!_settingsLoaded) {
       return Center(child: CircularProgressIndicator());
     }
-    
-    if (_isRunning || _isCompleted) {
+
+    if (timerState != TimerState.prepare) {
       return _buildCountdownView();
     } else {
       return _buildTimerSetupView();
@@ -221,7 +233,7 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
                 _buildCircleButton(
                   Icons.play_arrow,
                   Colors.blue,
-                  () => _startTimer(),
+                  () => _startTimer(false),
                 ),
                 _buildCircleButton(
                   Icons.settings,
@@ -267,7 +279,7 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
                       shape: BoxShape.circle,
                       border: Border.all(
                         color: Colors.blue.withOpacity(0.3),
-                        width: 1,
+                        width: 3,
                       ),
                     ),
                     child: Stack(
@@ -278,11 +290,11 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
                           width: 300,
                           height: 300,
                           child: CircularProgressIndicator(
-                            value: _isCompleted
+                            value: timerState == TimerState.finish
                                 ? 1
                                 : _remainingSeconds /
                                     (_hours * 3600 + _minutes * 60 + _seconds),
-                            strokeWidth: 1,
+                            strokeWidth: 5,
                             backgroundColor: Colors.grey.withOpacity(0.1),
                             color: Colors.blue,
                           ),
@@ -307,8 +319,8 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
                         Positioned(
                           bottom: 0,
                           child: Container(
-                            width: 10,
-                            height: 10,
+                            width: 20,
+                            height: 20,
                             decoration: BoxDecoration(
                               color: Colors.blue,
                               shape: BoxShape.circle,
@@ -338,7 +350,7 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
                   isActive: ScreenManager.isWakeLockEnabled,
                 ),
                 // 暂停/开始
-                _isRunning
+                timerState == TimerState.running
                     ? _buildCircleButton(
                         Icons.pause,
                         Colors.blue,
@@ -347,7 +359,7 @@ class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
                     : _buildCircleButton(
                         Icons.play_arrow,
                         Colors.blue,
-                        () => _startTimer(),
+                        () => _startTimer(true),
                       ),
                 // 重置
                 _buildCircleButton(

+ 4 - 0
lib/pages/timer/timer_settings_page.dart

@@ -1,7 +1,11 @@
 import 'package:flutter/material.dart';
 import 'package:flutter_clock/model/timer_settings.dart';
 
+/// Description: 倒计时设置页面
+/// Time       : 04/22/2025 Tuesday
+/// Author     : liuyuqi.gov@msn.cn
 class TimerSettingsPage extends StatefulWidget {
+  
   final TimerSettings settings;
 
   const TimerSettingsPage({Key? key, required this.settings}) : super(key: key);

+ 2 - 3
lib/utils/audio_manager.dart

@@ -17,8 +17,7 @@ class AudioManager {
       await stopSound();
     }
 
-    // For this example, we're using dummy sound mapping
-    // In a real app, you would have actual sound files
+
     String soundAsset;
     switch (sound) {
       case 'Dripping':
@@ -49,7 +48,7 @@ class AudioManager {
   Future<void> triggerVibration() async {
     if (await Vibration.hasVibrator() ?? false) {
       // Vibrate continuously for 5 seconds
-      Vibration.vibrate(duration: 5000);
+      Vibration.vibrate(duration: 1000);
     }
   }