Flutter通用功能库插件mobcommonlib的使用

Flutter通用功能库插件mobcommonlib的使用

mobcommonlib for flutter

Flutter插件用于MobTech SDK的通用库。

开始使用

此项目是一个起点,用于一个Flutter插件包,该包包括Android和/或iOS平台特定的实现代码。

对于如何开始使用Flutter,可以查看我们的在线文档,其中包含教程、示例、移动开发指导和完整的API参考。

集成

1. 添加依赖

在你的项目的pubspec.yaml文件中添加mobcommonlib的依赖项:

dependencies:
  flutter:
    sdk: flutter
  mobcommonlib:  # 指定最低版本,不填也可

2. 获取库

执行以下命令从pub.dev获取库:

flutter pub get

3. 导入库

在你想要使用MobTech SDK的.dart文件中导入库:

import 'package:mobcommonlib/mobcommonlib.dart';

API 介绍

1. 隐私授权

在使用任何MobTech SDK功能之前(如sharesdk, mobpus等),必须调用以下方法进行隐私授权:

Future submitPolicyGrantResult(bool granted, Function(dynamic ret, Map? err)? result)

例如:

void showPrivacyAlert(String text, BuildContext context) {
  showDialog(
      context: context,
      builder: (BuildContext context) => AlertDialog(
          title: new Text("隐私政策"),
          content: new Text(text),
          actions: <Widget>[
            new TextButton(
              child: new Text("同意"),
              onPressed: () {
                // 关闭对话框
                Navigator.of(context).pop();
                Mobcommonlib.submitPolicyGrantResult(true, null);
              },
            ),
            new TextButton(
              child: new Text("拒绝"),
              onPressed: () {
                Navigator.of(context).pop();
                Mobcommonlib.submitPolicyGrantResult(false, null);
              },
            )
          ]));
}

联系我们

如果遇到任何问题,请联系我们:

  • Email: support@mob.com
  • QQ:4006852216(在线时间: 周一至周五, 9:30-20:00)
  • 其他时间:
    • Android: 18516641950
    • iOS:18516641951

示例代码

example/lib/main.dart

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

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

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

class MyApp extends StatefulWidget {
  [@override](/user/override)
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  [@override](/user/override)
  void initState() {
    super.initState();
    initPlatformState();
  }

  // 平台消息是异步的,所以我们初始化在一个异步方法中。
  Future<void> initPlatformState() async {
    String platformVersion;
    // 平台消息可能失败,所以我们使用try/catch来捕获PlatformException。
    try {
      platformVersion = await Mobcommonlib.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // 如果在异步平台消息还在飞行时,小部件从树中被移除,我们应该丢弃回复而不是调用setState来更新我们的不存在的外观。
    if (!mounted) return;

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('MobCommon示例应用'),
        ),
        body: Padding(
          padding: const EdgeInsets.all(10.0),
          child: Container(
            child: ListView.builder(
              itemCount: 1,
              itemBuilder: (context, i) => renderRow(i, context),
            ),
          ),
        ),
      ),
    );
  }

  void showAlert(String text, BuildContext context) {
    showDialog(
        context: context,
        builder: (BuildContext context) => AlertDialog(
            title: new Text("提示"),
            content: new Text(text),
            actions: <Widget>[
              new TextButton(
                child: new Text("确定"),
                onPressed: () {
                  // 关闭对话框
                  Navigator.of(context).pop();
                },
              )
            ]));
  }

  void showPrivacyAlert(String text, BuildContext context) {
    showDialog(
        context: context,
        builder: (BuildContext context) => AlertDialog(
            title: new Text("隐私协议"),
            content: new Text(text),
            actions: <Widget>[
              new TextButton(
                child: new Text("同意"),
                onPressed: () {
                  // 关闭对话框
                  Navigator.of(context).pop();
                  // Mobcommonlib.submitPolicyGrantResult(true, (dynamic ret, Map err) => {
                  //   if(err!=null) {
                  //       // nothing to do
                  //   } else {
                  //       // nothing to do
                  //   }
                  // });
                  Mobcommonlib.submitPolicyGrantResult(true, null);
                },
              ),
              new TextButton(
                child: new Text("拒绝"),
                onPressed: () {
                  Navigator.of(context).pop();
                  // Mobcommonlib.submitPolicyGrantResult(false, (dynamic ret, Map err) => {
                  //   if(err!=null)
                  //     {
                  //       // nothing to do
                  //     }
                  //   else
                  //     {
                  //       // nothing to do
                  //     }
                  // });
                  Mobcommonlib.submitPolicyGrantResult(false, null);
                },
              )
            ]));
  }

  Widget renderRow(i, BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Container(
          height: 30,
        ),
        Text('运行于: $_platformVersion\n'),
        ConstrainedBox(
          constraints: const BoxConstraints(minWidth: double.infinity),
          child: TextButton(
            child: new Text('打开隐私协议弹窗'),
            onPressed: () {
              showPrivacyAlert('是否同意隐私协议?', context);
            },
          ),
        ),
      ],
    );
  }
}

