alroyso 1 year ago
parent
commit
6ac7a96569

+ 2 - 0
lib/app/component/sys_app_bar.dart

@@ -26,6 +26,8 @@ class SysAppBar extends StatelessWidget implements PreferredSizeWidget {
         toolbarHeight: preferredSize.height,
         title: title,
         actions: acs,
+        backgroundColor: Colors.transparent,
+        elevation: 0,
       ),
     );
   }

+ 121 - 0
lib/app/core/dio_client.dart

@@ -0,0 +1,121 @@
+import 'dart:io';
+
+import 'package:connectivity_plus/connectivity_plus.dart';
+import 'package:dio/dio.dart';
+import 'package:flutter/cupertino.dart';
+
+//import 'custom_interceptors.dart';
+
+class DioClient {
+  static final DioClient _instance = DioClient._internal();
+  late Dio _dio;
+
+  factory DioClient() => _instance;
+
+  DioClient._internal() {
+    _dio = Dio(BaseOptions(
+      baseUrl: 'https://api.example.com', // 你的API地址
+      connectTimeout: 5000,
+      receiveTimeout: 3000,
+    ));
+
+    // 添加拦截器
+    _dio.interceptors.add(CustomInterceptors());
+  }
+
+  Dio get dio => _dio;
+}
+class CustomInterceptors extends Interceptor {
+  int _retryCount = 0;
+  final List<String> _backupUrls = ['备用地址1', '备用地址2'];
+
+  Future<bool> isConnected() async {
+    var connectivityResult = await (Connectivity().checkConnectivity());
+    return connectivityResult != ConnectivityResult.none;
+  }
+
+  @override
+  Future<void> onError(DioError err, ErrorInterceptorHandler handler) async {
+    // 检查网络连接状态
+    bool isConnectNetWork = await isConnected();
+
+    if (!isConnectNetWork) {
+      // 无网络连接,设置友好的错误消息
+      err.error = AppException(message: "当前网络不可用,请检查您的网络");
+      return handler.next(err);
+    } else if (err.error is SocketException || err.type == DioErrorType.other) {
+      if (_retryCount < _backupUrls.length) {
+        // 有网络连接但请求失败,尝试使用备用地址
+        err.requestOptions.baseUrl = _backupUrls[_retryCount];
+        _retryCount++;
+        try {
+          final Dio dio = Dio();
+          final Response response = await dio.request<dynamic>(
+            err.requestOptions.path,
+            cancelToken: err.requestOptions.cancelToken,
+            data: err.requestOptions.data,
+            onReceiveProgress: err.requestOptions.onReceiveProgress,
+            onSendProgress: err.requestOptions.onSendProgress,
+            queryParameters: err.requestOptions.queryParameters,
+            options: Options(
+              method: err.requestOptions.method,
+              headers: err.requestOptions.headers,
+              contentType: err.requestOptions.contentType,
+              responseType: err.requestOptions.responseType,
+            ),
+          );
+          return handler.resolve(response);
+        } catch (e) {
+          return handler.next(err);
+        }
+      }
+    }
+
+    // 其他错误,统一处理
+    AppException appException = AppException.create(err);
+    debugPrint('DioError===: ${appException.toString()}');
+    err.error = appException;
+    return handler.next(err);
+  }
+}
+
+class AppException implements Exception {
+  final String message;
+  AppException({required this.message});
+
+  static AppException create(DioError err) {
+    switch (err.type) {
+      case DioErrorType.cancel:
+        return AppException(message: '请求被取消');
+
+      case DioErrorType.connectTimeout:
+        return AppException(message: '连接超时');
+
+      case DioErrorType.sendTimeout:
+        return AppException(message: '请求超时');
+
+      case DioErrorType.receiveTimeout:
+        return AppException(message: '响应超时');
+
+      case DioErrorType.response:
+        return AppException(
+          message: '服务器返回异常,状态码:${err.response?.statusCode}',
+        );
+
+      case DioErrorType.other:
+        return AppException(
+          message: '未知错误: ${err.message}',
+        );
+
+      default:
+        return AppException(
+          message: err.message,
+        );
+    }
+  }
+
+  @override
+  String toString() {
+    return message;
+  }
+}

+ 76 - 6
lib/app/modules/home/views/home_view.dart

