1

我想在我现有的应用程序中实现是否安装了特定应用程序的条件。

条件:

the first app is that which is I'm going to build

Second app that is required for run first app

  1. 如果安装了应用程序(第二个应用程序),则HomePage()在第一个应用程序中运行
  2. 如果未安装应用程序,则显示安装应用程序的弹出窗口或警报。

第一个应用程序的根代码

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
    future: Future.delayed(Duration(seconds: 2)),
        builder: (context, AsyncSnapshot snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return MaterialApp(
              home: Splash(),
              debugShowCheckedModeBanner: false,
            );
          } else {
            return StreamBuilder(
              stream: Connectivity().onConnectivityChanged,
              builder: (context, AsyncSnapshot<ConnectivityResult> snapshot) {
                return snapshot.data == ConnectivityResult.mobile ||
                        snapshot.data == ConnectivityResult.wifi
                    ? MaterialApp(
                        title: 'mFollower',
                        theme: ThemeData(
                          primarySwatch: Colors.blue,
                        ),
                        home: Homepage(),
                        debugShowCheckedModeBanner: false,
                      )
                    : NoInternet();
              },
            );
          }

        });
  }
}
4

1 回答 1

1

这可以使用包device_apps包来实现。这将返回 Android 设备中安装的应用程序列表。

// All the apps installed in the device
List<Application> apps = await DeviceApps.getInstalledApplications();

因此,根据您的要求,您可以执行以下操作

Future<bool> isAppInstalled() {
    return DeviceApps.getInstalledApplications().then((value) =>
        value.any((element) => element.packageName == 'com.example.app')); // Your app package id to check.
}

内部build方法使用 Future builder 来获取结果。

FutureBuilder<bool>(
        future: apps(),
        builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
          if (snapshot.hasData && snapshot.data) {
            // App is installed
            return Container(
              color: Colors.green,
            );
          } else {
            // App is not installed
            return Container(color: Colors.redAccent);
          }
        })

不幸的是,此插件仅支持 ANDROID

于 2021-07-09T12:57:07.700 回答