|
| 1 | +import 'package:flutter/cupertino.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:provider/provider.dart'; |
| 4 | +import 'package:sentinelx/models/db/prefs_store.dart'; |
| 5 | +import 'package:sentinelx/models/exchange/currencies.dart'; |
| 6 | +import 'package:sentinelx/shared_state/rate_state.dart'; |
| 7 | + |
| 8 | +class CurrencySettings extends StatefulWidget { |
| 9 | + @override |
| 10 | + _CurrencySettingsState createState() => _CurrencySettingsState(); |
| 11 | +} |
| 12 | + |
| 13 | +class _CurrencySettingsState extends State<CurrencySettings> { |
| 14 | + bool loading = false; |
| 15 | + String selectedCurrency; |
| 16 | + |
| 17 | + @override |
| 18 | + Widget build(BuildContext context) { |
| 19 | + selectedCurrency = RateState().provider.currency; |
| 20 | + |
| 21 | + return Scaffold( |
| 22 | + appBar: AppBar( |
| 23 | + title: Text("Currency settings"), |
| 24 | + bottom: UnderProgress(loading), |
| 25 | + ), |
| 26 | + backgroundColor: Theme.of(context).backgroundColor, |
| 27 | + body: ListView( |
| 28 | + children: <Widget>[ |
| 29 | + Divider(), |
| 30 | + ListTile( |
| 31 | + title: Text("Rate provider"), |
| 32 | + subtitle: Text("Select exchange for fiat price"), |
| 33 | + onTap: showProvider, |
| 34 | + ), |
| 35 | + Divider(), |
| 36 | + ListTile( |
| 37 | + title: Text("Select currency"), |
| 38 | + subtitle: Text("$selectedCurrency"), |
| 39 | + onTap: showCurrencyChooser, |
| 40 | + ), |
| 41 | + Divider(), |
| 42 | + ], |
| 43 | + ), |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + void showCurrencyChooser() { |
| 48 | + showModalBottomSheet( |
| 49 | + context: context, |
| 50 | + builder: (context) { |
| 51 | + return Card( |
| 52 | + margin: const EdgeInsets.all(8.0), |
| 53 | + child: Consumer<RateState>( |
| 54 | + builder: (context, rateState, _) { |
| 55 | + return Container( |
| 56 | + height: double.infinity, |
| 57 | + child: ListView.separated( |
| 58 | + separatorBuilder: (b, c) => Divider(height: 1,), |
| 59 | + itemBuilder: (BuildContext context, int i) { |
| 60 | + String curr = rateState.provider.availableCurrencies[i]; |
| 61 | + return ListTile( |
| 62 | + title: Text(curr), |
| 63 | + trailing: selectedCurrency == curr ? Icon(Icons.check,color: Colors.greenAccent,): SizedBox.shrink(), |
| 64 | + onTap: () { |
| 65 | + this.setCurrency(rateState.provider.availableCurrencies[i]); |
| 66 | + }, |
| 67 | + ); |
| 68 | + }, |
| 69 | + itemCount: rateState.provider.availableCurrencies.length, |
| 70 | + ), |
| 71 | + ); |
| 72 | + }, |
| 73 | + ), |
| 74 | + ); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + void setCurrency(String currency) async { |
| 79 | + PrefsStore().put(PrefsStore.CURRENCY, currency); |
| 80 | + setState(() { |
| 81 | + loading = true; |
| 82 | + selectedCurrency = currency; |
| 83 | + }); |
| 84 | + Navigator.pop(context); |
| 85 | + await RateState().setCurrency(currency); |
| 86 | + setState(() { |
| 87 | + loading = false; |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + showProvider () { |
| 92 | + List<String> providers = ["LocalBitcoins"]; |
| 93 | + String radioGroup = "LocalBitcoins"; |
| 94 | + showModalBottomSheet( |
| 95 | + context: context, |
| 96 | + builder: (context){ |
| 97 | + return Padding( |
| 98 | + padding: const EdgeInsets.all(8.0), |
| 99 | + child: Card( |
| 100 | + child: Wrap( |
| 101 | + direction: Axis.horizontal, |
| 102 | + children: providers.map((provider) { |
| 103 | + return ListTile( |
| 104 | + dense: true, |
| 105 | + onTap: () { |
| 106 | +// onSelect(timeout); |
| 107 | + }, |
| 108 | + title: Text('$provider'), |
| 109 | + trailing: Radio( |
| 110 | + value: provider, |
| 111 | + groupValue: radioGroup, |
| 112 | + onChanged: (va){}, |
| 113 | + ), |
| 114 | + ); |
| 115 | + }).toList(), |
| 116 | + ), |
| 117 | + ), |
| 118 | + ); |
| 119 | + }); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +class UnderProgress extends StatelessWidget implements PreferredSizeWidget { |
| 124 | + final bool loading; |
| 125 | + |
| 126 | + UnderProgress(this.loading); |
| 127 | + |
| 128 | + @override |
| 129 | + Widget build(BuildContext context) { |
| 130 | + return AnimatedOpacity( |
| 131 | + opacity: loading ? 1 : 0, |
| 132 | + duration: Duration(milliseconds: 600), |
| 133 | + child: Container( |
| 134 | + height: 1, |
| 135 | + child: LinearProgressIndicator(), |
| 136 | + ), |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + @override |
| 141 | + // TODO: implement preferredSize |
| 142 | + Size get preferredSize => Size(double.infinity, 1); |
| 143 | +} |
0 commit comments