@@ -13,9 +13,6 @@ class HomeView extends GetView<HomeController> {
   const HomeView({Key? key}) : super(key: key);
   @override
   Widget build(BuildContext context) {
-
-    final _usernameController = TextEditingController();
-    final _passwordController = TextEditingController();
     return Container(
       decoration: const BoxDecoration(
         image: DecorationImage(
@@ -23,12 +20,85 @@ class HomeView extends GetView<HomeController> {
           fit: BoxFit.fill,
         ),
       ),
-      child: const Scaffold(
+      child:  Scaffold(
         backgroundColor: Colors.transparent,
-        appBar: SysAppBar(title: Text("首页"),),
+        appBar: const SysAppBar(title: Text("登录"),),
 
-        body: Text("111111"),
+        body: LoginScreen(onLogin: (username,password) {
+          // 在这里处理登录逻辑,例如调用API
+          print('Username: $username');
+          print('Password: $password');
+        })
       ),
     );
   }
 }
+class LoginScreen extends StatefulWidget {
+  final Function(String username, String password) onLogin;
+
+  LoginScreen({required this.onLogin});
+
+  @override
+  _LoginScreenState createState() => _LoginScreenState();
+}
+
+class _LoginScreenState extends State<LoginScreen> {
+  final _usernameController = TextEditingController();
+  final _passwordController = TextEditingController();
+
+  @override
+  Widget build(BuildContext context) {
+    return Padding(
+      padding: const EdgeInsets.only(bottom: 30),
+      child: Center(
+        child: Padding(
+          padding: const EdgeInsets.fromLTRB(55, 0, 55, 0),
+          child: Column(
+            mainAxisAlignment: MainAxisAlignment.center,
+            children: [
+              TextField(
+                controller: _usernameController,
+                decoration: const InputDecoration(
+                  labelText: '用户名',
+                  hintText: '请输入用户名',
+                  border: InputBorder.none,
+                ),
+              ),
+              const SizedBox(height: 16),
+              TextField(
+                controller: _passwordController,
+                decoration: const InputDecoration(
+                  labelText: '密码',
+                  hintText: '输入密码',
+                  border: InputBorder.none,
+                ),
+                obscureText: true,
+
+              ),
+              const SizedBox(height: 20),
+              SizedBox(
+                width: 200,
+                height: 40,
+                child: ElevatedButton(
+                  onPressed: () {
+                    final username = _usernameController.text;
+                    final password = _passwordController.text;
+                    widget.onLogin(username, password);
+                  },
+                  child: const Text('登录'),
+                ),
+              ),
+            ],
+          ),
+        ),
+      ),
+    );
+  }
+
+  @override
+  void dispose() {
+    _usernameController.dispose();
+    _passwordController.dispose();
+    super.dispose();
+  }
+}

+ 2 - 2
lib/main.dart

@@ -7,12 +7,12 @@ import 'app/routes/app_pages.dart';
 
 void main() async {
   const width = 375.0;
-  const height = 680.0;
+  const height = 736.0;
   WidgetsFlutterBinding.ensureInitialized();
   await windowManager.ensureInitialized();
   WindowOptions windowOptions = const WindowOptions(
     minimumSize: Size(width, height),
-    maximumSize: Size(width, height),
+    maximumSize: Size(width, height - kToolbarHeight),
     size: Size(width, height),
     center: true,
     backgroundColor: Colors.transparent,

+ 2 - 0
macos/Flutter/GeneratedPluginRegistrant.swift

@@ -5,10 +5,12 @@
 import FlutterMacOS
 import Foundation
 
+import connectivity_plus_macos
 import screen_retriever
 import window_manager
 
 func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
+  ConnectivityPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlugin"))
   ScreenRetrieverPlugin.register(with: registry.registrar(forPlugin: "ScreenRetrieverPlugin"))
   WindowManagerPlugin.register(with: registry.registrar(forPlugin: "WindowManagerPlugin"))
 }

+ 2 - 1
pubspec.yaml

@@ -15,7 +15,8 @@ dependencies:
   get: 4.6.6
   flutter: 
     sdk: flutter
-
+  dio: ^4.0.6
+  connectivity_plus: ^2.3.6
 dev_dependencies: 
   flutter_lints: ^2.0.0
   flutter_test: 

+ 3 - 0
windows/flutter/generated_plugin_registrant.cc

@@ -6,10 +6,13 @@
 
 #include "generated_plugin_registrant.h"
 
+#include <connectivity_plus_windows/connectivity_plus_windows_plugin.h>
 #include <screen_retriever/screen_retriever_plugin.h>
 #include <window_manager/window_manager_plugin.h>
 
 void RegisterPlugins(flutter::PluginRegistry* registry) {
+  ConnectivityPlusWindowsPluginRegisterWithRegistrar(
+      registry->GetRegistrarForPlugin("ConnectivityPlusWindowsPlugin"));
   ScreenRetrieverPluginRegisterWithRegistrar(
       registry->GetRegistrarForPlugin("ScreenRetrieverPlugin"));
   WindowManagerPluginRegisterWithRegistrar(

+ 1 - 0
windows/flutter/generated_plugins.cmake

@@ -3,6 +3,7 @@
 #
 
 list(APPEND FLUTTER_PLUGIN_LIST
+  connectivity_plus_windows
   screen_retriever
   window_manager
 )