login.dart 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import 'package:flutter/material.dart';
  2. import 'package:fun_selfie_app/utils/storage.dart';
  3. import 'dart:convert';
  4. class LoginPage extends StatefulWidget {
  5. const LoginPage({Key? key}) : super(key: key);
  6. @override
  7. State<StatefulWidget> createState() => _LoginPageState();
  8. }
  9. class _LoginPageState extends State<LoginPage> {
  10. bool showPassword = false;
  11. GlobalKey<FormState> formKey = GlobalKey<FormState>(); //表单验证
  12. final TextEditingController configName = TextEditingController();
  13. final TextEditingController configPass = TextEditingController();
  14. @override
  15. Widget build(BuildContext context) {
  16. final screenSize = MediaQuery.of(context).size;
  17. return Scaffold(
  18. appBar: AppBar(
  19. title: const Text("登录"),
  20. ),
  21. body: Container(
  22. width: screenSize.width,
  23. height: screenSize.height,
  24. decoration: const BoxDecoration(
  25. image: DecorationImage(
  26. fit: BoxFit.cover,
  27. image: AssetImage('static/images/login_bg.png'))),
  28. child: Center(
  29. child: Container(
  30. decoration: const BoxDecoration(
  31. color: Colors.white,
  32. ),
  33. width: 500,
  34. height: 400,
  35. child: Form(
  36. key: formKey,
  37. autovalidateMode: AutovalidateMode.onUserInteraction,
  38. child: Padding(
  39. padding: const EdgeInsets.only(
  40. left: 50, right: 50, top: 70, bottom: 0),
  41. // padding: const EdgeInsets.all(50),
  42. child: Column(
  43. children: [
  44. // const SizedBox(
  45. // height: 24,
  46. // ),
  47. Container(
  48. padding: const EdgeInsets.symmetric(
  49. horizontal: 20, vertical: 5),
  50. decoration: const BoxDecoration(
  51. color: Color.fromARGB(8, 0, 0, 0),
  52. borderRadius:
  53. BorderRadius.all(Radius.circular(10))),
  54. child: TextFormField(
  55. controller: configName,
  56. validator: (v) => (v == '') ? "请输入账号" : null,
  57. decoration: const InputDecoration(
  58. border: InputBorder.none,
  59. labelText: "账号",
  60. hintText: "请输入账号",
  61. prefixIcon: Icon(Icons.person),
  62. ),
  63. ),
  64. ),
  65. const SizedBox(
  66. height: 16,
  67. ),
  68. Container(
  69. padding: const EdgeInsets.symmetric(
  70. horizontal: 20, vertical: 5),
  71. decoration: const BoxDecoration(
  72. color: Color.fromARGB(8, 0, 0, 0),
  73. borderRadius:
  74. BorderRadius.all(Radius.circular(10))),
  75. child: TextFormField(
  76. controller: configPass,
  77. validator: (v) => (v == '') ? "请输入密码" : null,
  78. obscureText: !showPassword,
  79. decoration: InputDecoration(
  80. border: InputBorder.none,
  81. labelText: "密码",
  82. hintText: "请输入密码",
  83. prefixIcon: const Icon(Icons.lock),
  84. suffixIcon: IconButton(
  85. icon: Icon(showPassword
  86. ? Icons.visibility_off
  87. : Icons.visibility),
  88. onPressed: () {
  89. setState(() {
  90. showPassword = !showPassword;
  91. });
  92. },
  93. ),
  94. ),
  95. ),
  96. ),
  97. // const SizedBox(
  98. // height: 16,
  99. // ),
  100. // GestureDetector(
  101. // child: const SizedBox(
  102. // width: double.infinity,
  103. // child: Text(
  104. // "找回密码",
  105. // style: TextStyle(color: Colors.blue),
  106. // textAlign: TextAlign.right,
  107. // ),
  108. // ),
  109. // onTap: () {
  110. // debugPrint("找回密码被点击");
  111. // },
  112. // ),
  113. const SizedBox(
  114. height: 50,
  115. ),
  116. SizedBox(
  117. width: double.infinity,
  118. height: 40,
  119. child: ElevatedButton(
  120. style: ButtonStyle(
  121. // backgroundColor:
  122. // MaterialStateProperty.all(Colors.white),
  123. shadowColor: MaterialStateProperty.all(
  124. Colors.transparent),
  125. // foregroundColor: MaterialStateProperty.all(
  126. // const Color(0xffBB86FC)),
  127. ),
  128. child: const Text("登录"),
  129. onPressed: () {
  130. _onSubmit(context);
  131. },
  132. ),
  133. ),
  134. const SizedBox(
  135. height: 16,
  136. ),
  137. // SizedBox(
  138. // width: double.infinity,
  139. // child: ElevatedButton(
  140. // child: const Text("注册"),
  141. // onPressed: () {
  142. // Navigator.pushReplacementNamed(context, 'register');
  143. // },
  144. // ),
  145. // ),
  146. ],
  147. ),
  148. )))),
  149. ),
  150. );
  151. }
  152. void _onSubmit(context) async {
  153. final form = formKey.currentState;
  154. if (form!.validate()) {
  155. Object str = {
  156. 'configName': configName.text,
  157. 'configPass': configPass.text
  158. };
  159. await StorageUtil.setString('userMsg', jsonEncode(str));
  160. Navigator.pushNamed(context, "/");
  161. }
  162. }
  163. }