Skip to content
Snippets Groups Projects
walletsHome.dart 8.98 KiB
Newer Older
poka's avatar
poka committed
import 'package:flutter/services.dart';
import 'package:gecko/globals.dart';
poka's avatar
poka committed
import 'package:gecko/models/myWallets.dart';
import 'package:gecko/models/walletOptions.dart';
import 'package:flutter/material.dart';
import 'package:gecko/screens/myWallets/unlockingWallet.dart';
import 'package:gecko/screens/onBoarding/0_noKeychainFound.dart';
import 'package:provider/provider.dart';
poka's avatar
poka committed
// ignore: must_be_immutable
class WalletsHome extends StatelessWidget {
poka's avatar
poka committed
  final _derivationKey = GlobalKey<FormState>();
  int firstWalletDerivation;
  @override
  Widget build(BuildContext context) {
poka's avatar
poka committed
    SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
    MyWalletsProvider myWalletProvider =
        Provider.of<MyWalletsProvider>(context);
    WalletOptionsProvider _walletOptions =
        Provider.of<WalletOptionsProvider>(context);
    _walletOptions.isWalletUnlock = false;

    final int _currentChest = myWalletProvider.getCurrentChest();

    myWalletProvider.listWallets =
        myWalletProvider.getAllWalletsNames(_currentChest);
    final bool isWalletsExists = myWalletProvider.checkIfWalletExist();
poka's avatar
poka committed

    if (myWalletProvider.listWallets != '') {
      firstWalletDerivation =
          int.parse(myWalletProvider.listWallets.split('\n')[0].split(':')[3]);
        appBar: AppBar(
          title: Text('Mes portefeuilles',
              style: TextStyle(color: Colors.grey[850])),
          backgroundColor: Color(0xffFFD58D),
        ),
        floatingActionButton: Visibility(
            visible: (isWalletsExists && firstWalletDerivation != -1),
            child: Container(
                height: 80.0,
                width: 80.0,
                child: FittedBox(
                    child: FloatingActionButton(
                        heroTag: "buttonGenerateWallet",
                        onPressed: () {
poka's avatar
poka committed
                          showDialog(
                              context: context,
                              builder: (BuildContext context) {
                                return addNewDerivation(context, 1);
                              });
                            child: Icon(Icons.person_add_alt_1_rounded,
                                color: Colors.grey[850])),
                        backgroundColor: Color(0xffEFEFBF))))),
        body: SafeArea(
poka's avatar
poka committed
            child: !isWalletsExists
                ? NoKeyChainScreen()
                : myWalletsTiles(context)));
  }

  Widget myWalletsTiles(BuildContext context) {
    MyWalletsProvider _myWalletProvider =
        Provider.of<MyWalletsProvider>(context);

    final bool isWalletsExists = _myWalletProvider.checkIfWalletExist();

    if (!isWalletsExists) {
      return Text('');
    }

    if (_myWalletProvider.listWallets == '') {
      return Expanded(
          child: Center(
              child: Text(
        'Veuillez générer votre premier portefeuille',
        style: TextStyle(fontSize: 17, fontWeight: FontWeight.w500),
      )));
    }

    List _listWallets = _myWalletProvider.listWallets.split('\n');
    // final int nbrOfWallets = _listWallets.length;
    // print(_listWallets);
    // print("${_listWallets[0].split(':')[0]}:${_listWallets[0].split(':')[2]}");
    // print(defaultWallet);

    return GridView.count(
        crossAxisCount: 2,
        childAspectRatio: 1,
        crossAxisSpacing: 0,
        mainAxisSpacing: 0,
        children: <Widget>[
          for (String _repository in _listWallets)
            Padding(
                padding: EdgeInsets.all(16),
                child: GestureDetector(
                    onTap: () {
                      Navigator.push(context,
                          MaterialPageRoute(builder: (context) {
                        return UnlockingWallet(
                            walletNbr: int.parse(_repository.split(':')[1]),
                            walletName: _repository.split(':')[2],
                            derivation: int.parse(_repository.split(':')[3]));
                      }));
                    },
                    child: ClipRRect(
                        borderRadius: BorderRadius.all(Radius.circular(12)),
                        child: Column(children: <Widget>[
                          Expanded(
                              child: Container(
                            width: double.infinity,
                            height: double.infinity,
                            decoration: BoxDecoration(
                                gradient: RadialGradient(
                              radius: 1,
                              colors: [
                                Colors.green[100],
                                Colors.green[500],
                              ],
                            )),
                            child:
                                // SvgPicture.asset('assets/chopp-gecko2.png',
                                //         semanticsLabel: 'Gecko', height: 48),
                                Image.asset(
                              'assets/chopp-gecko2.png',
                            ),
                          )),
                          ListTile(
                            // contentPadding: const EdgeInsets.only(left: 7.0),
                            tileColor:
                                "${_repository.split(':')[0]}:${_repository.split(':')[1]}" ==
                                        defaultWallet
                                    ? Color(0xffD28928)
                                    : Color(0xffFFD58D),
                            // leading: Text('IMAGE'),

                            // subtitle: Text(_repository.split(':')[3],
                            //     style: TextStyle(fontSize: 12.0, fontFamily: 'Monospace')),
                            title: Center(
                                child: Padding(
                                    padding:
                                        EdgeInsets.symmetric(horizontal: 5),
                                    child: Text(_repository.split(':')[2],
                                        textAlign: TextAlign.center,
                                        style: TextStyle(
                                            fontSize: 16.0,
                                            color:
                                                "${_repository.split(':')[0]}:${_repository.split(':')[1]}" ==
                                                        defaultWallet
                                                    ? Color(0xffF9F9F1)
                                                    : Colors.black)))),
                            // dense: true,
                            onTap: () {
                              Navigator.push(context,
                                  MaterialPageRoute(builder: (context) {
                                return UnlockingWallet(
                                    walletNbr:
                                        int.parse(_repository.split(':')[1]),
                                    walletName: _repository.split(':')[2],
                                    derivation:
                                        int.parse(_repository.split(':')[3]));
                              }));
                            },
                          )
                        ]))))
poka's avatar
poka committed
  Widget addNewDerivation(context, int _walletNbr) {
    final TextEditingController _newDerivationName = TextEditingController();
poka's avatar
poka committed
    MyWalletsProvider _myWalletProvider =
        Provider.of<MyWalletsProvider>(context);
poka's avatar
poka committed
    return AlertDialog(
      content: Stack(
        clipBehavior: Clip.hardEdge,
poka's avatar
poka committed
        children: <Widget>[
          Form(
            key: _derivationKey,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Text('Nom du portefeuille:'),
                Padding(
                  padding: EdgeInsets.all(8.0),
                  child: TextFormField(
                    controller: _newDerivationName,
                    textAlign: TextAlign.center,
                    autofocus: true,
                  ),
                ),
                SizedBox(height: 20),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: ElevatedButton(
                      style: ElevatedButton.styleFrom(
                        elevation: 1,
                        primary: Color(0xffFFD68E), // background
                        onPrimary: Colors.black, // foreground
                      ),
                      onPressed: () async {
                        await _myWalletProvider
                            .generateNewDerivation(
                                context, _newDerivationName.text)
                            .then((_) => _newDerivationName.text == '');
                      },
                      child: Text("Créer")),