Skip to content
Snippets Groups Projects
transaction_page.dart 11.1 KiB
Newer Older
vjrj's avatar
vjrj committed
import 'package:backdrop/backdrop.dart';
vjrj's avatar
vjrj committed
import 'package:easy_localization/easy_localization.dart';
vjrj's avatar
vjrj committed
import 'package:flutter/foundation.dart';
vjrj's avatar
vjrj committed
import 'package:flutter/material.dart';
vjrj's avatar
vjrj committed
import 'package:flutter_bloc/flutter_bloc.dart';
vjrj's avatar
vjrj committed

vjrj's avatar
vjrj committed
import '../../../data/models/node_list_cubit.dart';
vjrj's avatar
vjrj committed
import '../../../data/models/transaction.dart';
import '../../../data/models/transaction_cubit.dart';
vjrj's avatar
vjrj committed
import '../../../shared_prefs.dart';
vjrj's avatar
vjrj committed
import '../../ui_helpers.dart';
vjrj's avatar
vjrj committed
import 'transaction_chart.dart';
vjrj's avatar
vjrj committed
import 'transaction_item.dart';
vjrj's avatar
vjrj committed

class TransactionsAndBalanceWidget extends StatefulWidget {
  const TransactionsAndBalanceWidget({super.key});

  @override
  State<TransactionsAndBalanceWidget> createState() =>
      _TransactionsAndBalanceWidgetState();
}

