import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:restart_app/restart_app.dart'; import '../../api/api_service.dart'; import '../../theme/theme_controller.dart'; import 'network_settings.dart'; import 'package:dio/dio.dart'; class LoginPage extends StatefulWidget { const LoginPage({super.key}); @override State createState() => _LoginPageState(); } class _LoginPageState extends State { final ThemeController themeController = Get.find(); Dio dio = Dio(); String selectstr = "浅色"; String _username = 'super'; String _password = 'xltys2024'; @override void initState() { super.initState(); } // 登录按钮点击后的处理函数 void _login() async { var res = await ApiService.login(_username,_password); if (res['code'] == 200) { Get.snackbar("登录成功", "欢迎回来!", duration: Duration(seconds: 2)); Future.delayed(Duration(seconds: 2), () { Get.offNamed("/home_page"); // 1.导航到下一个页面 // Get.offNamed("/home_page"); // 2.浏览并删除前一个页面 // Get.offAllNamed("/home_page"); // 浏览并删除所有以前的页面 }); } else { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text(res['msg']), duration: Duration(seconds: 2), backgroundColor: Colors.red), ); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text( 'Login', ), backgroundColor: themeController.appBarColor, actions: [ PopupMenuButton( onSelected: (String value) { print(value); // 处理选中的菜单项 if (value == 'option1') { Get.to(NetworkSettings(), transition: Transition.rightToLeft); } else if (value == 'option2') { themeController.toggleTheme(); Restart.restartApp(); } else {} // Get.toNamed("/setting_network"); print('Selected: $value'); }, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(16), ), position: PopupMenuPosition.under, // 默认是over ///控制菜单的偏移位置,正值向右和下偏移,负值向左和上偏移 offset: const Offset(-32, 0), onOpened: () => debugPrint('open'), onCanceled: () { debugPrint('cancel'); }, initialValue: 'two', itemBuilder: (BuildContext context) { return [ PopupMenuItem(value: 'option1', child: Text('网络设置')), PopupMenuItem(value: 'option2', child: Text('颜色')), PopupMenuItem(value: 'option3', child: Text('其他')), ]; }, ), ], ), body: Center( child: ListView( children: [ Container( child: Padding( padding: EdgeInsets.only(left: 20, top: 100, right: 20, bottom: 100), child: Image( image: AssetImage("lib/assets/images/newLogo.png"), width: double.infinity, height: 120.0, ), ), ), Container( child: Padding( padding: EdgeInsets.all(20), child: Column( children: [ TextField( decoration: InputDecoration( prefixIcon: Icon(Icons.phone_android), hintText: '账号', labelText: '账号', // errorText: '请输入账号', border: OutlineInputBorder()), controller: TextEditingController(text: _username), onChanged: (value) { _username = value; }, ), SizedBox( height: 20, ), TextField( obscureText: true, decoration: InputDecoration( prefixIcon: Icon(Icons.lock), hintText: '密码', labelText: '密码', // errorText: '请输入账号', border: OutlineInputBorder()), controller: TextEditingController(text: _password), onChanged: (value) { _password = value; }, ), ], ), )), Container( child: Padding( padding: EdgeInsets.only( left: 20, top: 100, right: 20, bottom: 100), child: Center( child: ElevatedButton(onPressed: _login, child: Text('登录')), )), ), ], ), ), ); } }