spectus_in_app_purchase 3.0.36 copy "spectus_in_app_purchase: ^3.0.36" to clipboard
spectus_in_app_purchase: ^3.0.36 copied to clipboard

outdated

A new Flutter project.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:spectus_in_app_purchase/spectus_in_app_purchase.dart';

import 'ProductList.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  List<ProductInfo> arrayProducts = [];
  List<String> productIds = [];
  List<Map<String, String>> products = [];

  @override
  void initState() {
    super.initState();
    // initPlatformState();
    getProductList();
  }

  void getProductList() async {
    print("getProductList CALLED");
    productIds = ['com.monthly.iapp', 'com.quarterly.iap', 'com.yearly.iap'];
    List<ProductInfo> arrayInfo = [];

    List<Map<String, String>> productsLocal = [];
    productsLocal = await SpectusInAppPurchase.getSKUProductListWith(
        productIDs: productIds);

    setState(() {
      products.addAll(productsLocal);
    });

    // productIds.forEach((prodID) async {
    //   try {
    //
    //     print("==>> Array Response" + products.length.toString());
    //     List<Map<String, String>> productsLocal = [];
    //     productsLocal = await SpectusInAppPurchase.getSKUProductListWith(productID: prodID);
    //
    //     setState(() {
    //       products.addAll(productsLocal);
    //     });
    //
    //     // products.forEach((element) {
    //     //   ProductInfo obj = ProductInfo(productID: element['productID'],
    //     //       amount: element['amount'],
    //     //       title: element['title'],
    //     //       description: element['description'],
    //     //       currencyCode: element['currency_code']);
    //     //   // obj.productID = element['productID'];
    //     //   // obj.amount = element['amount'];
    //     //   // obj.title = element['title'];
    //     //   // obj.description = element['description'];
    //     //   // obj.currencyCode = element['currency_code'];
    //     //
    //     //   print("==>> Array Item" + obj.productID + " || " + obj.amount);
    //     //   arrayInfo.add(obj);
    //     // });
    //   } on PlatformException {
    //     print('--->> Failed to get platform version.');
    //   }
    // });
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await SpectusInAppPurchase.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body:Container(
          margin: EdgeInsets.only(top: 20),
          color: Colors.white,
          child: ListView.builder(
              itemCount: products.length,
              itemBuilder: (BuildContext content, int index) {
                //ProductInfo productList = arrayProducts[index];
                return GestureDetector(
                  onTap: () async {
                    var productId = products[index]['product_id'];
                    print('--->>' + productId);

                    SpectusInAppPurchase.startInAppPurchaseWith(
                        productID: productId)
                        .then((transactionID) async {
                      if (transactionID == "NA") {
                        print("Payment Failed!");
                        return;
                      }

                      //Here - Payment Success
                      //Use the Transaction ID for further reference
                      print("SUCCESS: Transaction ID => " + transactionID);

                      await showDialog(
                        context: context,
                        builder: (context) => new AlertDialog(
                          title: new Text('Purchase Successful!'),
                          content: Text(
                              'Your purchase has made and your transaction ID is ' +
                                  transactionID),
                          actions: <Widget>[
                            new FlatButton(
                              onPressed: () {
                                Navigator.of(context, rootNavigator: true).pop();


                                Navigator.pushReplacement(
                                    context,
                                    MaterialPageRoute(
                                        builder: (BuildContext context) => super
                                            .widget)); // dismisses only the dialog and returns nothing
                              },
                              child: new Text('OK'),
                            ),
                          ],
                        ),
                      );
                    });
                  },
                  child: Padding(
                    padding: const EdgeInsets.all(12.0),
                    child: Material(
                        elevation: 4.0,
                        borderRadius: BorderRadius.circular(5.0),
                        child: ProductListView(products[index], context)),
                  ),
                );
              }),
        ),
      ),
    );
  }


  Widget ProductListView(Map<String, String> contact, BuildContext context) {
    return Container(
      height: 90,
      margin: EdgeInsets.only(top: 10),
      padding: EdgeInsets.all(10),
      child:
      Column(mainAxisAlignment: MainAxisAlignment.spaceAround, children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              contact["title"].toString(),
              style: TextStyle(
                fontSize: 22,
                color: Colors.black,
                fontWeight: FontWeight.bold,
              ),
            ),
            Text(
              contact["amount"].toString(),
              style: TextStyle(
                fontSize: 22,
                color: Colors.green,
                fontWeight: FontWeight.bold,
              ),
            ),
          ],
        ),
        Text(
          contact["description"].toString(),
          maxLines: 3,
          style: TextStyle(
            fontSize: 16,
            color: Colors.grey.shade700,
          ),
        ),
/*
        Divider(
          color: Colors.grey,
          thickness: 2,
          height: 5,
        )
*/
      ]),
    );
  }
}