Skip to content
Snippets Groups Projects
card_terminal.dart 4.45 KiB
Newer Older
vjrj's avatar
vjrj committed
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
vjrj's avatar
vjrj committed
import 'package:vibration/vibration.dart';
vjrj's avatar
vjrj committed

vjrj's avatar
vjrj committed
import 'card_terminal_screen.dart';
import 'rubber_button.dart';
vjrj's avatar
vjrj committed

vjrj's avatar
vjrj committed
class CardTerminal extends StatefulWidget {
vjrj's avatar
vjrj committed
  const CardTerminal({super.key});

vjrj's avatar
vjrj committed
  @override
  State<CardTerminal> createState() => _CardTerminalState();
}

class _CardTerminalState extends State<CardTerminal> {
  String _currentValue = '';
  final List<String> _numbers = <String>[
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    'DEC',
    '0',
    '#',
  ];
  final int _cancelIndex = 12;
  final int _backspaceIndex = 13;
  final int _submitIndex = 14;

  late String _decimalSep;

vjrj's avatar
vjrj committed
  @override
  Widget build(BuildContext context) {
vjrj's avatar
vjrj committed
    _decimalSep = NumberFormat
        .decimalPattern(context.locale.toString())
vjrj's avatar
vjrj committed
        .symbols
        .DECIMAL_SEP;
    _numbers[9] = _decimalSep;
    return Expanded(
        child: Center(
            child: Card(
vjrj's avatar
vjrj committed
              elevation: 8.0,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(16.0),
              ),
              child: Container(
                width: 320.0,
                height: 700,
                padding: const EdgeInsets.all(10.0),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(16.0),
                  gradient: LinearGradient(
                    begin: Alignment.topLeft,
                    end: Alignment.bottomRight,
                    colors: <Color>[
                      Colors.grey[800]!,
                      Colors.grey[500]!,
                    ],
                  ),
                ),
                child: ListView(
                  //crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      CardTerminalScreen(amount: _currentValue),
                      const SizedBox(height: 8.0),
                      Padding(
                          padding: const EdgeInsets.symmetric(vertical: 16.0),
                          child: GridView.count(
                              physics: const NeverScrollableScrollPhysics(),
                              shrinkWrap: true,
                              crossAxisCount: 3,
                              crossAxisSpacing: 8,
                              mainAxisSpacing: 8,
                              childAspectRatio: 1.75 / 1,
                              padding: EdgeInsets.zero,
                              children: <Widget>[
                                for (int i = 0; i < _numbers.length + 3; i++)
                                  _buildKeyboardButton(i)
                              ]))
                    ]),
              ),
            )));
vjrj's avatar
vjrj committed
  }

  Widget _buildKeyboardButton(int index) {
    if (index == _backspaceIndex) {
      return RubberButton(
vjrj's avatar
vjrj committed
        // Yellow, remove
vjrj's avatar
vjrj committed
          bgColor: const Color(0xFFF7E378),
          icon: Icons.backspace,
          onPressed: () {
            setState(() {
              _currentValue = _currentValue.isNotEmpty
                  ? _currentValue.substring(0, _currentValue.length - 1)
                  : '';
              vibrateIfPossible();
            });
          });
    } else if (index == _submitIndex) {
      return RubberButton(
vjrj's avatar
vjrj committed
        // Green, send
vjrj's avatar
vjrj committed
          bgColor: const Color(0xFF36B649),
          icon: Icons.subdirectory_arrow_left,
          onPressed: () {
            vibrateIfPossible();
          });
    } else if (index == _cancelIndex) {
      return RubberButton(
vjrj's avatar
vjrj committed
        // Red, cancel
vjrj's avatar
vjrj committed
          bgColor: const Color(0xFFCD303D),
          icon: Icons.cancel,
          onPressed: () {
            setState(() {
              _currentValue = '';
              vibrateIfPossible();
            });
          });
    } else
      return RubberButton(
          label: _numbers[index],
          onPressed: () {
            if (_numbers[index] == '#') {
              return;
            }
            if (_numbers[index] == _decimalSep &&
                _currentValue.contains(_decimalSep)) {
              return;
            }
            setState(() {
              _currentValue += _numbers[index];
              vibrateIfPossible();
            });
          });
  }
}

Future<void> vibrateIfPossible() async {
  try {
    final bool? hasVibrator = await Vibration.hasVibrator();
    if (hasVibrator ?? false) {
      Vibration.vibrate(duration: 1000);
    }
  } catch (e) {
    // ok we tryied...
vjrj's avatar
vjrj committed
  }
}