appflow_flutter 0.0.2 copy "appflow_flutter: ^0.0.2" to clipboard
appflow_flutter: ^0.0.2 copied to clipboard

outdated

An AppFlow Flutter project.

example/lib/main.dart

import 'dart:async';
import 'dart:collection';

import 'package:appflow_flutter/appflow_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(const MaterialApp(
    home: MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _appflowFlutterPlugin = AppFlowFlutter();
  String packageJson = "";
  bool isSetup = false;

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

  // 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.
    // We also handle the message potentially returning null.
    try {
      platformVersion = await _appflowFlutterPlugin.getPlatformVersion() ??
          'Unknown platform version';
    } 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('AppFlow example app'),
        ),
        body: Center(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: getData(),
          ),
        ),
      ),
    );
  }

  List<Widget> getData() {
    List<Widget> data = [];
    data.add(
        TextButton(onPressed: () => {setup()}, child: const Text("setup sdk")));
    var after = [
      TextButton(
          onPressed: () async => {await _appflowFlutterPlugin.showPaywall()},
          child: const Text("showPaywall")),
      TextButton(
          onPressed: () async => {await _appflowFlutterPlugin.closePaywall()},
          child: const Text("closePaywall")),
      TextButton(
          onPressed: () async => {getSkuInfo()},
          child: const Text("getProductDetail")),
      TextButton(
          onPressed: () async => {purchasePackage()},
          child: const Text("purchasePackage")),
      TextButton(
          onPressed: () async => {getSubscriberInfo()},
          child: const Text("getSubscriberInfo")),
      TextButton(
          onPressed: () async => {sendEvent()},
          child: const Text("sendEvent")),
    ];
    if (isSetup) {
      data.addAll(after);
    }
    return data;
  }

  getSubscriberInfo() async {
    try{
      var data = await _appflowFlutterPlugin.getSubscriberInfo();
      showConfirmDialog1("getSubscriberInfo success ", data.toString());
    }catch(e){
      showConfirmDialog1("error", e.toString());
    }

  }

  getSkuInfo() async {
    try{
      var list = ["yearly_v1"];
      var data = (await _appflowFlutterPlugin.getSkuDetails(skus: list));
      packageJson = data!["yearly_v1"];
      showConfirmDialog1("getProductInfo success ", data.toString());
    }catch(e){
      showConfirmDialog1("error", e.toString());
    }

  }

  setup() async {
    try {
      await _appflowFlutterPlugin.setup();
      setState(() {
        isSetup = true;
      });
    } catch (e) {
      showConfirmDialog1("error", e.toString());
    }
  }

  sendEvent() async {
    try {
      var map = <String,String>{};
      map["test"] = "123";
      map["test2"] = "abc";
      await _appflowFlutterPlugin.sendEvent(eventName: "test",attributes: map);
    } catch (e) {
      showConfirmDialog1("error", e.toString());
    }
  }

  purchasePackage() async {
    if (packageJson.isEmpty) {
      showConfirmDialog1("error", "please invoke getProductDetail method before invoke purchasePackage");
      return;
    }
    try {
      var data = await _appflowFlutterPlugin.purchasePackage(packageJson);
      print(data);
    } catch (e, stack) {
      showConfirmDialog1("error", e.toString());
    }
  }

  // 弹出对话框
  Future<bool?> showConfirmDialog1(String title, String content) {
    return showDialog<bool>(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text(title),
          content: Text(content),
          actions: <Widget>[
            TextButton(
              child: const Text("ok"),
              onPressed: () => Navigator.of(context).pop(), // 关闭对话框
            )
          ],
        );
      },
    );
  }
}
1
likes
0
points
37
downloads

Publisher

unverified uploader

Weekly Downloads

An AppFlow Flutter project.

Homepage

License

unknown (license)

Dependencies

flutter, freezed_annotation, json_annotation, plugin_platform_interface

More

Packages that depend on appflow_flutter