【问题标题】:Flutter launcher - How to update app list when I uninstall an app?Flutter 启动器 - 卸载应用程序时如何更新应用程序列表?
【发布时间】:2021-03-28 05:13:24
【问题描述】:

大家好,我是 Flutter 和编程的新手。我正在尝试使用颤振创建一个 android 应用启动器。

我到处寻找,但找不到问题的答案。

我正在使用包:device_apps

所以要检索我正在使用的应用列表

List<Application> apps = await DeviceApps.getInstalledApplications();

我如何在安装/卸载应用程序时更新我的​​列表视图?

【问题讨论】:

    标签: flutter listview flutter-layout flutter-dependencies launcher


    【解决方案1】:

    DeviceApps.getInstalledApplications() 返回一个List&lt;Application&gt;,我们可以用它来绘制我们的ListView

    Widget _buildListView() {
      return ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            Application app = _apps[index];
            return Column(
              children: <Widget>[
                ListTile(
                  onTap: () => DeviceApps.openApp(app.packageName),
                  title: Text('${app.appName} (${app.packageName})'),
                ),
                Divider()
              ],
            );
          },
          itemCount: _apps.length);
    }
    

    我们可以通过覆盖StatefulWidgetinitState 方法在初始化状态后立即加载已安装的应用程序:

    List<Application> _apps = [];
    
    @override 
    void initState() {
       super.initState();
       _loadApps();
    }
    
    Future<void> _loadApps() async {
      List<Application> applications = await DeviceApps.getInstalledApplications();
      setState(() => _apps = applications);
    }
    
    Widget _buildListView() {
      return ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            Application app = _apps[index];
            return Column(
              children: <Widget>[
                ListTile(
                  onTap: () => DeviceApps.openApp(app.packageName),
                  title: Text('${app.appName} (${app.packageName})'),
                ),
                Divider()
              ],
            );
          },
          itemCount: _apps.length);
    }
    

    这样,我们也可以轻松地使用GestureDetector 重新加载已安装的应用程序:

    Widget _buildRefreshButton() {
       return GesetureDetector(
          onTap: () async { await _loadApps(); }
          child: Text("Reload apps")
       );
    }
    

    最好的解决方案是在卸载/安装应用时收到通知,但不幸的是,该软件包尚不支持此功能。

    一个 hacky 解决方法是通过 Timer.periodic 轮询上面的 _loadApps(),但是,出于性能和用户体验的原因,不建议这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-09
      • 2013-03-17
      • 2022-06-22
      • 2020-06-19
      • 1970-01-01
      • 2021-04-02
      • 2021-04-02
      • 1970-01-01
      相关资源
      最近更新 更多