buzz_booster 1.11.1 copy "buzz_booster: ^1.11.1" to clipboard
buzz_booster: ^1.11.1 copied to clipboard

BuzzBooster SDK

example/lib/main.dart

import 'package:buzz_booster_example/AppKeySearchPage.dart';
import 'package:buzz_booster_example/FCMService.dart';
import 'package:buzz_booster_example/LinkUrlPage.dart';
import 'package:buzz_booster_example/Repository.dart';
import 'package:buzz_booster_example/ServerMutator.dart';
import 'package:flutter/material.dart';
import 'package:buzz_booster/buzz_booster.dart';
import 'package:flutter_phoenix/flutter_phoenix.dart';
import 'package:fluttertoast/fluttertoast.dart';

final fcmService = FCMService();
final buzzBooster = BuzzBooster();
final repository = Repository();
final serverMutator = ServerMutator();

void main() async {
  await fcmService.initFirebase();
  await fcmService.printToken();

  runApp(
    Phoenix(
      child: const MaterialApp(home: MyApp()),
    ),
  );
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

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

class _MyAppState extends State<MyApp> {
  Environment _environment = Environment.Production;
  final appKeyTextController = TextEditingController();
  final userIdTextController = TextEditingController();
  final eventNameTextController = TextEditingController();
  final eventValuesKeyTextController = TextEditingController();
  final eventValuesValueTextController = TextEditingController();

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

  Future<void> doAsyncStuff() async {
    _environment = await repository.getServerEnvironment();
    print(_environment);
    await serverMutator.changeEnvironment(_environment);
    appKeyTextController.text = await repository.getAppKey();
    userIdTextController.text = await repository.getUserId();   
    setState(() { });
    await buzzBooster.init(
      androidAppKey: await repository.getAndroidAppKey(),
      iosAppKey: await repository.getiOSAppKey(),
    );
    await buzzBooster.startService();

    linkStream.listen((url) async {
      if (url != null) {
        await Navigator.push(context, MaterialPageRoute(builder: (context) => LinkUrlPage(url: url)));
      }
    });
    return Future.value();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Padding(
          padding: const EdgeInsets.all(20.0),
          child: Column(
            children: [
              appInfoWidget(),
              loginWidget(),
              SizedBox(
                height: 10,
              ),
              eventWidget(),
              OutlinedButton(
                onPressed: () async {
                  await buzzBooster.showSpecificCampaign(CampaignType.attendance);
                },
                child: const Text("Attendance Campaign"),
              ),
              OutlinedButton(
                onPressed: () async {
                  await buzzBooster.showSpecificCampaign(CampaignType.referral);
                },
                child: const Text("Referral Campaign"),
              ),
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () async {
            await buzzBooster.showCampaign();
          },
          backgroundColor: Colors.deepPurple,
          child: const Icon(Icons.gif_box),
        ),
      ),
    );
  }

  Widget appInfoWidget() {
    return Column(children: [
      const Text("This is BuzzBooster Flutter App"),
      Row(
        children: [
          Expanded(child:TextField(
            onTap: (() async {
              final result = await Navigator.push(context, MaterialPageRoute(builder: (context) => AppKeySearchPage()));
              appKeyTextController.text = result;
            }),
            controller: appKeyTextController,
            decoration: const InputDecoration(
              border: OutlineInputBorder(),
              labelText: 'App Key',
            ),
          ),),
        ],
      ),
      TextField(
        controller: userIdTextController,
        decoration: const InputDecoration(
          border: OutlineInputBorder(),
          labelText: 'UserId',
        ),
      ),
      serverMutatorWidget(),
      OutlinedButton(
        onPressed: () async {
          String appKey = appKeyTextController.text;
          String userId = userIdTextController.text;
          await repository.clearCursor(appKey);
          await repository.saveAppKey(appKey, userId);
          await repository.saveServerEnvironment(_environment);
          await buzzBooster.setUserId(null);
          Phoenix.rebirth(context);
        },
        child: const Text("Restart"),
      ),
    ]);
  }

  StatefulWidget serverMutatorWidget() {
    return DropdownButton<Environment>(
      value: _environment,
      items: Environment.values.map((e) => DropdownMenuItem(value:e, child: Text(e.name))).toList(),
      onChanged: (value) {
        setState(() {
          _environment = value!;    
        });
      }
    );
  }

  Widget loginWidget() {
    return Column(
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            OutlinedButton(
              onPressed: () async {
                String userId = userIdTextController.text;
                if (userId.isNotEmpty) {
                  showToast("login");
                  await buzzBooster.setUserId(userId);
                  await buzzBooster.showInAppMessage();
                }
              },
              child: const Text("Login"),
            ),
            OutlinedButton(
              onPressed: () async {
                showToast("logout");
                userIdTextController.clear();
                await buzzBooster.setUserId(null);
              },
              child: const Text("Logout"),
            ),
          ],
        ),
      ],
    );
  }

  Widget eventWidget() {
    return Column(children: [
      const Text("Send Event"),
      TextField(
        controller: eventNameTextController,
        decoration: const InputDecoration(
          border: OutlineInputBorder(),
          labelText: 'event name',
        ),
      ),
      TextField(
        controller: eventValuesKeyTextController,
        decoration: const InputDecoration(
          border: OutlineInputBorder(),
          labelText: 'event values key',
        ),
      ),
      TextField(
        controller: eventValuesValueTextController,
        decoration: const InputDecoration(
          border: OutlineInputBorder(),
          labelText: 'event values value',
        ),
      ),
      Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          OutlinedButton(
            onPressed: () async {
              String eventName = eventNameTextController.text;
              String eventValuesKey = eventValuesKeyTextController.text;
              String eventValuesValue = eventValuesValueTextController.text;
              if (eventName.isNotEmpty) {
                await buzzBooster
                    .sendEvent(eventName, {eventValuesKey: eventValuesValue});
                showToast("send event: ${eventName}");
              } else {
                showToast("event name is required");
              }
            },
            child: const Text("Send Event"),
          ),
          OutlinedButton(
            onPressed: () async {
              eventNameTextController.clear();
              eventValuesKeyTextController.clear();
              eventValuesValueTextController.clear();
            },
            child: const Text("Clear Event"),
          ),
        ],
      ),
    ]);
  }

  void showToast(String message) {
    Fluttertoast.showToast(
        msg: message,
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.black45,
        textColor: Colors.white,
        fontSize: 16.0);
  }
}