code.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:link/constants.dart';
  4. import '../../utils/size_fit.dart';
  5. class Code extends StatefulWidget {
  6. int type; //1 invite code 2 phone code
  7. var changed;
  8. String? _code;
  9. Code({Key? key, required this.type, required this.changed, String? code})
  10. : super(key: key) {
  11. if (code != null) {
  12. _code = code;
  13. }
  14. }
  15. @override
  16. State<Code> createState() => _CodeState();
  17. }
  18. class _CodeState extends State<Code> {
  19. String strCode = '';
  20. FocusNode focusNode = FocusNode();
  21. TextEditingController controller = TextEditingController();
  22. @override
  23. void initState() {
  24. // TODO: implement initState
  25. WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
  26. focusNode.requestFocus();
  27. });
  28. if (widget._code != null) {
  29. strCode = widget._code!;
  30. controller.text = strCode;
  31. }
  32. super.initState();
  33. }
  34. @override
  35. void dispose() {
  36. // TODO: implement dispose
  37. focusNode.dispose();
  38. controller.dispose();
  39. super.dispose();
  40. }
  41. Widget item(String code) {
  42. return GestureDetector(
  43. onTap: () {
  44. FocusScope.of(context).requestFocus(focusNode);
  45. },
  46. child: Container(
  47. width: 54.px,
  48. height: 76.px,
  49. alignment: Alignment.center,
  50. decoration: BoxDecoration(
  51. color: const Color(0xFF2C2C2E),
  52. borderRadius: BorderRadius.all(Radius.circular(16.px))),
  53. child: Text(
  54. code,
  55. style: TextStyle(
  56. color: kThemeColor,
  57. fontSize: 40.px,
  58. fontFamily: 'Link1',
  59. fontWeight: FontWeight.w600),
  60. ),
  61. ),
  62. );
  63. }
  64. @override
  65. Widget build(BuildContext context) {
  66. SizeFit.initialize(context);
  67. return Stack(
  68. children: [
  69. Row(
  70. mainAxisAlignment: MainAxisAlignment.center,
  71. children: [
  72. item(strCode.isNotEmpty
  73. ? strCode.substring(0, 1).toUpperCase()
  74. : ''),
  75. SizedBox(
  76. width: 24.px,
  77. ),
  78. item(strCode.length > 1
  79. ? strCode.substring(1, 2).toUpperCase()
  80. : ''),
  81. SizedBox(
  82. width: 24.px,
  83. ),
  84. item(strCode.length > 2
  85. ? strCode.substring(2, 3).toUpperCase()
  86. : ''),
  87. SizedBox(
  88. width: 24.px,
  89. ),
  90. item(
  91. strCode.length > 3 ? strCode.substring(3, 4).toUpperCase() : '')
  92. ],
  93. ),
  94. Opacity(
  95. opacity: 0,
  96. child: TextField(
  97. focusNode: focusNode,
  98. controller: controller,
  99. autofocus: true,
  100. maxLength: 4,
  101. keyboardType:
  102. widget.type == 1 ? TextInputType.text : TextInputType.number,
  103. onEditingComplete: () {
  104. FocusScope.of(context).requestFocus(focusNode);
  105. },
  106. onChanged: (value) {
  107. setState(() {
  108. strCode = value;
  109. widget.changed(value);
  110. });
  111. },
  112. // keyboardType: TextInputType.number,
  113. inputFormatters: [
  114. FilteringTextInputFormatter(RegExp("[a-z0-9A-Z]"), allow: true),
  115. ],
  116. ),
  117. )
  118. ],
  119. );
  120. }
  121. }