class _TransactionsAndBalanceWidgetState
    extends State<TransactionsAndBalanceWidget>
    with SingleTickerProviderStateMixin {
vjrj's avatar
vjrj committed
  final ScrollController _transScrollController = ScrollController();
vjrj's avatar
vjrj committed

vjrj's avatar
vjrj committed
  late NodeListCubit nodeListCubit;
  late TransactionsCubit transCubit;
  bool isLoading = false;
vjrj's avatar
vjrj committed

  @override
  void initState() {
    super.initState();
vjrj's avatar
vjrj committed
    _transScrollController.addListener(_scrollListener);
vjrj's avatar
vjrj committed
    transCubit = context.read<TransactionsCubit>();
    nodeListCubit = context.read<NodeListCubit>();
    transCubit.fetchTransactions(nodeListCubit);
vjrj's avatar
vjrj committed
  }

  @override
  void dispose() {
vjrj's avatar
vjrj committed
    _transScrollController.removeListener(_scrollListener);
    _transScrollController.dispose();
vjrj's avatar
vjrj committed
    super.dispose();
  }

vjrj's avatar
vjrj committed
  Future<void> _scrollListener() async {
    if (_transScrollController.offset == 0) {
vjrj's avatar
vjrj committed
      _refreshIndicatorKey.currentState?.show();
vjrj's avatar
vjrj committed
    }
vjrj's avatar
vjrj committed
  }

vjrj's avatar
vjrj committed
  Future<void> _refreshTransactions() async {
vjrj's avatar
vjrj committed
    return transCubit.fetchTransactions(nodeListCubit);
vjrj's avatar
vjrj committed
  }

vjrj's avatar
vjrj committed
  final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
vjrj's avatar
vjrj committed
      GlobalKey<RefreshIndicatorState>();
vjrj's avatar
vjrj committed

vjrj's avatar
vjrj committed
  @override
  Widget build(BuildContext context) {
vjrj's avatar
vjrj committed
    final String myPubKey = SharedPreferencesHelper().getPubKey();
vjrj's avatar
vjrj committed
    return BlocBuilder<TransactionsCubit, TransactionsAndBalanceState>(builder:
        (BuildContext context, TransactionsAndBalanceState transBalanceState) {
      final List<Transaction> transactions = transBalanceState.transactions;
vjrj's avatar
vjrj committed
      final double balance = transBalanceState.balance;
vjrj's avatar
vjrj committed
      return BackdropScaffold(
          appBar: BackdropAppBar(
vjrj's avatar
vjrj committed
            backgroundColor: Theme.of(context).colorScheme.inversePrimary,
vjrj's avatar
vjrj committed
            title: Text(tr('balance')),
            actions: <Widget>[
              IconButton(
                icon: const Icon(Icons.refresh),
                onPressed: () {
                  _refreshIndicatorKey.currentState?.show();
                  // _refreshTransactions();
                },
              ),
vjrj's avatar
vjrj committed
              const BackdropToggleButton()
              /* IconButton(
vjrj's avatar
vjrj committed
                icon: const Icon(Icons.savings),
                onPressed: () => Backdrop.of(context).animationController,
vjrj's avatar
vjrj committed
              ) */
vjrj's avatar
vjrj committed
            ],
          ),
          backLayer: Center(
              child: Container(
vjrj's avatar
vjrj committed
            decoration: BoxDecoration(
              color: Theme.of(context).colorScheme.inversePrimary,
              border: Border.all(
                  color: Theme.of(context).colorScheme.inversePrimary,
                  width: 3),
              /* borderRadius: const BorderRadius.only(
vjrj's avatar
vjrj committed
              topLeft: Radius.circular(8),
              topRight: Radius.circular(8),
            ), */
vjrj's avatar
vjrj committed
            ),
            child: Scrollbar(
                child: ListView(
              //   controller: scrollController,
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.symmetric(vertical: 10.0),
                  child: Center(
                      child: Text(
                    formatKAmount(context, balance),
                    style: TextStyle(
                        fontSize: 36.0,
                        color:
                            balance == 0 ? Colors.lightBlue : Colors.lightBlue,
                        fontWeight: FontWeight.bold),
                  )),
vjrj's avatar
vjrj committed
                ),
vjrj's avatar
vjrj committed
                if (!kReleaseMode) TransactionChart()
                /*BalanceChart(
vjrj's avatar
vjrj committed
                                    transactions: .transactions),*/
vjrj's avatar
vjrj committed
              ],
            )),
          )),
vjrj's avatar
vjrj committed
          subHeader: BackdropSubHeader(
            title: Text(tr('transactions')),
            divider: Divider(
vjrj's avatar
vjrj committed
              color: Theme.of(context).colorScheme.surfaceVariant,
vjrj's avatar
vjrj committed
              height: 0,
vjrj's avatar
vjrj committed
            ),
vjrj's avatar
vjrj committed
          ),
          frontLayer: Center(
            child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  /* Container(
vjrj's avatar
vjrj committed
                      /* color: Theme
                      .of(context)
                      .colorScheme
                      .surfaceVariant, */
                      height: 70,
                      width: double.infinity,
                      child: const Padding(
                          padding: EdgeInsets.symmetric(horizontal: 16),
                          child: Header(text: 'transactions'))), */
vjrj's avatar
vjrj committed
                  Expanded(
vjrj's avatar
vjrj committed
                      child: Padding(
vjrj's avatar
vjrj committed
                    padding: const EdgeInsets.fromLTRB(0, 0, 0, 50),
                    child: transactions.isEmpty
                        ? Padding(
vjrj's avatar
vjrj committed
                            padding: const EdgeInsets.symmetric(horizontal: 20),
                            child: Center(child: Text(tr('no_transactions'))))
vjrj's avatar
vjrj committed
                        : RefreshIndicator(
vjrj's avatar
vjrj committed
                            key: _refreshIndicatorKey,
                            color: Colors.white,
                            backgroundColor:
vjrj's avatar
vjrj committed
                                Theme.of(context).colorScheme.primary,
vjrj's avatar
vjrj committed
                            strokeWidth: 4.0,
                            onRefresh: () async {
                              return _refreshTransactions();
                            },
                            // Pull from top to show refresh indicator.
                            child: ListView.builder(
                              physics: const AlwaysScrollableScrollPhysics(),
                              shrinkWrap: true,
                              controller: _transScrollController,
                              itemCount: transactions.length,
                              // Size of elements
                              // itemExtent: 100,
                              itemBuilder: (BuildContext context, int index) {
                                return TransactionListItem(
                                  pubKey: myPubKey,
vjrj's avatar
vjrj committed
                                  index: index,
                                  transaction: transactions[index],
                                );
                                /*
vjrj's avatar
vjrj committed
                                   Slidable(

                                      // Specify a key if the Slidable is dismissible.
                                      key: const ValueKey<int>(0),
                                      // The end action pane is the one at the right or the bottom side.
                                      endActionPane: ActionPane(
                                        motion: const ScrollMotion(),
                                        children: <SlidableAction>[
                                          SlidableAction(
                                            onPressed: (BuildContext c) {
                                              _addContact(transactions, index,
                                                  myPubKey, contactsCubit);
                                              // FIXME i18n
                                              ScaffoldMessenger.of(context)
                                                  .showSnackBar(
                                                SnackBar(
                                                  content:
                                                      Text(tr('contact_added')),
                                                ),
                                              );
                                            },
                                            backgroundColor:
                                                Theme.of(context).primaryColor,
                                            foregroundColor: Colors.white,
                                            icon: Icons.contacts,
                                            label: tr('add_contact'),
                                          ),
                                        ],
                                      ),
                                      child: ListTile(
                                        title: Text(tr('transaction_from_to',
                                            namedArgs: <String, String>{
                                              'from': humanizeFromToPubKey(
                                                  myPubKey,
                                                  transactions[index].from),
                                              'to': humanizeFromToPubKey(
                                                  myPubKey,
                                                  transactions[index].to)
                                            })),
                                        subtitle: Column(
                                            crossAxisAlignment:
                                                CrossAxisAlignment.start,
                                            children: <Widget>[
                                              if (transactions[index]
                                                  .comment
                                                  .isNotEmpty)
                                                Text(
                                                  transactions[index].comment,
                                                  style: const TextStyle(
                                                    fontStyle: FontStyle.italic,
                                                  ),
                                                ),
                                              Text(humanizeTime(
                                                  transactions[index].time,
                                                  context.locale.toString())!)
                                            ]),
                                        tileColor: tileColor(index, context),
                                        trailing: Text(
                                            '${transactions[index].amount < 0 ? "" : "+"}${(transactions[index].amount / 100).toStringAsFixed(2)} Ğ1',
                                            style: TextStyle(
                                                color:
                                                    transactions[index].amount <
                                                            0
                                                        ? Colors.red
                                                        : Colors.blue)),
                                      ));
                                      */
vjrj's avatar
vjrj committed
                              },
                            )),
vjrj's avatar
vjrj committed
                  ))
vjrj's avatar
vjrj committed
                ]),
          ));
vjrj's avatar
vjrj committed
    });
  }
vjrj's avatar
vjrj committed
}