set_password_page.dart 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import 'package:flutter/material.dart';
  2. /// Description: set password and confirm password
  3. /// Time : 08/04/2023 Friday
  4. /// Author : liuyuqi.gov@msn.cn
  5. class SetPasswordPage extends StatefulWidget {
  6. SetPasswordPage({Key? key}) : super(key: key);
  7. @override
  8. _SetPasswordPageState createState() => _SetPasswordPageState();
  9. }
  10. class _SetPasswordPageState extends State<SetPasswordPage> {
  11. TextEditingController _pwdController = TextEditingController();
  12. bool _isconfirmPwd = false; // 是否输入成功密码
  13. @override
  14. Widget build(BuildContext context) {
  15. return Container(
  16. child: Scaffold(
  17. appBar: AppBar(
  18. title: Text("青少年模式"),
  19. centerTitle: true,
  20. leading: IconButton(
  21. icon: Icon(Icons.arrow_back_ios),
  22. onPressed: () {
  23. Navigator.pop(context);
  24. },
  25. ),
  26. ),
  27. body: SingleChildScrollView(
  28. child: Column(
  29. mainAxisAlignment: MainAxisAlignment.center,
  30. children: [
  31. Text(
  32. _isconfirmPwd ? "确认密码" : "输入密码",
  33. style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
  34. ),
  35. Text(
  36. "4个数字",
  37. style: TextStyle(fontSize: 16),
  38. ),
  39. TextField(
  40. textAlign: TextAlign.center,
  41. controller: _pwdController,
  42. decoration: InputDecoration(
  43. border: InputBorder.none,
  44. ),
  45. ),
  46. Text("输入正确密码后,将关闭青少年模式"),
  47. Row(
  48. children: [Text("忘记密码?"), Text("点击申诉")],
  49. ),
  50. // button: next
  51. TextButton(
  52. onPressed: () {
  53. next();
  54. },
  55. child: Text("下一步"))
  56. ],
  57. )),
  58. ),
  59. );
  60. }
  61. /// 下一步
  62. void next() {
  63. if (_pwdController.text.length == 4) {
  64. // 密码长度为4
  65. if (_isconfirmPwd) {
  66. // 确认密码
  67. if (_pwdController.text == "1234") {
  68. // 密码正确
  69. Navigator.pop(context);
  70. } else {
  71. // 密码错误
  72. ScaffoldMessenger.of(context).showSnackBar(SnackBar(
  73. content: Text("密码错误"),
  74. ));
  75. }
  76. } else {
  77. // 输入密码
  78. setState(() {
  79. _isconfirmPwd = true;
  80. _pwdController.text = "";
  81. });
  82. }
  83. } else {
  84. // 密码长度不为4
  85. ScaffoldMessenger.of(context).showSnackBar(SnackBar(
  86. content: Text("密码长度不为4"),
  87. ));
  88. }
  89. }
  90. }