Flutter事件处理插件flutter_openwrap_event_handler_gam的使用

发布于 1周前 作者 ionicwang 来自 Flutter

Flutter事件处理插件flutter_openwrap_event_handler_gam的使用

Dart包用于支持OpenWrap SDK的GAM标头竞价。该插件使发布者能够利用OpenWrap和Google Mobile Ads SDK Flutter插件来实现Flutter应用的变现。

2. 文档

要详细了解如何使用OpenWrap SDK Flutter插件,请访问官方OpenWrap Flutter插件文档

3. 报告改进/问题

如果您遇到任何问题或有改进建议,请在Github上打开一个issue。如需其他查询,请联系您的PubMatic客户经理。


完整示例Demo

以下是一个完整的示例代码,展示了如何使用flutter_openwrap_event_handler_gam插件。

import 'package:flutter/material.dart';
import 'package:flutter_openwrap_sdk/flutter_openwrap_sdk.dart';
import 'package:permission_handler/permission_handler.dart';

import 'screens/screens.dart';

void main() {
  runApp(const MainScreen());
}

const List<String> adType = [
  'Banner',
  'MREC Display',
  'Interstitial Display',
  'Interstitial Video'
];

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

  [@override](/user/override)
  State<MainScreen> createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
  [@override](/user/override)
  void initState() {
    super.initState();

    // 设置日志级别为所有级别
    OpenWrapSDK.setLogLevel(POBLogLevel.all);
    // 请求位置权限
    _requestLocationPermission();
  }

  void _requestLocationPermission() async {
    const permission = Permission.location;
    await permission.request();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('PubMatic Flutter Sample App'),
        ),
        body: const MyListView(),
      ),
      debugShowCheckedModeBanner: false,
    );
  }
}

class MyListView extends StatelessWidget {
  const MyListView({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Container(
      height: MediaQuery.of(context).size.height,
      width: MediaQuery.of(context).size.width,
      color: Colors.grey[200],
      child: Padding(
        padding: const EdgeInsets.fromLTRB(15.0, 15.0, 12.0, 12.0),
        child: SingleChildScrollView(
          scrollDirection: Axis.vertical,
          child: Column(
            children: [
              for (var index in Iterable.generate(adType.length)) SubRow(index)
            ],
          ),
        ),
      ),
    );
  }
}

class SubRow extends StatelessWidget {
  final int adTypeIndex;
  const SubRow(this.adTypeIndex, {Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        switch (adTypeIndex) {
          case 0:
            // 导航到GAM Banner屏幕
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => const GAMBannerScreen()),
            );
            break;
          case 1:
            // 导航到GAM MREC Display屏幕
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => const GAMMRECDisplayScreen()),
            );
            break;
          case 2:
            // 导航到GAM Interstitial Display屏幕
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => const GAMInterstitialScreen()),
            );
            break;
          case 3:
            // 导航到GAM Interstitial Video屏幕
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => const GAMInterstitialVideoScreen()),
            );
            break;
        }
      },
      child: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
        Padding(
          padding: const EdgeInsets.fromLTRB(8.0, 0, 0, 0),
          child: ExcludeSemantics(
            child: Text(
              adType[adTypeIndex],
              style: const TextStyle(fontSize: 18),
            ),
          ),
        ),
        if (adTypeIndex + 1 != adType.length)
          Divider(
            thickness: 1.0,
            color: Colors.grey[300],
          )
      ]),
    );
  }
}

更多关于Flutter事件处理插件flutter_openwrap_event_handler_gam的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter事件处理插件flutter_openwrap_event_handler_gam的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何使用 flutter_openwrap_event_handler_gam 插件进行事件处理的代码示例。这个示例假设你已经将 flutter_openwrap_event_handler_gam 插件添加到了你的 Flutter 项目中。

首先,确保你的 pubspec.yaml 文件中包含了这个插件的依赖项:

dependencies:
  flutter:
    sdk: flutter
  flutter_openwrap_event_handler_gam: ^最新版本号  # 请替换为实际版本号

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

接下来,在你的 Flutter 应用中实现事件处理。以下是一个简单的示例,展示了如何初始化插件并监听事件:

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

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

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

class _MyAppState extends State<MyApp> {
  FlutterOpenwrapEventHandlerGam? _eventHandler;

  @override
  void initState() {
    super.initState();
    // 初始化插件
    _eventHandler = FlutterOpenwrapEventHandlerGam();

    // 监听事件
    _eventHandler!.onCustomEventReceived.listen((CustomEvent event) {
      // 处理自定义事件
      print('Received custom event: ${event.name} with data: ${event.data}');
      // 根据需要更新UI或执行其他操作
      setState(() {
        // 例如,更新一个状态变量来显示事件信息
      });
    });

    // 如果插件支持注册事件处理器,可以在这里注册
    // _eventHandler!.registerEventHandler(...);
  }

  @override
  void dispose() {
    // 取消监听事件
    _eventHandler!.onCustomEventReceived.cancel();
    _eventHandler = null;
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Event Handling Demo'),
        ),
        body: Center(
          child: Text('Listening for events...'),
        ),
      ),
    );
  }
}

// 自定义事件类,假设插件返回的事件数据包含name和data字段
class CustomEvent {
  final String name;
  final Map<String, dynamic> data;

  CustomEvent({required this.name, required this.data});
}

在这个示例中,我们做了以下几件事:

  1. pubspec.yaml 中添加了 flutter_openwrap_event_handler_gam 插件的依赖项。
  2. MyApp 类中初始化了 FlutterOpenwrapEventHandlerGam 插件实例。
  3. 使用 _eventHandler!.onCustomEventReceived.listen 方法监听自定义事件。
  4. 在事件处理回调中,我们打印了事件名称和数据,并演示了如何使用 setState 方法来更新UI(尽管在这个简单示例中并没有实际更新UI)。
  5. dispose 方法中取消了事件监听,并清理了插件实例。

请注意,这个示例假设 flutter_openwrap_event_handler_gam 插件提供了一个 onCustomEventReceived 流来监听自定义事件,并且事件数据包含 namedata 字段。实际使用时,你需要根据插件的实际API文档进行调整。如果插件提供了其他事件处理或注册处理器的方法,你也应该在 initState 方法中相应地调用它们。

回到顶部