Browse Source

设置屏幕大小

heavyrain 3 months ago
parent
commit
091bb036ed
4 changed files with 81 additions and 16 deletions
  1. 13 7
      lib/index_page.dart
  2. 42 9
      lib/main.dart
  3. 24 0
      pubspec.lock
  4. 2 0
      pubspec.yaml

+ 13 - 7
lib/index_page.dart

@@ -6,6 +6,7 @@ import 'package:flutter_clock/pages/alarm_page.dart';
 import 'package:flutter_clock/pages/clock_page.dart';
 import 'package:flutter_clock/pages/stopwatch_page.dart';
 import 'package:flutter_clock/pages/timer/timer_page.dart';
+import 'package:flutter_screenutil/flutter_screenutil.dart';
 
 /// Description: main page
 /// Time       : 04/06/2025 Sunday
@@ -20,12 +21,7 @@ class IndexPage extends StatefulWidget {
 class _IndexPageState extends State<IndexPage>
     with SingleTickerProviderStateMixin {
   late TabController _tabController;
-  final List<Widget> _tabs = [
-    Tab(child: Text('Alarm', style: TextStyle(fontSize: 16))),
-    Tab(child: Text('Clock', style: TextStyle(fontSize: 16))),
-    Tab(child: Text('Stopwatch', style: TextStyle(fontSize: 16))),
-    Tab(child: Text('Timer', style: TextStyle(fontSize: 16))),
-  ];
+  late List<Widget> _tabs;
 
   final _utcMidnightRadiansOffset = radiansFromDegrees(-64);
   static const _secondsInDay = 86400;
@@ -38,6 +34,15 @@ class _IndexPageState extends State<IndexPage>
         length: 4,
         vsync: this,
         initialIndex: 3); // Timer tab is selected by default
+    
+    // Initialize tabs with screen adaptation
+    _tabs = [
+      Tab(child: Text('Alarm', style: TextStyle(fontSize: 16.sp))),
+      Tab(child: Text('Clock', style: TextStyle(fontSize: 16.sp))),
+      Tab(child: Text('Stopwatch', style: TextStyle(fontSize: 16.sp))),
+      Tab(child: Text('Timer', style: TextStyle(fontSize: 16.sp))),
+    ];
+    
     Timer.periodic(Duration(seconds: 1),
         (_) => setState(() => _localTime = DateTime.now()));
   }
@@ -54,13 +59,14 @@ class _IndexPageState extends State<IndexPage>
       appBar: AppBar(
         backgroundColor: Colors.white,
         elevation: 0,
+        toolbarHeight: 50.h,
         title: TabBar(
           controller: _tabController,
           tabs: _tabs,
           labelColor: Colors.blue,
           unselectedLabelColor: Colors.black,
           indicatorColor: Colors.blue,
-          indicatorWeight: 3,
+          indicatorWeight: 3.h,
         ),
         // actions: [
         //   IconButton(

+ 42 - 9
lib/main.dart

@@ -4,6 +4,8 @@ import 'package:flutter/material.dart';
 import 'package:flutter/services.dart';
 import 'package:flutter_clock/index_page.dart';
 import 'package:flutter_clock/utils/app_utils.dart';
+import 'package:flutter_screenutil/flutter_screenutil.dart';
+import 'package:window_manager/window_manager.dart';
 
 /// Description: enter point of the app
 /// Time       : 04/06/2025 Sunday
@@ -12,6 +14,29 @@ void main() async {
   WidgetsFlutterBinding.ensureInitialized();
   AppUtils.setOverrideForDesktop();
   
+  // Initialize window manager for desktop platforms
+  // if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
+  //   await windowManager.ensureInitialized();
+    
+  //   // Calculate window size with 16:9 portrait ratio
+  //   // Common phone resolution is 1080x1920 (16:9 portrait)
+  //   const double width = 360.0;  // Phone width
+  //   const double height = 640.0;  // Phone height (16:9 ratio)
+    
+  //   WindowOptions windowOptions = WindowOptions(
+  //     size: Size(width, height),
+  //     center: true,
+  //     backgroundColor: Colors.transparent,
+  //     skipTaskbar: false,
+  //     titleBarStyle: TitleBarStyle.normal,
+  //   );
+    
+  //   await windowManager.waitUntilReadyToShow(windowOptions, () async {
+  //     await windowManager.show();
+  //     await windowManager.focus();
+  //   });
+  // }
+  
   // Configure system settings for better background execution
   if (Platform.isAndroid) {
     SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
@@ -31,15 +56,23 @@ void main() async {
 class MyApp extends StatelessWidget {
   @override
   Widget build(BuildContext context) {
-    return MaterialApp(
-      title: 'Flutter Clock',
-      debugShowCheckedModeBanner: false,
-      theme: ThemeData(
-        primarySwatch: Colors.blue,
-        visualDensity: VisualDensity.adaptivePlatformDensity,
-        fontFamily: 'Roboto',
-      ),
-      home: IndexPage(),
+    // Initialize ScreenUtil for responsive UI
+    return ScreenUtilInit(
+      designSize: const Size(360, 640),  // Base design size
+      minTextAdapt: true,
+      splitScreenMode: true,
+      builder: (context, child) {
+        return MaterialApp(
+          title: 'Flutter Clock',
+          debugShowCheckedModeBanner: false,
+          theme: ThemeData(
+            primarySwatch: Colors.blue,
+            visualDensity: VisualDensity.adaptivePlatformDensity,
+            fontFamily: 'Roboto',
+          ),
+          home: IndexPage(),
+        );
+      }
     );
   }
 }

+ 24 - 0
pubspec.lock

@@ -142,6 +142,14 @@ packages:
     description: flutter
     source: sdk
     version: "0.0.0"
+  flutter_screenutil:
+    dependency: "direct main"
+    description:
+      name: flutter_screenutil
+      sha256: "8239210dd68bee6b0577aa4a090890342d04a136ce1c81f98ee513fc0ce891de"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "5.9.3"
   flutter_test:
     dependency: "direct dev"
     description: flutter
@@ -280,6 +288,14 @@ packages:
       url: "https://pub.flutter-io.cn"
     source: hosted
     version: "2.1.8"
+  screen_retriever:
+    dependency: transitive
+    description:
+      name: screen_retriever
+      sha256: "6ee02c8a1158e6dae7ca430da79436e3b1c9563c8cf02f524af997c201ac2b90"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "0.1.9"
   shared_preferences:
     dependency: "direct main"
     description:
@@ -493,6 +509,14 @@ packages:
       url: "https://pub.flutter-io.cn"
     source: hosted
     version: "3.1.4"
+  window_manager:
+    dependency: "direct main"
+    description:
+      name: window_manager
+      sha256: "8699323b30da4cdbe2aa2e7c9de567a6abd8a97d9a5c850a3c86dcd0b34bbfbf"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "0.3.9"
   xdg_directories:
     dependency: transitive
     description:

+ 2 - 0
pubspec.yaml

@@ -14,6 +14,8 @@ dependencies:
   wakelock: ^0.6.2
   audioplayers: ^5.2.1
   shared_preferences: ^2.2.2
+  flutter_screenutil: ^5.9.0
+  window_manager: ^0.3.8
   # dio: ^5.2.1
   # get
 dev_dependencies: