| 1 | import 'package:cake_wallet/generated/i18n.dart'; |
| 2 | import 'package:flutter/material.dart'; |
| 3 | import 'package:flutter/services.dart'; |
| 4 | |
| 5 | class NumberPad extends StatelessWidget { |
| 6 | final VoidCallback? onDecimalPressed; |
| 7 | final VoidCallback onDeletePressed; |
| 8 | final void Function(int index) onNumberPressed; |
| 9 | final FocusNode focusNode; |
| 10 | |
| 11 | const NumberPad({ |
| 12 | super.key, |
| 13 | required this.onNumberPressed, |
| 14 | required this.onDeletePressed, |
| 15 | required this.focusNode, |
| 16 | this.onDecimalPressed, |
| 17 | }); |
| 18 | |
| 19 | @override |
| 20 | Widget build(BuildContext context) => KeyboardListener( |
| 21 | focusNode: focusNode, |
| 22 | onKeyEvent: (keyEvent) { |
| 23 | if (keyEvent is KeyDownEvent) { |
| 24 | if (keyEvent.logicalKey.keyLabel == "Backspace") { |
| 25 | return onDeletePressed(); |
| 26 | } |
| 27 | |
| 28 | if ([".", ","].contains(keyEvent.logicalKey.keyLabel) && onDecimalPressed != null) { |
| 29 | return onDecimalPressed!(); |
| 30 | } |
| 31 | |
| 32 | int? number = int.tryParse(keyEvent.character ?? ''); |
| 33 | if (number != null) return onNumberPressed(number); |
| 34 | } |
| 35 | }, |
| 36 | child: SizedBox( |
| 37 | height: 300, |
| 38 | child: GridView.count( |
| 39 | childAspectRatio: 2, |
| 40 | shrinkWrap: true, |
| 41 | crossAxisCount: 3, |
| 42 | physics: const NeverScrollableScrollPhysics(), |
| 43 | children: List.generate(12, (index) { |
| 44 | if (index == 9) { |
| 45 | if (onDecimalPressed == null) return Container(); |
| 46 | return InkWell( |
| 47 | onTap: onDecimalPressed, |
| 48 | child: Center( |
| 49 | child: Text( |
| 50 | '.', |
| 51 | style: Theme.of(context).textTheme.headlineMedium?.copyWith( |
| 52 | fontWeight: FontWeight.w600, |
| 53 | fontSize: 30, |
| 54 | color: Theme.of(context).colorScheme.onSurfaceVariant, |
| 55 | ), |
| 56 | textAlign: TextAlign.center, |
| 57 | ), |
| 58 | ), |
| 59 | ); |
| 60 | } else if (index == 10) { |
| 61 | index = 0; |
| 62 | } else if (index == 11) { |
| 63 | return MergeSemantics( |
| 64 | child: Container( |
| 65 | child: Semantics( |
| 66 | label: S.of(context).delete, |
| 67 | button: true, |
| 68 | onTap: onDeletePressed, |
| 69 | child: TextButton( |
| 70 | onPressed: onDeletePressed, |
| 71 | style: TextButton.styleFrom( |
| 72 | backgroundColor: Colors.transparent, |
| 73 | shape: CircleBorder(), |
| 74 | ), |
| 75 | child: Image.asset( |
| 76 | 'assets/images/delete_icon.png', |
| 77 | color: Theme.of(context).colorScheme.primary, |
| 78 | ), |
| 79 | ), |
| 80 | ), |
| 81 | ), |
| 82 | ); |
| 83 | } else { |
| 84 | index++; |
| 85 | } |
| 86 | |
| 87 | return InkWell( |
| 88 | onTap: () => onNumberPressed(index), |
| 89 | child: Center( |
| 90 | child: Text( |
| 91 | '$index', |
| 92 | style: Theme.of(context).textTheme.headlineMedium?.copyWith( |
| 93 | fontWeight: FontWeight.w600, |
| 94 | fontSize: 30, |
| 95 | color: Theme.of(context).colorScheme.onSurfaceVariant, |
| 96 | ), |
| 97 | textAlign: TextAlign.center, |
| 98 | ), |
| 99 | ), |
| 100 | ); |
| 101 | }), |
| 102 | ), |
| 103 | ), |
| 104 | ); |
| 105 | } |