Flutter Google Play Instant插件google_play_instant的使用

Flutter Google Play Instant插件google_play_instant的使用

特性

  • 检查当前运行的应用是否为即时应用。
  • 显示安装提示给用户。

开始使用

首先,初始化 GooglePlayInstant 对象:

final googlePlayInstant = GooglePlayInstant();

检查当前应用是否为即时应用:

final isInstantApp = await googlePlayInstant.isInstantApp();

显示安装提示(原生Android对话框):

googlePlayInstant.showInstallPrompt();

完整示例

以下是一个完整的示例代码,展示了如何在Flutter应用中使用 google_play_instant 插件。

import 'package:flutter/material.dart';

import 'package:google_play_instant/google_play_instant.dart';

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

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String isInstantApp = '-------';  // 用于存储是否为即时应用的结果
  final _googlePlayInstantPlugin = GooglePlayInstant();  // 初始化插件对象

  [@override](/user/override)
  void initState() {
    super.initState();
    isInstantAppGetter();  // 调用异步方法获取即时应用状态
  }

  // 异步方法,用于获取即时应用状态并更新UI
  isInstantAppGetter() async {
    isInstantApp = (await _googlePlayInstantPlugin.isInstantApp()).toString();
    setState(() {});  // 更新UI
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('插件示例应用'),
        ),
        body: Center(
          child: Column(
            children: [
              // 显示是否为即时应用的结果
              Text('运行于: $isInstantApp\n'),
              // 提供一个按钮,点击时显示安装提示
              TextButton(
                onPressed: () {
                  _googlePlayInstantPlugin.showInstallPrompt();  // 显示安装提示
                },
                child: const Text('安装'),
              )
            ],
          ),
        ),
      ),
    );
  }
}

更多关于Flutter Google Play Instant插件google_play_instant的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

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


Google Play Instant 允许用户在安装应用程序之前先体验应用程序的部分功能。在 Flutter 中,你可以使用 google_play_instant 插件来实现这一功能。以下是如何使用 google_play_instant 插件的步骤:

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  google_play_instant: ^1.0.0  # 请检查最新版本

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

2. 初始化插件

在你的 Dart 代码中,你需要在应用启动时初始化 google_play_instant 插件。通常,你可以在 main.dart 文件中进行初始化:

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

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await GooglePlayInstant.initialize();
  runApp(MyApp());
}

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

3. 检查是否在 Instant 模式下运行

你可以使用 GooglePlayInstant.isInstantApp() 方法来检查应用是否在 Instant 模式下运行。根据这个结果,你可以调整应用的行为:

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Demo Home Page'),
      ),
      body: Center(
        child: FutureBuilder<bool>(
          future: GooglePlayInstant.isInstantApp(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting) {
              return CircularProgressIndicator();
            } else if (snapshot.hasError) {
              return Text('Error: ${snapshot.error}');
            } else {
              final isInstantApp = snapshot.data ?? false;
              return Text('Is Instant App: $isInstantApp');
            }
          },
        ),
      ),
    );
  }
}

4. 处理 Instant 模式下的特定逻辑

如果应用在 Instant 模式下运行,你可能需要限制某些功能或展示特定的 UI。例如,你可以禁用某些需要完整安装的功能:

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Demo Home Page'),
      ),
      body: Center(
        child: FutureBuilder<bool>(
          future: GooglePlayInstant.isInstantApp(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting) {
              return CircularProgressIndicator();
            } else if (snapshot.hasError) {
              return Text('Error: ${snapshot.error}');
            } else {
              final isInstantApp = snapshot.data ?? false;
              return Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text('Is Instant App: $isInstantApp'),
                  if (isInstantApp)
                    Text('Some features are not available in Instant App mode.')
                  else
                    ElevatedButton(
                      onPressed: () {
                        // 完整应用的功能
                      },
                      child: Text('Full App Feature'),
                    ),
                ],
              );
            }
          },
        ),
      ),
    );
  }
}

5. 发布 Instant App

要发布 Instant App,你需要按照 Google Play 的要求配置你的应用。这包括:

  • AndroidManifest.xml 中添加 <dist:module dist:instant="true" />
  • 确保你的应用符合 Instant App 的大小限制(通常为 10MB)。
  • 在 Google Play Console 中配置 Instant App 的发布流程。

6. 测试 Instant App

在开发过程中,你可以使用 Android Studio 的 Instant App 工具来测试你的应用。确保在模拟器或真实设备上测试 Instant App 的行为。

7. 处理用户升级到完整应用

如果用户从 Instant App 升级到完整应用,你可能需要处理数据迁移或其他逻辑。你可以使用 GooglePlayInstant.showInstallPrompt() 方法来提示用户安装完整应用:

ElevatedButton(
  onPressed: () async {
    await GooglePlayInstant.showInstallPrompt();
  },
  child: Text('Install Full App'),
);
回到顶部