Created
November 23, 2021 06:10
-
-
Save doyle-flutter/9adfd59ec307c07e2748e1d0bdefacad 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.instance.isAvailable(); | |
| if (available) { | |
| Set<String> ids = Set<String>.from(["product1","2_02202", "1_011",]); | |
| ProductDetailsResponse res = await _inAppPurchase.queryProductDetails(ids); | |
| print(res.productDetails.toString()); | |
| this.view = res.productDetails; | |
| } | |
| 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{ | |
| await InAppPurchase.instance.buyConsumable(purchaseParam: purchaseParam); | |
| Navigator.of(context).pop(); | |
| }, | |
| ), | |
| TextButton( | |
| child: Text("buyNonConsumable - 비소모"), | |
| onPressed: () async{ | |
| await InAppPurchase.instance.buyNonConsumable(purchaseParam: purchaseParam); | |
| Navigator.of(context).pop(); | |
| }, | |
| ), | |
| TextButton( | |
| child: Text("Close"), | |
| onPressed: () async => Navigator.of(context).pop(), | |
| ) | |
| ], | |
| ) | |
| ); | |
| }, | |
| child: Text("${p.title} : ${p.price}") | |
| ) | |
| ).toList(), | |
| ), | |
| ) | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment