Add a YearlyPurchaseWidget - disabled for now

Abstract out the PurchaseWidget and use it for both monthly and yearly
purchases. The yearly purchases hasn't been enabled so far, as the IAPs
still need to be approved on iOS and the PageView widget I was using to
switch between the two options isn't giving me what I want.
This commit is contained in:
Vishesh Handa
2020-08-19 16:39:42 +02:00
parent 40c03f2c3e
commit 7d88ef0399
2 changed files with 98 additions and 32 deletions

View File

@ -23,9 +23,6 @@ class PurchaseScreen extends StatelessWidget {
}
Widget buildBody(BuildContext context) {
var theme = Theme.of(context);
var textTheme = theme.textTheme;
Widget w = Column(
children: <Widget>[
Text(
@ -33,21 +30,7 @@ class PurchaseScreen extends StatelessWidget {
style: Theme.of(context).textTheme.bodyText2,
),
const SizedBox(height: 32.0),
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child: Padding(
padding: const EdgeInsets.fromLTRB(16.0, 32.0, 16.0, 32.0),
child: Column(
children: [
Text("Monthly Subscription", style: textTheme.headline5),
const SizedBox(height: 32.0),
PurchaseWidget(),
],
),
),
),
const MonthlyRentalWidget(),
const SizedBox(height: 32.0),
Row(
children: [
@ -85,3 +68,84 @@ class PurchaseScreen extends StatelessWidget {
return true;
}
}
class MonthlyRentalWidget extends StatelessWidget {
const MonthlyRentalWidget({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
var textTheme = Theme.of(context).textTheme;
return Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child: Padding(
padding: const EdgeInsets.fromLTRB(16.0, 32.0, 16.0, 32.0),
child: Column(
children: [
Text("Monthly Subscription", style: textTheme.headline5),
const SizedBox(height: 32.0),
PurchaseWidget(
skus: _generateSkus(),
defaultSku: "sku_monthly_min3",
timePeriod: "Month",
),
],
),
),
);
}
Set<String> _generateSkus() {
var list = <String>{};
for (var i = 0; i <= 50; i++) {
list.add("sku_monthly_min$i");
}
return list;
}
}
class YearlyPurchaseWidget extends StatelessWidget {
const YearlyPurchaseWidget({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
var textTheme = Theme.of(context).textTheme;
return Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child: Padding(
padding: const EdgeInsets.fromLTRB(16.0, 32.0, 16.0, 32.0),
child: Column(
children: [
Text("Yearly Purchase", style: textTheme.headline5),
const SizedBox(height: 32.0),
PurchaseWidget(
skus: _generateSkus(),
defaultSku: "sku_sub_yearly_0",
timePeriod: "Year",
),
const SizedBox(height: 8.0),
const Text(
"Enables all Pro features currently in GitJournal and new ones added the next year. These features will be yours forever.")
],
),
),
);
}
Set<String> _generateSkus() {
var list = <String>{};
for (var i = 0; i <= 20; i++) {
list.add("sku_sub_yearly_$i");
}
return list;
}
}

View File

@ -15,15 +15,16 @@ import 'package:gitjournal/widgets/purchase_slider.dart';
class PurchaseButton extends StatelessWidget {
final ProductDetails product;
final String timePeriod;
PurchaseButton(this.product);
PurchaseButton(this.product, this.timePeriod);
@override
Widget build(BuildContext context) {
var price = product != null ? product.price : "Dev Mode";
return RaisedButton(
child: Text('Subscribe for $price / month'),
child: Text('Subscribe for $price / $timePeriod'),
color: Theme.of(context).primaryColor,
padding: const EdgeInsets.fromLTRB(32.0, 16.0, 32.0, 16.0),
onPressed: product != null ? () => _initPurchase(context) : null,
@ -46,15 +47,17 @@ class PurchaseButton extends StatelessWidget {
}
}
Set<String> _generateSkus() {
var list = <String>{};
for (var i = 0; i <= 50; i++) {
list.add("sku_monthly_min$i");
}
return list;
}
class PurchaseWidget extends StatefulWidget {
final Set<String> skus;
final String defaultSku;
final String timePeriod;
PurchaseWidget({
@required this.skus,
@required this.defaultSku,
@required this.timePeriod,
});
@override
_PurchaseWidgetState createState() => _PurchaseWidgetState();
}
@ -64,7 +67,6 @@ class _PurchaseWidgetState extends State<PurchaseWidget> {
ProductDetails _selectedProduct;
StreamSubscription<List<PurchaseDetails>> _subscription;
final defaultSku = "sku_monthly_min3";
String error = "";
bool pendingPurchase = false;
@ -86,7 +88,7 @@ class _PurchaseWidgetState extends State<PurchaseWidget> {
return;
}
final response = await iapCon.queryProductDetails(_generateSkus());
final response = await iapCon.queryProductDetails(widget.skus);
if (response.error != null) {
Log.e("IAP queryProductDetails: ${response.error}");
}
@ -112,7 +114,7 @@ class _PurchaseWidgetState extends State<PurchaseWidget> {
if (_products.length > 1) {
for (var p in _products) {
if (p.id == defaultSku) {
if (p.id == widget.defaultSku) {
_selectedProduct = p;
break;
}
@ -264,7 +266,7 @@ class _PurchaseWidgetState extends State<PurchaseWidget> {
mainAxisSize: MainAxisSize.max,
),
const SizedBox(height: 16.0),
PurchaseButton(_selectedProduct),
PurchaseButton(_selectedProduct, widget.timePeriod),
],
mainAxisAlignment: MainAxisAlignment.spaceAround,
);