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({
|
2024-06-12 20:54:47 +00:00
|
|
|
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
|
2024-06-12 20:54:47 +00:00
|
|
|
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(),
|
|
|
|
);
|
2024-06-12 20:54:47 +00:00
|
|
|
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));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
2024-06-12 20:54:47 +00:00
|
|
|
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: [
|
2024-06-12 20:54:47 +00:00
|
|
|
FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?\d*')),
|
2024-06-12 19:23:04 +00:00
|
|
|
],
|
2024-06-12 20:54:47 +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
|
2024-06-12 20:54:47 +00:00
|
|
|
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(),
|
|
|
|
);
|
2024-06-12 20:54:47 +00:00
|
|
|
if (bigIntValue >= widget.min &&
|
|
|
|
bigIntValue <= widget.max) {
|
|
|
|
setState(() {
|
|
|
|
_currentSliderValue = newValue;
|
|
|
|
widget.onFeeChanged(bigIntValue);
|
|
|
|
});
|
|
|
|
}
|
2024-06-12 19:23:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|