show iap dialog on every iap list tile

This commit is contained in:
Vadim 2023-09-17 22:21:12 +02:00
parent 613a0f18a6
commit 18808a0943
3 changed files with 57 additions and 41 deletions

View file

@ -1,42 +1,20 @@
import 'package:flutter/material.dart';
import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/res/dimens.dart';
import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart';
import 'package:lightmeter/screens/settings/components/shared/iap_list_tile/widget_list_tile_iap.dart';
import 'package:lightmeter/screens/settings/components/utils/show_buy_pro_dialog.dart';
class BuyProListTile extends StatelessWidget {
const BuyProListTile({super.key});
@override
Widget build(BuildContext context) {
return ListTile(
return IAPListTile(
leading: const Icon(Icons.star),
title: Text(S.of(context).buyLightmeterPro),
onTap: () {
showDialog(
context: context,
builder: (_) => AlertDialog(
icon: const Icon(Icons.star),
titlePadding: Dimens.dialogIconTitlePadding,
title: Text(S.of(context).lightmeterPro),
contentPadding: const EdgeInsets.symmetric(horizontal: Dimens.paddingL),
content: SingleChildScrollView(child: Text(S.of(context).lightmeterProDescription)),
actionsPadding: Dimens.dialogActionsPadding,
actions: [
TextButton(
onPressed: Navigator.of(context).pop,
child: Text(S.of(context).cancel),
),
FilledButton(
onPressed: () {
Navigator.of(context).pop();
IAPProductsProvider.of(context).buy(IAPProductType.paidFeatures);
},
child: Text(S.of(context).buy),
),
],
),
);
showBuyProDialog(context);
},
showPendingTrailing: true,
);
}
}

View file

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:lightmeter/res/dimens.dart';
import 'package:lightmeter/screens/settings/components/utils/show_buy_pro_dialog.dart';
import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart';
/// Depends on the product status and replaces [onTap] with purchase callback
@ -9,31 +10,37 @@ class IAPListTile extends StatelessWidget {
final Icon leading;
final Text title;
final VoidCallback onTap;
final bool showPendingTrailing;
const IAPListTile({
this.product = IAPProductType.paidFeatures,
required this.leading,
required this.title,
required this.onTap,
this.showPendingTrailing = false,
super.key,
});
@override
Widget build(BuildContext context) {
return Opacity(
opacity: IAPProducts.isPurchased(context, product)
? Dimens.enabledOpacity
: Dimens.disabledOpacity,
child: ListTile(
leading: leading,
title: title,
onTap: switch (IAPProducts.productOf(context, product)?.status) {
IAPProductStatus.purchasable => () => IAPProductsProvider.of(context).buy(product),
IAPProductStatus.pending => null,
IAPProductStatus.purchased => onTap,
null => null,
},
),
final status = IAPProducts.productOf(context, IAPProductType.paidFeatures)?.status;
final isPending = status == IAPProductStatus.purchased || status == null;
return ListTile(
leading: leading,
title: title,
onTap: switch (status) {
IAPProductStatus.purchasable => () => showBuyProDialog(context),
IAPProductStatus.pending => null,
IAPProductStatus.purchased => onTap,
null => null,
},
trailing: showPendingTrailing && isPending
? const SizedBox(
height: Dimens.grid24,
width: Dimens.grid24,
child: CircularProgressIndicator(),
)
: null,
);
}
}

View file

@ -0,0 +1,31 @@
import 'package:flutter/material.dart';
import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/res/dimens.dart';
import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart';
Future<void> showBuyProDialog(BuildContext context) {
return showDialog(
context: context,
builder: (_) => AlertDialog(
icon: const Icon(Icons.star),
titlePadding: Dimens.dialogIconTitlePadding,
title: Text(S.of(context).lightmeterPro),
contentPadding: const EdgeInsets.symmetric(horizontal: Dimens.paddingL),
content: SingleChildScrollView(child: Text(S.of(context).lightmeterProDescription)),
actionsPadding: Dimens.dialogActionsPadding,
actions: [
TextButton(
onPressed: Navigator.of(context).pop,
child: Text(S.of(context).cancel),
),
FilledButton(
onPressed: () {
Navigator.of(context).pop();
IAPProductsProvider.of(context).buy(IAPProductType.paidFeatures);
},
child: Text(S.of(context).buy),
),
],
),
);
}