|
| 1 | +import 'package:expense_manager/misc/extensions.dart'; |
| 2 | +import 'package:expense_manager/components/dialog/custom_dialog.dart'; |
| 3 | +import 'package:expense_manager/misc/utils.dart'; |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | +import 'package:get/get.dart'; |
| 6 | + |
| 7 | +class AmountDialog extends StatelessWidget { |
| 8 | + final int amount; |
| 9 | + final ValueChanged<int> onAmountChanged; |
| 10 | + |
| 11 | + const AmountDialog({ |
| 12 | + super.key, |
| 13 | + this.amount = 0, |
| 14 | + required this.onAmountChanged, |
| 15 | + }); |
| 16 | + |
| 17 | + @override |
| 18 | + Widget build(BuildContext context) { |
| 19 | + return CustomDialog( |
| 20 | + maxWidth: 600, |
| 21 | + child: GetBuilder<_AmountDialogController>( |
| 22 | + init: _AmountDialogController(amount: amount), |
| 23 | + builder: (controller) { |
| 24 | + return Column( |
| 25 | + mainAxisSize: MainAxisSize.min, |
| 26 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 27 | + children: [ |
| 28 | + Padding( |
| 29 | + padding: |
| 30 | + EdgeInsets.symmetric(horizontal: 16).copyWith(top: 16), |
| 31 | + child: Text( |
| 32 | + controller.amount.toMoney(), |
| 33 | + style: Theme.of(context).textTheme.displaySmall, |
| 34 | + ), |
| 35 | + ), |
| 36 | + |
| 37 | + // number pads |
| 38 | + GridView.count( |
| 39 | + crossAxisCount: 3, |
| 40 | + shrinkWrap: true, |
| 41 | + childAspectRatio: 2.3, |
| 42 | + children: controller.keys.map((numKey) { |
| 43 | + return InkWell( |
| 44 | + onTap: () { |
| 45 | + controller.calculateAmount(numKey); |
| 46 | + }, |
| 47 | + child: Center( |
| 48 | + child: numKey == 'back' |
| 49 | + ? Icon(Icons.backspace) |
| 50 | + : Text(numKey, style: TextStyle(fontSize: 21)), |
| 51 | + ), |
| 52 | + ); |
| 53 | + }).toList(), |
| 54 | + ), |
| 55 | + |
| 56 | + Padding( |
| 57 | + padding: EdgeInsets.all(8), |
| 58 | + child: Row( |
| 59 | + mainAxisAlignment: MainAxisAlignment.end, |
| 60 | + children: [ |
| 61 | + TextButton( |
| 62 | + child: Text( |
| 63 | + 'CANCEL', |
| 64 | + style: TextStyle(color: Colors.grey), |
| 65 | + ), |
| 66 | + onPressed: () => goBack(context), |
| 67 | + ), |
| 68 | + TextButton( |
| 69 | + child: Text('OK'), |
| 70 | + onPressed: () { |
| 71 | + onAmountChanged.call(controller.amount); |
| 72 | + goBack(context); |
| 73 | + }, |
| 74 | + ), |
| 75 | + ], |
| 76 | + ), |
| 77 | + ), |
| 78 | + ], |
| 79 | + ); |
| 80 | + }), |
| 81 | + ); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +class _AmountDialogController extends GetxController { |
| 86 | + final List<String> keys = [ |
| 87 | + '1', |
| 88 | + '2', |
| 89 | + '3', |
| 90 | + '4', |
| 91 | + '5', |
| 92 | + '6', |
| 93 | + '7', |
| 94 | + '8', |
| 95 | + '9', |
| 96 | + '', |
| 97 | + '0', |
| 98 | + 'back' |
| 99 | + ]; |
| 100 | + int amount; |
| 101 | + |
| 102 | + _AmountDialogController({required this.amount}); |
| 103 | + |
| 104 | + void calculateAmount(String numKey) { |
| 105 | + if (numKey.isEmpty) return; |
| 106 | + |
| 107 | + switch (numKey) { |
| 108 | + case 'back': |
| 109 | + String amountText = amount.toString(); |
| 110 | + amountText = amountText.length <= 1 |
| 111 | + ? '0' |
| 112 | + : amountText.substring(0, amountText.length - 1); |
| 113 | + amount = int.parse(amountText); |
| 114 | + break; |
| 115 | + default: |
| 116 | + final amountText = amount.toString() + numKey; |
| 117 | + amount = int.parse(amountText); |
| 118 | + } |
| 119 | + |
| 120 | + update(); |
| 121 | + } |
| 122 | +} |
0 commit comments