1. import 'dart:convert';
    2. import 'package:flutter/material.dart';
    3. import 'package:dio/dio.dart';
    4. import 'dart:async';
    5. import 'package:fluttertoast/fluttertoast.dart';
    6. import './main.dart';
    7. class LoginPage extends StatefulWidget {
    8. _LoginPageState createState() => _LoginPageState();
    9. }
    10. class _LoginPageState extends State<LoginPage> {
    11. TextStyle TheStyle = TextStyle(
    12. color: Colors.white, fontSize: 17.0, fontWeight: FontWeight.w500);
    13. var usernameController = new TextEditingController();
    14. var userPwdController = new TextEditingController();
    15. @override
    16. Widget build(BuildContext context) {
    17. return Scaffold(
    18. // appBar: AppBar(
    19. // title: Text('芝麻开门'),
    20. // centerTitle: true,
    21. // ),
    22. backgroundColor: Color.fromRGBO(150, 180, 230, 3),
    23. body: Center(
    24. child: new Column(
    25. //MainAxisSize在主轴方向占有空间的值,默认是max。还有一个min
    26. mainAxisSize: MainAxisSize.max,
    27. //MainAxisAlignment:主轴方向上的对齐方式,会对child的位置起作用,默认是start。
    28. mainAxisAlignment: MainAxisAlignment.start,
    29. children: <Widget>[
    30. Container(
    31. height: 25,),
    32. Container(
    33. width: 410,
    34. height: 120,
    35. padding: new EdgeInsets.fromLTRB(10, 0.0, 10.0, 10.0),
    36. child: Container(
    37. child: Image.network(
    38. 'https://cdn.nlark.com/yuque/0/2019/png/164272/1556429404436-33f4dea0-2d03-4d5a-b991-fa28eca50ffb.png'),
    39. color: Color.fromRGBO(150, 180, 230, 1),
    40. )),
    41. Padding(
    42. padding: EdgeInsets.all(10.0),
    43. // 用户名输入框
    44. child: TextField(
    45. // 控制器
    46. controller: usernameController,
    47. keyboardType: TextInputType.number,
    48. // maxLength: 11,
    49. // maxLines: 1,
    50. // 是否自动更正
    51. autocorrect: false,
    52. // 是否自动对焦
    53. autofocus: false,
    54. decoration: new InputDecoration(
    55. suffixText: "可以用学号",
    56. suffixStyle: TextStyle(fontSize: 12.0, fontWeight: FontWeight.w300,color: Colors.blueGrey[300]),
    57. labelText: "用户名",
    58. // labelStyle: TextStyle(fontSize: 17.0, fontWeight: FontWeight.w600,color: Colors.blueGrey[300]),
    59. icon: new Icon(Icons.filter_tilt_shift,color: Colors.lightBlueAccent[300],),
    60. ),
    61. onChanged: (text) {
    62. //内容改变的回调
    63. print('change $text');
    64. },
    65. onSubmitted: (text) {
    66. //内容提交(按回车)的回调
    67. print('submit $text');
    68. },
    69. ),
    70. ),
    71. Padding(
    72. padding: EdgeInsets.all(10.0),
    73. child: TextField(
    74. //控制器
    75. controller: userPwdController,
    76. // 密码
    77. obscureText: true,
    78. decoration: new InputDecoration(
    79. labelText: "密码",
    80. icon: new Icon(Icons.lock_outline,color: Colors.lightBlueAccent[300],),
    81. ),
    82. onChanged: (text) {
    83. //内容改变的回调
    84. print('change $text');
    85. },
    86. onSubmitted: (text) {
    87. //内容提交(按回车)的回调
    88. print('submit $text');
    89. },
    90. ),
    91. ),
    92. Container(
    93. //这里写800已经超出屏幕了,可以理解为match_parent
    94. width: 800.0,
    95. margin: new EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 10.0),
    96. padding: new EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 10.0),
    97. //类似cardview
    98. child: new Card(
    99. color: Colors.blueAccent,
    100. // 阴影
    101. elevation: 3.0,
    102. //按钮
    103. child: new FlatButton(
    104. disabledColor: Colors.grey,
    105. disabledTextColor: Colors.black,
    106. onPressed: () {
    107. if (usernameController.text.isEmpty) {
    108. //第三方的插件Toast,https://pub.dartlang.org/packages/fluttertoast
    109. Fluttertoast.showToast(
    110. msg: "用户名不能为空哦",
    111. toastLength: Toast.LENGTH_SHORT,
    112. gravity: ToastGravity.BOTTOM,
    113. timeInSecForIos: 2,
    114. backgroundColor: Colors.white70,
    115. textColor: Colors.grey[800]);
    116. } else if (userPwdController.text.isEmpty) {
    117. Fluttertoast.showToast(
    118. msg: "密码不能为空哦",
    119. toastLength: Toast.LENGTH_SHORT,
    120. gravity: ToastGravity.BOTTOM,
    121. timeInSecForIos: 1,
    122. backgroundColor: Colors.white70,
    123. fontSize: 16.0,
    124. textColor: Colors.black);
    125. } else {
    126. //弹出对话框,里面写着账号和密码
    127. showDialog(
    128. context: context,
    129. builder: (_) {
    130. return AlertDialog(
    131. title: Text("对话框"),
    132. content: Text(usernameController.text +
    133. "\n" +
    134. userPwdController.text),
    135. actions: <Widget>[
    136. //对话框里面的两个按钮
    137. FlatButton(
    138. onPressed: () {
    139. Navigator.of(context).pop();
    140. },
    141. child: Text("取消")),
    142. FlatButton(
    143. //点击确定跳转到下一个界面,也就是HomePage
    144. onPressed: () {
    145. Navigator.push(
    146. context,
    147. MaterialPageRoute(
    148. builder: (context) =>
    149. new MyHomePage()));
    150. },
    151. child: Text("确定")),
    152. ],
    153. );
    154. });
    155. }
    156. },
    157. child: new Padding(
    158. padding: new EdgeInsets.all(10.0),
    159. child: new Text(
    160. '登录',
    161. style: new TextStyle(
    162. color: Colors.white, fontSize: 16.0),
    163. ),
    164. )),
    165. ),
    166. )
    167. ])));
    168. }
    169. }

    image.png