PurchaseWidget: Move slider to its own file

Also de-couple it from RevenueCat specific APIs.
This commit is contained in:
Vishesh Handa
2020-07-09 16:31:49 +02:00
parent 51d79fdbf2
commit 94730c7040
2 changed files with 83 additions and 28 deletions

View File

@ -0,0 +1,60 @@
import 'package:flutter/material.dart';
import 'package:equatable/equatable.dart';
class PaymentInfo extends Equatable {
final double value;
final String text;
PaymentInfo(this.value, this.text);
@override
List<Object> get props => [value, text];
}
typedef PaymentSliderChanged = Function(PaymentInfo);
class PurchaseSlider extends StatelessWidget {
final List<PaymentInfo> values;
final PaymentInfo selectedValue;
final PaymentSliderChanged onChanged;
PurchaseSlider({
@required this.values,
@required this.selectedValue,
@required this.onChanged,
}) {
values.sort((a, b) => a.value.compareTo(b.value));
}
@override
Widget build(BuildContext context) {
return Slider(
min: values.first.value,
max: values.last.value + 0.50,
value: selectedValue.value,
onChanged: (double val) {
int i = -1;
for (i = 1; i < values.length; i++) {
var prev = values[i - 1].value;
var cur = values[i].value;
if (prev < val && val <= cur) {
i--;
break;
}
}
if (val == values.first.value) {
i = 0;
} else if (val >= values.last.value) {
i = values.length - 1;
}
if (i != -1) {
onChanged(values[i]);
}
},
label: selectedValue.text,
divisions: values.length,
);
}
}

View File

@ -7,6 +7,7 @@ import 'package:gitjournal/analytics.dart';
import 'package:gitjournal/.env.dart'; import 'package:gitjournal/.env.dart';
import 'package:gitjournal/iap.dart'; import 'package:gitjournal/iap.dart';
import 'package:gitjournal/settings.dart'; import 'package:gitjournal/settings.dart';
import 'package:gitjournal/widgets/purchase_slider.dart';
import 'package:purchases_flutter/purchases_flutter.dart'; import 'package:purchases_flutter/purchases_flutter.dart';
@ -163,36 +164,30 @@ class _PurchaseWidgetState extends State<PurchaseWidget> {
: buildBody(context); : buildBody(context);
} }
PaymentInfo _fromOffering(Offering o) {
var prod = o.monthly.product;
return PaymentInfo(prod.price, prod.priceString);
}
Offering _fromPaymentInfo(PaymentInfo info) {
for (var o in _offerings) {
if (o.monthly.product.priceString == info.text) {
return o;
}
}
assert(false);
return null;
}
Widget buildBody(BuildContext context) { Widget buildBody(BuildContext context) {
var slider = Slider( var slider = PurchaseSlider(
min: _offerings.first.monthly.product.price, values: _offerings.map(_fromOffering).toList(),
max: _offerings.last.monthly.product.price + 0.50, selectedValue: _fromOffering(_selectedOffering),
value: _selectedOffering.monthly.product.price, onChanged: (PaymentInfo info) {
onChanged: (double val) { setState(() {
int i = -1; _selectedOffering = _fromPaymentInfo(info);
for (i = 1; i < _offerings.length; i++) { });
var prev = _offerings[i - 1].monthly.product;
var cur = _offerings[i].monthly.product;
if (prev.price < val && val <= cur.price) {
i--;
break;
}
}
if (val == _offerings.first.monthly.product.price) {
i = 0;
} else if (val >= _offerings.last.monthly.product.price) {
i = _offerings.length - 1;
}
if (i != -1) {
setState(() {
_selectedOffering = _offerings[i];
});
}
}, },
label: _selectedOffering.monthly.product.priceString,
divisions: _offerings.length,
); );
return Column( return Column(