Flutter广告中介插件tapsell_mediation_legacy的使用

Flutter广告中介插件tapsell_mediation_legacy的使用


Flutter Tapsell Legacy Mediation Adapter

Pub.dev version Popularity Likes Flutter linter
Technical Support

tapsell_mediation_legacy #

Legacy mediation adapter

安装 #

flutter pub add tapsell_mediation_legacy

使用 #

无需特定实现。以下是一个完整的示例Demo来展示如何使用该插件。

完整示例Demo

首先,在pubspec.yaml文件中添加依赖项:

dependencies:
  tapsell_mediation_legacy: ^版本号

然后在main.dart文件中初始化并使用插件:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Tapsell Mediation Legacy Example'),
        ),
        body: Center(
          child: TapsellMediationWidget(
            adUnitId: '你的广告单元ID', // 替换为你的广告单元ID
            onAdLoaded: () {
              print("广告加载成功");
            },
            onAdFailedToLoad: (error) {
              print("广告加载失败: $error");
            },
            onAdClicked: () {
              print("广告点击");
            },
          ),
        ),
      ),
    );
  }
}

更多关于Flutter广告中介插件tapsell_mediation_legacy的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter广告中介插件tapsell_mediation_legacy的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


tapsell_mediation_legacy 是一个用于 Flutter 的广告中介插件,主要用于集成 Tapsell 广告平台。Tapsell 是一个中东地区的广告平台,提供多种广告格式,包括横幅广告、插页广告和视频广告。

以下是如何在 Flutter 项目中使用 tapsell_mediation_legacy 插件的基本步骤:

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 tapsell_mediation_legacy 插件的依赖。

dependencies:
  flutter:
    sdk: flutter
  tapsell_mediation_legacy: ^1.0.0  # 请根据实际情况使用最新版本

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

2. 初始化 Tapsell

在你的 Flutter 应用程序启动时,首先需要初始化 Tapsell。通常可以在 main.dart 文件中进行初始化。

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

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await TapsellMediation.initialize('YOUR_TAPSELL_APP_KEY'); // 替换为你的 Tapsell App Key
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

3. 加载并显示广告

你可以使用 TapsellMediation 类来加载和显示不同类型的广告。以下是一些常见的广告类型示例:

插页广告 (Interstitial Ad)

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

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _interstitialAdId = 'YOUR_INTERSTITIAL_ZONE_ID'; // 替换为你的插页广告 Zone ID

  Future<void> _loadInterstitialAd() async {
    await TapsellMediation.loadInterstitialAd(_interstitialAdId);
  }

  Future<void> _showInterstitialAd() async {
    await TapsellMediation.showInterstitialAd(_interstitialAdId);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Tapsell Ads Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              onPressed: _loadInterstitialAd,
              child: Text('Load Interstitial Ad'),
            ),
            ElevatedButton(
              onPressed: _showInterstitialAd,
              child: Text('Show Interstitial Ad'),
            ),
          ],
        ),
      ),
    );
  }
}

横幅广告 (Banner Ad)

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

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _bannerAdId = 'YOUR_BANNER_ZONE_ID'; // 替换为你的横幅广告 Zone ID

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Tapsell Ads Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TapsellMediationBannerAd(
              zoneId: _bannerAdId,
              adSize: TapsellMediationBannerAdSize.BANNER_320x50,
              onAdLoaded: () {
                print('Banner Ad Loaded');
              },
              onAdFailedToLoad: (error) {
                print('Banner Ad Failed to Load: $error');
              },
            ),
          ],
        ),
      ),
    );
  }
}

视频广告 (Rewarded Video Ad)

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

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _rewardedAdId = 'YOUR_REWARDED_ZONE_ID'; // 替换为你的视频广告 Zone ID

  Future<void> _loadRewardedAd() async {
    await TapsellMediation.loadRewardedAd(_rewardedAdId);
  }

  Future<void> _showRewardedAd() async {
    await TapsellMediation.showRewardedAd(_rewardedAdId);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Tapsell Ads Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              onPressed: _loadRewardedAd,
              child: Text('Load Rewarded Ad'),
            ),
            ElevatedButton(
              onPressed: _showRewardedAd,
              child: Text('Show Rewarded Ad'),
            ),
          ],
        ),
      ),
    );
  }
}

4. 处理回调

你可以通过回调来处理广告的加载、显示、点击等事件。例如:

TapsellMediation.setInterstitialAdListener(
  onAdAvailable: (zoneId) {
    print('Interstitial Ad Available for zone: $zoneId');
  },
  onAdNotAvailable: (zoneId) {
    print('Interstitial Ad Not Available for zone: $zoneId');
  },
  onAdShown: (zoneId) {
    print('Interstitial Ad Shown for zone: $zoneId');
  },
  onAdClosed: (zoneId) {
    print('Interstitial Ad Closed for zone: $zoneId');
  },
);
回到顶部