pin_input_screen.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import 'package:flutter/material.dart';
  2. class PinInputScreen extends StatefulWidget {
  3. @override
  4. _PinInputScreenState createState() => _PinInputScreenState();
  5. }
  6. class _PinInputScreenState extends State<PinInputScreen> {
  7. List<String> pin = [];
  8. void _onKeyPressed(String value) {
  9. setState(() {
  10. if (value == 'back') {
  11. if (pin.isNotEmpty) pin.removeLast();
  12. } else if (pin.length < 6) {
  13. pin.add(value);
  14. }
  15. });
  16. }
  17. Widget _buildPinDots() {
  18. return Row(
  19. mainAxisAlignment: MainAxisAlignment.center,
  20. children: List.generate(6, (index) {
  21. return Container(
  22. margin: EdgeInsets.all(8),
  23. width: 16,
  24. height: 16,
  25. decoration: BoxDecoration(
  26. shape: BoxShape.circle,
  27. border: Border.all(color: Colors.grey),
  28. color: index < pin.length ? Colors.black : Colors.transparent,
  29. ),
  30. );
  31. }),
  32. );
  33. }
  34. Widget _buildKey(String value, {String? letters, IconData? icon}) {
  35. return GestureDetector(
  36. onTap: () => _onKeyPressed(value),
  37. child: Container(
  38. decoration: BoxDecoration(
  39. shape: BoxShape.circle,
  40. color: Colors.white,
  41. boxShadow: [
  42. BoxShadow(
  43. color: Colors.black26, blurRadius: 2, offset: Offset(0, 2)),
  44. ],
  45. ),
  46. width: 80,
  47. height: 80,
  48. alignment: Alignment.center,
  49. child: icon != null
  50. ? Icon(icon, size: 28)
  51. : Column(
  52. mainAxisAlignment: MainAxisAlignment.center,
  53. children: [
  54. Text(value,
  55. style:
  56. TextStyle(fontSize: 24, fontWeight: FontWeight.bold)),
  57. if (letters != null)
  58. Text(letters,
  59. style: TextStyle(fontSize: 12, color: Colors.grey)),
  60. ],
  61. ),
  62. ),
  63. );
  64. }
  65. @override
  66. Widget build(BuildContext context) {
  67. final keys = [
  68. ['1', ''],
  69. ['2', 'ABC'],
  70. ['3', 'DEF'],
  71. ['4', 'GHI'],
  72. ['5', 'JKL'],
  73. ['6', 'MNO'],
  74. ['7', 'PQRS'],
  75. ['8', 'TUV'],
  76. ['9', 'WXYZ'],
  77. ];
  78. return Scaffold(
  79. backgroundColor: Color(0xFFF5F5F5),
  80. body: SafeArea(
  81. child: Column(
  82. children: [
  83. SizedBox(height: 60),
  84. Text('输入解锁密码',
  85. style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold)),
  86. SizedBox(height: 20),
  87. _buildPinDots(),
  88. SizedBox(height: 40),
  89. Expanded(
  90. child: GridView.count(
  91. crossAxisCount: 3,
  92. mainAxisSpacing: 24,
  93. crossAxisSpacing: 24,
  94. padding: EdgeInsets.symmetric(horizontal: 32),
  95. children: [
  96. ...keys.map((e) => _buildKey(e[0], letters: e[1])),
  97. SizedBox(), // empty space
  98. _buildKey('0'),
  99. _buildKey('back', icon: Icons.backspace),
  100. ],
  101. ),
  102. ),
  103. SizedBox(height: 30),
  104. ],
  105. ),
  106. ),
  107. );
  108. }
  109. }