stack_wallet/lib/widgets/boost_fee_slider.dart

146 lines
5 KiB
Dart
Raw Normal View History

2024-06-12 19:23:04 +00:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../utilities/amount/amount.dart';
import '../utilities/amount/amount_formatter.dart';
import '../wallets/crypto_currency/crypto_currency.dart'; // Update with your actual path
class BoostFeeSlider extends ConsumerStatefulWidget {
final CryptoCurrency coin;
2024-06-15 16:27:16 +00:00
final void Function(BigInt) onFeeChanged;
2024-06-12 19:23:04 +00:00
final BigInt min;
final BigInt max;
2024-06-15 16:27:16 +00:00
const BoostFeeSlider({
super.key,
2024-06-12 19:23:04 +00:00
required this.coin,
required this.onFeeChanged,
required this.min,
required this.max,
});
@override
2024-06-15 16:27:16 +00:00
ConsumerState<BoostFeeSlider> createState() => _BoostFeeSliderState();
2024-06-12 19:23:04 +00:00
}
class _BoostFeeSliderState extends ConsumerState<BoostFeeSlider> {
double _currentSliderValue = 0;
late TextEditingController _textEditingController;
@override
void initState() {
super.initState();
_currentSliderValue = widget.min.toDouble();
_textEditingController = TextEditingController(
text: ref.read(pAmountFormatter(widget.coin)).format(
Amount(
2024-06-15 16:27:16 +00:00
rawValue: BigInt.from(_currentSliderValue),
fractionDigits: widget.coin.fractionDigits,
),
2024-06-12 19:23:04 +00:00
withUnitName: false,
),
);
_textEditingController.addListener(() {
2024-06-15 16:27:16 +00:00
// TODO: value.replaceAll(',', '') doesn't work for certain locales
final double? value =
double.tryParse(_textEditingController.text.replaceAll(',', ''));
if (value != null) {
final BigInt bigIntValue = BigInt.from(
2024-06-15 16:27:16 +00:00
value * BigInt.from(10).pow(widget.coin.fractionDigits).toInt(),
);
if (bigIntValue >= widget.min && bigIntValue <= widget.max) {
setState(() {
_currentSliderValue = value;
widget.onFeeChanged(bigIntValue);
});
}
2024-06-12 19:23:04 +00:00
}
});
}
@override
void dispose() {
_textEditingController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: <Widget>[
Row(
children: [
Expanded(
child: Slider(
value: _currentSliderValue,
min: widget.min.toDouble(),
max: widget.max.toDouble(),
divisions: (widget.max - widget.min).toInt(),
2024-06-15 16:27:16 +00:00
label: ref.read(pAmountFormatter(widget.coin)).format(
Amount(
rawValue: BigInt.from(_currentSliderValue),
fractionDigits: widget.coin.fractionDigits,
),
),
2024-06-12 19:23:04 +00:00
onChanged: (value) {
setState(() {
_currentSliderValue = value;
2024-06-15 16:27:16 +00:00
_textEditingController.text =
ref.read(pAmountFormatter(widget.coin)).format(
Amount(
rawValue: BigInt.from(_currentSliderValue),
fractionDigits: widget.coin.fractionDigits,
),
);
2024-06-12 19:23:04 +00:00
widget.onFeeChanged(BigInt.from(_currentSliderValue));
});
},
),
),
SizedBox(
width: 16 + // Left and right padding.
122 / 8 * widget.coin.fractionDigits + // Variable width.
8 * widget.coin.ticker.length, // End padding for ticker.
2024-06-12 19:23:04 +00:00
child: TextField(
controller: _textEditingController,
2024-06-15 16:27:16 +00:00
keyboardType:
const TextInputType.numberWithOptions(decimal: true),
2024-06-12 19:23:04 +00:00
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?\d*')),
2024-06-12 19:23:04 +00:00
],
decoration: const InputDecoration(
2024-06-12 19:23:04 +00:00
border: OutlineInputBorder(),
),
onChanged: (value) {
2024-06-15 16:27:16 +00:00
// TODO: value.replaceAll(',', '') doesn't work for certain locales
final double? newValue =
double.tryParse(value.replaceAll(',', ''));
if (newValue != null) {
2024-06-15 16:27:16 +00:00
final BigInt bigIntValue = BigInt.from(
newValue *
BigInt.from(10)
.pow(widget.coin.fractionDigits)
.toInt(),
);
if (bigIntValue >= widget.min &&
bigIntValue <= widget.max) {
setState(() {
_currentSliderValue = newValue;
widget.onFeeChanged(bigIntValue);
});
}
2024-06-12 19:23:04 +00:00
}
},
),
),
],
),
],
),
);
}
}