Flutter应用启动插件startapp_sdk的使用
Flutter应用启动插件startapp_sdk的使用
Start.io (StartApp) SDK
这是一个Flutter插件,它使用原生平台视图来展示来自Start.io网络的广告。
支持的格式
- Banner
- 插页式广告(Interstitial)
- 激励视频(Rewarded Video)
- 原生广告(Native)
支持的平台
- Android
- iOS
安装
添加依赖
在pubspec.yaml
文件中添加:
dependencies:
startapp_sdk: '<LATEST_VERSION>'
然后从命令行安装包:
flutter pub get
Android特定设置
必须项:
将您的App ID添加到应用程序的AndroidManifest.xml
文件中,通过添加以下<meta-data>
标签。您可以在Start.io门户上找到您的App ID。对于android:value
插入您的App ID并用引号括起来,如下所示。
<!-- TODO replace YOUR_APP_ID with actual value -->
<meta-data
android:name="com.startapp.sdk.APPLICATION_ID"
android:value="YOUR_APP_ID" />
可选项:
返回广告默认是启用的。如果要禁用它,请在AndroidManifest.xml
文件中添加另一个<meta-data>
标签。
<!-- TODO Return Ad controlled by the android:value below -->
<meta-data
android:name="com.startapp.sdk.RETURN_ADS_ENABLED"
android:value="false" />
开屏广告默认是启用的。如果要禁用它,请在AndroidManifest.xml
文件中添加<provider>
标签,并在其中嵌套另一个<meta-data>
标签,如下所示。
<!-- TODO Splash Ad controlled by the android:value below -->
<provider
android:authorities="com.startapp.flutter.sdk.${applicationId}"
android:name="com.startapp.flutter.sdk.StartAppFlutterHelper"
android:exported="false">
<meta-data
android:name="com.startapp.sdk.SPLASH_ADS_ENABLED"
android:value="false" />
</provider>
iOS特定设置
必须项:
将您的App ID添加到应用程序的Info.plist
文件中,为键com.startapp.sdk.APPLICATION_ID
设置值。您可以在Start.io门户上找到您的App ID。
<!-- TODO replace YOUR_APP_ID with actual value -->
<key>com.startapp.sdk.APPLICATION_ID</key>
<string>YOUR_APP_ID</string>
可选项:
返回广告默认是启用的。如果要禁用它,在Info.plist
中为com.startapp.sdk.RETURN_ADS_ENABLED
设置NO
。
<key>com.startapp.sdk.RETURN_ADS_ENABLED</key>
<false/>
开屏广告默认是禁用的。如果要启用它,在Info.plist
中为com.startapp.sdk.SPLASH_ADS_ENABLED
设置YES
。
<key>com.startapp.sdk.SPLASH_ADS_ENABLED</key>
<true/>
使用方法
插件初始化
通过调用默认构造函数获取StartAppSdk
实例。多次调用此构造函数是安全的——您将获得相同的单例实例。
class _MyAppState extends State<MyApp> {
var startAppSdk = StartAppSdk();
}
测试模式
在应用开发期间始终使用测试模式,但在发布前不要忘记禁用它。
class _MyAppState extends State<MyApp> {
var startAppSdk = StartAppSdk();
@override
void initState() {
super.initState();
// TODO make sure to comment out this line before release
startAppSdk.setTestAdsEnabled(true);
}
}
示例代码
下面是一个完整的示例代码,展示了如何在Flutter项目中集成startapp_sdk
插件。
import 'package:flutter/material.dart';
import 'package:startapp_sdk/startapp.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var startAppSdk = StartAppSdk();
var _sdkVersion = "";
StartAppBannerAd? bannerAd;
StartAppBannerAd? mrecAd;
StartAppInterstitialAd? interstitialAd;
StartAppRewardedVideoAd? rewardedVideoAd;
StartAppNativeAd? nativeAd;
int? reward;
@override
void initState() {
super.initState();
// TODO make sure to comment out this line before release
startAppSdk.setTestAdsEnabled(true);
// TODO your app doesn't need to call this method unless for debug purposes
startAppSdk.getSdkVersion().then((value) {
setState(() => _sdkVersion = value);
});
}
@override
Widget build(BuildContext context) {
var buttonStyle = ButtonStyle(minimumSize: MaterialStateProperty.all(Size(224, 36)));
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Start.io SDK $_sdkVersion"),
),
body: SingleChildScrollView(
child: Center(
child: Column(
children: [
ElevatedButton(
style: buttonStyle,
onPressed: () {
startAppSdk.loadInterstitialAd(
prefs: const StartAppAdPreferences(adTag: 'home_screen'),
onAdDisplayed: () {
debugPrint('onAdDisplayed: interstitial');
},
onAdNotDisplayed: () {
debugPrint('onAdNotDisplayed: interstitial');
setState(() {
// NOTE interstitial ad can be shown only once
this.interstitialAd?.dispose();
this.interstitialAd = null;
});
},
onAdClicked: () {
debugPrint('onAdClicked: interstitial');
},
onAdHidden: () {
debugPrint('onAdHidden: interstitial');
setState(() {
// NOTE interstitial ad can be shown only once
this.interstitialAd?.dispose();
this.interstitialAd = null;
});
},
).then((interstitialAd) {
setState(() {
this.interstitialAd = interstitialAd;
});
}).onError<StartAppException>((ex, stackTrace) {
debugPrint("Error loading Interstitial ad: ${ex.message}");
}).onError((error, stackTrace) {
debugPrint("Error loading Interstitial ad: $error");
});
},
child: Text('Load Interstitial'),
),
ElevatedButton(
style: buttonStyle,
onPressed: (StartAppInterstitialAd? interstitialAd) {
if (interstitialAd != null) {
return () => interstitialAd.show().onError((error, stackTrace) {
debugPrint("Error showing Interstitial ad: $error");
return false;
});
} else {
return null;
}
}(interstitialAd),
child: Text('Show Interstitial'),
),
ElevatedButton(
style: buttonStyle,
onPressed: () {
startAppSdk.loadRewardedVideoAd(
prefs: const StartAppAdPreferences(adTag: 'home_screen_rewarded_video'),
onAdDisplayed: () {
debugPrint('onAdDisplayed: rewarded video');
},
onAdNotDisplayed: () {
debugPrint('onAdNotDisplayed: rewarded video');
setState(() {
// NOTE rewarded video ad can be shown only once
this.rewardedVideoAd?.dispose();
this.rewardedVideoAd = null;
});
},
onAdClicked: () {
debugPrint('onAdClicked: rewarded video');
},
onAdHidden: () {
debugPrint('onAdHidden: rewarded video');
setState(() {
// NOTE rewarded video ad can be shown only once
this.rewardedVideoAd?.dispose();
this.rewardedVideoAd = null;
});
},
onVideoCompleted: () {
debugPrint('onVideoCompleted: rewarded video completed, user gain a reward');
setState(() {
reward = reward != null ? reward! + 1 : 1;
});
},
).then((rewardedVideoAd) {
setState(() {
this.rewardedVideoAd = rewardedVideoAd;
});
}).onError<StartAppException>((ex, stackTrace) {
debugPrint("Error loading Rewarded Video ad: ${ex.message}");
}).onError((error, stackTrace) {
debugPrint("Error loading Rewarded Video ad: $error");
});
},
child: Text('Load Rewarded Video'),
),
ElevatedButton(
style: buttonStyle,
onPressed: (StartAppRewardedVideoAd? rewardedVideoAd) {
if (rewardedVideoAd != null) {
return () => rewardedVideoAd.show().onError((error, stackTrace) {
debugPrint("Error showing Rewarded Video ad: $error");
return false;
});
} else {
return null;
}
}(rewardedVideoAd),
child: Text(reward != null ? 'Show Rewarded Video ($reward)' : 'Show Rewarded Video'),
),
bannerAd != null
? StartAppBanner(bannerAd!)
: ElevatedButton(
style: buttonStyle,
onPressed: () {
startAppSdk.loadBannerAd(
StartAppBannerType.BANNER,
prefs: const StartAppAdPreferences(adTag: 'primary'),
onAdImpression: () {
debugPrint('onAdImpression: banner');
},
onAdClicked: () {
debugPrint('onAdClicked: banner');
},
).then((bannerAd) {
setState(() {
this.bannerAd = bannerAd;
});
}).onError<StartAppException>((ex, stackTrace) {
debugPrint("Error loading Banner ad: ${ex.message}");
}).onError((error, stackTrace) {
debugPrint("Error loading Banner ad: $error");
});
},
child: Text('Show Banner'),
),
mrecAd != null
? StartAppBanner(mrecAd!)
: ElevatedButton(
style: buttonStyle,
onPressed: () {
startAppSdk.loadBannerAd(
StartAppBannerType.MREC,
prefs: const StartAppAdPreferences(adTag: 'secondary'),
).then((mrecAd) {
setState(() {
this.mrecAd = mrecAd;
});
}).onError<StartAppException>((ex, stackTrace) {
debugPrint("Error loading Mrec ad: ${ex.message}");
}).onError((error, stackTrace) {
debugPrint("Error loading Mrec ad: $error");
});
},
child: Text('Show Mrec'),
),
nativeAd != null
? Container(
color: Colors.blueGrey.shade50,
child: StartAppNative(
nativeAd!,
(context, setState, nativeAd) {
return Row(
children: [
nativeAd.imageUrl != null
? SizedBox(
child: Padding(
padding: const EdgeInsets.all(8),
child: Image.network(nativeAd.imageUrl!),
),
width: 160,
height: 160,
)
: Container(),
Flexible(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Title: ${nativeAd.title}', maxLines: 1),
Text('Description: ${nativeAd.description}', maxLines: 1),
Text('Rating: ${nativeAd.rating}', maxLines: 1),
Text('Installs: ${nativeAd.installs}', maxLines: 1),
Text('Category: ${nativeAd.category}', maxLines: 1),
Text('Campaign: ${nativeAd.campaign}', maxLines: 1),
Text('Call to action: ${nativeAd.callToAction}', maxLines: 1),
Text('Image 1: ${cutString(nativeAd.imageUrl, 20)}', maxLines: 1),
Text('Image 2: ${cutString(nativeAd.secondaryImageUrl, 20)}', maxLines: 1),
],
),
),
],
);
},
height: 180,
),
)
: ElevatedButton(
style: buttonStyle,
onPressed: () {
startAppSdk.loadNativeAd(
prefs: const StartAppAdPreferences(adTag: 'game_over'),
onAdImpression: () {
debugPrint('onAdImpression: nativeAd');
},
onAdClicked: () {
debugPrint('onAdClicked: nativeAd');
},
).then((nativeAd) {
setState(() {
this.nativeAd = nativeAd;
});
}).onError<StartAppException>((ex, stackTrace) {
debugPrint("Error loading Native ad: ${ex.message}");
}).onError((error, stackTrace) {
debugPrint("Error loading Native ad: $error");
});
},
child: Text('Show Native'),
),
],
),
),
),
),
);
}
String? cutString(String? input, int length) {
if (input != null && input.length > length) {
return input.substring(0, length) + '...';
}
return input;
}
}
这个示例代码涵盖了加载和显示各种类型的广告,包括插页式广告、激励视频、横幅广告、MREC广告和原生广告。每个广告类型都有相应的按钮来触发加载和显示操作。
更多关于Flutter应用启动插件startapp_sdk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter应用启动插件startapp_sdk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter应用中集成并使用startapp_sdk
插件的一个示例。startapp_sdk
插件通常用于在应用启动时展示广告或其他启动屏幕内容。以下步骤假定你已经有一个Flutter项目,并且熟悉基本的Flutter开发流程。
1. 添加依赖
首先,在你的pubspec.yaml
文件中添加startapp_sdk
依赖:
dependencies:
flutter:
sdk: flutter
startapp_sdk: ^最新版本号 # 请替换为最新的版本号
然后运行flutter pub get
来安装依赖。
2. 配置Android和iOS
Android
在android/app/src/main/AndroidManifest.xml
中添加必要的权限和配置(如果插件文档有特定要求):
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourapp">
<!-- 添加必要的权限 -->
<!-- 如果startapp_sdk文档有特定权限要求,请在这里添加 -->
<application
...>
<!-- 如果startapp_sdk文档有特定配置要求,请在这里添加 -->
</application>
</manifest>
在android/app/build.gradle
文件中,确保你的minSdkVersion
和targetSdkVersion
符合startapp_sdk
的要求。
iOS
对于iOS,通常插件会自动处理大部分配置。但是,你可能需要在Info.plist
中添加一些特定的配置,或者根据startapp_sdk
的文档进行其他设置。
3. 使用插件
在你的Flutter代码中,你可以这样使用startapp_sdk
插件:
import 'package:flutter/material.dart';
import 'package:startapp_sdk/startapp_sdk.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
// 初始化startapp_sdk
_initializeStartApp();
}
Future<void> _initializeStartApp() async {
// 替换为你的StartApp应用ID和广告ID
String appId = "你的StartApp应用ID";
String adId = "你的StartApp广告ID";
try {
// 初始化SDK
await StartApp.init(appId, adId: adId);
// 显示启动广告
await StartApp.showAd();
} catch (e) {
print("StartApp initialization failed: $e");
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter StartApp SDK Demo'),
),
body: Center(
child: Text('Check your log for StartApp initialization status.'),
),
);
}
}
注意事项
- 替换ID:确保你替换了
appId
和adId
为你从StartApp平台获取的实际值。 - 错误处理:在实际应用中,你可能需要更详细的错误处理逻辑。
- 平台特定配置:根据
startapp_sdk
的文档,你可能需要在Android和iOS项目中添加额外的配置。
这个示例展示了如何在Flutter应用中集成并使用startapp_sdk
插件来显示启动广告。如果你需要更详细的功能,请参考startapp_sdk
的官方文档。