|
| 1 | +import 'package:nocterm/nocterm.dart'; |
| 2 | +import 'package:simutil/components/simutil_theme.dart'; |
| 3 | + |
| 4 | +class PinCodeFields extends StatelessComponent { |
| 5 | + const PinCodeFields({ |
| 6 | + required this.label, |
| 7 | + required this.groupFocused, |
| 8 | + this.crossAxisAlignment = CrossAxisAlignment.center, |
| 9 | + this.spacing = 1.0, |
| 10 | + this.cellSpacing = 1.0, |
| 11 | + required this.pinControllers, |
| 12 | + required this.focusedPinIndex, |
| 13 | + required this.onPinChanged, |
| 14 | + required this.onPinKeyEvent, |
| 15 | + required this.onSubmitted, |
| 16 | + }); |
| 17 | + |
| 18 | + final String label; |
| 19 | + final bool groupFocused; |
| 20 | + final CrossAxisAlignment crossAxisAlignment; |
| 21 | + final double spacing; |
| 22 | + final double cellSpacing; |
| 23 | + final List<TextEditingController> pinControllers; |
| 24 | + final int focusedPinIndex; |
| 25 | + final void Function(int index, String value) onPinChanged; |
| 26 | + final bool Function(int index, KeyboardEvent event) onPinKeyEvent; |
| 27 | + final VoidCallback onSubmitted; |
| 28 | + |
| 29 | + @override |
| 30 | + Component build(BuildContext context) { |
| 31 | + final st = context.simutilTheme; |
| 32 | + return Column( |
| 33 | + crossAxisAlignment: crossAxisAlignment, |
| 34 | + mainAxisSize: MainAxisSize.min, |
| 35 | + children: [ |
| 36 | + Text(' $label', style: groupFocused ? st.label : st.body), |
| 37 | + SizedBox(height: spacing), |
| 38 | + Row( |
| 39 | + mainAxisSize: MainAxisSize.min, |
| 40 | + children: [ |
| 41 | + Text(' ', style: st.body), |
| 42 | + ...List.generate(pinControllers.length, (index) { |
| 43 | + return Row( |
| 44 | + children: [ |
| 45 | + _PinCell( |
| 46 | + controller: pinControllers[index], |
| 47 | + focused: groupFocused && focusedPinIndex == index, |
| 48 | + onChanged: (value) => onPinChanged(index, value), |
| 49 | + onSubmitted: onSubmitted, |
| 50 | + onKeyEvent: (event) => onPinKeyEvent(index, event), |
| 51 | + ), |
| 52 | + if (index < pinControllers.length - 1) |
| 53 | + SizedBox(width: cellSpacing), |
| 54 | + ], |
| 55 | + ); |
| 56 | + }), |
| 57 | + ], |
| 58 | + ), |
| 59 | + ], |
| 60 | + ); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +class _PinCell extends StatelessComponent { |
| 65 | + const _PinCell({ |
| 66 | + required this.controller, |
| 67 | + required this.focused, |
| 68 | + required this.onChanged, |
| 69 | + required this.onSubmitted, |
| 70 | + required this.onKeyEvent, |
| 71 | + }); |
| 72 | + |
| 73 | + final TextEditingController controller; |
| 74 | + final bool focused; |
| 75 | + final void Function(String value) onChanged; |
| 76 | + final VoidCallback onSubmitted; |
| 77 | + final bool Function(KeyboardEvent event) onKeyEvent; |
| 78 | + |
| 79 | + @override |
| 80 | + Component build(BuildContext context) { |
| 81 | + final st = context.simutilTheme; |
| 82 | + return Container( |
| 83 | + width: 5, |
| 84 | + height: 3, |
| 85 | + child: TextField( |
| 86 | + controller: controller, |
| 87 | + focused: focused, |
| 88 | + placeholder: '', |
| 89 | + placeholderStyle: st.dimmed, |
| 90 | + style: TextStyle(fontWeight: FontWeight.bold, color: st.onSurface), |
| 91 | + showCursor: false, |
| 92 | + textAlign: TextAlign.center, |
| 93 | + onChanged: onChanged, |
| 94 | + onSubmitted: (_) => onSubmitted(), |
| 95 | + onKeyEvent: onKeyEvent, |
| 96 | + decoration: InputDecoration( |
| 97 | + border: BoxBorder.all( |
| 98 | + style: BoxBorderStyle.rounded, |
| 99 | + color: st.outline, |
| 100 | + ), |
| 101 | + focusedBorder: BoxBorder.all( |
| 102 | + style: BoxBorderStyle.rounded, |
| 103 | + color: st.primary, |
| 104 | + ), |
| 105 | + contentPadding: EdgeInsets.zero, |
| 106 | + ), |
| 107 | + ), |
| 108 | + ); |
| 109 | + } |
| 110 | +} |
0 commit comments