Created
November 23, 2021 08:38
-
-
Save doyle-flutter/2d09bb0d861fd1c517f8fe9fac83c7e9 to your computer and use it in GitHub Desktop.
인앱결제 - 구매 이후
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:async'; | |
| import 'package:flutter/foundation.dart'; | |
| import 'package:flutter/material.dart'; | |
| import 'package:in_app_purchase/in_app_purchase.dart'; | |
| import 'package:in_app_purchase_android/in_app_purchase_android.dart'; | |
| void main() { | |
| if (defaultTargetPlatform == TargetPlatform.android) { | |
| InAppPurchaseAndroidPlatformAddition.enablePendingPurchases(); | |
| } | |
| runApp(Sys()); | |
| } | |
| class Sys extends StatelessWidget { | |
| const Sys({Key? key}) : super(key: key); | |
| @override | |
| Widget build(BuildContext context) => MaterialApp( | |
| onGenerateRoute: (RouteSettings route) => MaterialPageRoute( | |
| settings: RouteSettings(name: MainPage.path), | |
| builder: (context) => MainPage() | |
| ), | |
| ); | |
| } | |
| class MainPage extends StatefulWidget { | |
| MainPage({Key? key}) : super(key: key); | |
| static const String path = "/"; | |
| @override | |
| State<MainPage> createState() => _MainPageState(); | |
| } | |
| class _MainPageState extends State<MainPage> { | |
| final InAppPurchase _inAppPurchase = InAppPurchase.instance; | |
| List<ProductDetails> view = []; | |
| Future fetch() async{ | |
| final bool available = await _inAppPurchase.isAvailable(); | |
| if (available) { | |
| Set<String> ids = Set<String>.from(["product1","2_02202", "1_011",]); | |
| ProductDetailsResponse res = await _inAppPurchase.queryProductDetails(ids); | |
| this.view = res.productDetails; | |
| /// 📌 소모 및 비소모 둘 다 | |
| _inAppPurchase.purchaseStream.listen((List<PurchaseDetails> event) { | |
| PurchaseDetails e = event[0]; | |
| print("📌 EVENT $e ${e.status} ${e.productID} ${e.pendingCompletePurchase}"); | |
| /// 구매 여부 pendingCompletePurchase - 승인 true / 취소 false | |
| if(e.pendingCompletePurchase){ | |
| if(!mounted) return; | |
| Navigator.of(context).push(MaterialPageRoute(builder: (context) => ThankPage())); | |
| } | |
| }); | |
| } | |
| if(!mounted) return; | |
| setState(() {}); | |
| } | |
| @override | |
| void initState() { | |
| Future(this.fetch); | |
| super.initState(); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar(), | |
| body: this.view.isEmpty | |
| ? Center(child: Text("Load")) | |
| : Container( | |
| padding: EdgeInsets.all(10.0), | |
| child: Column( | |
| mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
| crossAxisAlignment: CrossAxisAlignment.start, | |
| children: this.view.map<Widget>( | |
| (ProductDetails p) => GestureDetector( | |
| onTap: () async{ | |
| final PurchaseParam purchaseParam = PurchaseParam(productDetails: p); | |
| await showDialog( | |
| context: context, | |
| builder: (context) => AlertDialog( | |
| title: Text("Buy"), | |
| actions: [ | |
| TextButton( | |
| child: Text("buyConsumable - 소모"), | |
| onPressed: () async{ | |
| bool c = await _inAppPurchase.buyConsumable(purchaseParam: purchaseParam); | |
| print("📌 $c"); /// popup 나타나는 여부 | |
| Navigator.of(context).pop(); | |
| }, | |
| ), | |
| TextButton( | |
| child: Text("buyNonConsumable - 비소모"), | |
| onPressed: () async{ | |
| bool c = await _inAppPurchase.buyNonConsumable(purchaseParam: purchaseParam); | |
| print("📌 $c"); /// popup 나타나는 여부 | |
| Navigator.of(context).pop(); | |
| }, | |
| ), | |
| TextButton( | |
| child: Text("Close"), | |
| onPressed: () async => Navigator.of(context).pop(), | |
| ) | |
| ], | |
| ) | |
| ); | |
| }, | |
| child: Text("${p.title} : ${p.price}") | |
| ) | |
| ).toList(), | |
| ), | |
| ) | |
| ); | |
| } | |
| } | |
| class ThankPage extends StatelessWidget { | |
| const ThankPage({Key? key}) : super(key: key); | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar(), | |
| body: Center( | |
| child: Text("감사합니다 🍭"), | |
| ) | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment