agconnect_appmessaging 1.3.0+300 copy "agconnect_appmessaging: ^1.3.0+300" to clipboard
agconnect_appmessaging: ^1.3.0+300 copied to clipboard

outdated

You can use App Messaging of AppGallery Connect to send relevant messages to target users.

example/lib/main.dart

// Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.

import 'package:flutter/material.dart';

import 'package:agconnect_appmessaging/agconnect_appmessaging.dart';

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

class _App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: _Home(),
    );
  }
}

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

  @override
  __HomeState createState() => __HomeState();
}

class __HomeState extends State<_Home> {
  final AGCAppMessaging _appMessaging = AGCAppMessaging.getInstance();

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

    _appMessaging.onMessageDisplay.listen((AppMessage event) {
      _showDialog(context, 'onMessageDisplay', event);
    });
    _appMessaging.onMessageDismiss.listen((AppMessage event) {
      _showDialog(context, 'onMessageDismiss', event);
    });
    _appMessaging.onMessageClick.listen((AppMessage event) {
      _showDialog(context, 'onMessageClick', event);
    });
    _appMessaging.onMessageError.listen((AppMessage event) {
      _showDialog(context, 'onMessageError', event);
    });

    // Uncomment this for use custom view.
    // _appMessaging.onCustomEvent.listen((AppMessage? event) async {
    //   _showDialog(context, 'onCustomEvent', event);

    //   await _appMessaging.handleCustomViewMessageEvent(
    //     AGCAppMessagingEventType.onMessageDismiss,
    //     AGCAppMessagingDismissType.CLICK,
    //   );
    // });
  }

  void _showDialog(BuildContext context, String title, [dynamic content]) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(title),
          content: content == null
              ? null
              : SingleChildScrollView(
                  physics: const BouncingScrollPhysics(),
                  child: Text('$content'),
                ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("AGC AppMessaging Demo"),
      ),
      body: ListView(
        padding: const EdgeInsets.all(16),
        physics: const BouncingScrollPhysics(),
        children: <Widget>[
          _buildGroup(
            children: <Widget>[
              _buildButton(
                text: 'isDisplayEnable',
                onTap: () async => await _appMessaging.isDisplayEnable(),
              ),
              const Divider(height: 0),
              const Text('Set'),
              _buildButton(
                text: 'true',
                onTap: () async => await _appMessaging.setDisplayEnable(true),
              ),
              _buildButton(
                text: 'false',
                onTap: () async => await _appMessaging.setDisplayEnable(false),
              ),
            ],
          ),
          _buildGroup(
            children: <Widget>[
              _buildButton(
                text: 'isFetchMessageEnable',
                onTap: () async => await _appMessaging.isFetchMessageEnable(),
              ),
              const Divider(height: 0),
              const Text('Set'),
              _buildButton(
                text: 'true',
                onTap: () async => await _appMessaging.setFetchMessageEnable(true),
              ),
              _buildButton(
                text: 'false',
                onTap: () async => await _appMessaging.setFetchMessageEnable(false),
              ),
            ],
          ),
          _buildGroup(
            children: <Widget>[
              _buildButton(
                text: 'setForceFetch',
                onTap: () async => await _appMessaging.setForceFetch(),
              ),
              const Divider(height: 0),
              _buildButton(
                text: 'setDisplayLocation\nCENTER',
                onTap: () async => await _appMessaging.setDisplayLocation(AGCAppMessagingDisplayLocation.CENTER),
              ),
              const Divider(height: 0),
              _buildButton(
                text: 'removeCustomView',
                onTap: () async => await _appMessaging.removeCustomView(),
              ),
              const Divider(height: 0),
              _buildButton(
                text: 'trigger\n#AppOnForeground',
                onTap: () async => await _appMessaging.trigger('#AppOnForeground'),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildGroup({
    required List<Widget> children,
  }) {
    return Container(
      padding: const EdgeInsets.all(16),
      margin: const EdgeInsets.only(bottom: 16),
      decoration: const BoxDecoration(
        color: Colors.black12,
        borderRadius: BorderRadius.all(Radius.circular(16)),
      ),
      child: Wrap(
        spacing: 8,
        runSpacing: 8,
        alignment: WrapAlignment.center,
        crossAxisAlignment: WrapCrossAlignment.center,
        children: children,
      ),
    );
  }

  Widget _buildButton({
    required String text,
    required Future<dynamic> Function() onTap,
  }) {
    return ElevatedButton(
      child: Text(
        text,
        textAlign: TextAlign.center,
      ),
      onPressed: () async {
        try {
          final dynamic result = await onTap();
          _showDialog(context, 'SUCCESS', result);
        } catch (e) {
          _showDialog(context, 'ERROR', e.toString());
        }
      },
    );
  }
}
2
likes
0
points
8
downloads

Publisher

unverified uploader

Weekly Downloads

You can use App Messaging of AppGallery Connect to send relevant messages to target users.

Homepage

License

unknown (license)

Dependencies

flutter

More

Packages that depend on agconnect_appmessaging

Packages that implement agconnect_appmessaging