更多关于Flutter通用功能库插件mobcommonlib的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter通用功能库插件mobcommonlib的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中集成和使用mobcommonlib插件的一个示例。假设mobcommonlib是一个包含通用功能的Flutter插件,这里将展示如何配置和使用该插件的一些基本功能。

1. 添加依赖

首先,你需要在你的pubspec.yaml文件中添加mobcommonlib的依赖。如果mobcommonlib已经在pub.dev上发布,你可以这样添加:

dependencies:
  flutter:
    sdk: flutter
  mobcommonlib: ^x.y.z  # 替换为实际的版本号

然后运行flutter pub get来获取依赖。

2. 导入插件

在你的Dart文件中导入mobcommonlib

import 'package:mobcommonlib/mobcommonlib.dart';

3. 使用插件功能

假设mobcommonlib提供了一些通用功能,比如网络请求、日志记录、设备信息等,以下是如何使用这些功能的示例代码。

网络请求示例

void makeNetworkRequest() async {
  try {
    // 假设插件提供了一个简单的GET请求方法
    var response = await MobCommonLib.network.get('https://api.example.com/data');
    print('Response data: ${response.body}');
  } catch (e) {
    print('Network error: $e');
  }
}

日志记录示例

void logMessage(String message) {
  // 假设插件提供了一个日志记录方法
  MobCommonLib.logger.info(message);
}

获取设备信息示例

void getDeviceInfo() async {
  try {
    // 假设插件提供了一个获取设备信息的方法
    var deviceInfo = await MobCommonLib.deviceInfo.getInfo();
    print('Device model: ${deviceInfo.model}');
    print('Device OS version: ${deviceInfo.osVersion}');
  } catch (e) {
    print('Error getting device info: $e');
  }
}

4. 在应用中调用这些功能

你可以在你的Flutter应用的适当位置调用这些功能,比如在按钮点击事件中:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('MobCommonLib Example'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              ElevatedButton(
                onPressed: makeNetworkRequest,
                child: Text('Make Network Request'),
              ),
              ElevatedButton(
                onPressed: () => logMessage('This is an info log message'),
                child: Text('Log Message'),
              ),
              ElevatedButton(
                onPressed: getDeviceInfo,
                child: Text('Get Device Info'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

注意事项

  • 确保mobcommonlib插件已经正确发布在pub.dev上,或者你有该插件的本地副本并正确配置在项目中。
  • 如果mobcommonlib插件的实际API与上述示例不同,请参考插件的官方文档或源代码来调整代码。
  • 示例代码仅为演示目的,实际项目中可能需要根据需求进行调整和扩展。

以上就是在Flutter项目中集成和使用mobcommonlib插件的一个基本示例。

回到顶部