AppScreenC
将以ListView
提供所有已安装的应用程序
class AppScreenC extends StatefulWidget {
@override
_AppScreenCState createState() => _AppScreenCState();
}
List<Application> apps;
getApps() async {
if (apps == null) {
apps = await DeviceApps.getInstalledApplications(
onlyAppsWithLaunchIntent: true,
includeSystemApps: true,
includeAppIcons: true);
apps.sort((a, b) => a.appName.compareTo(b.appName));
}
}
屏幕上将显示具有所有已安装应用程序的ListView。我正在尝试根据onTap
事件更改图标。但是,单击列表后,图标没有改变。
class _AppScreenCState extends State<AppScreenC> {
final _app = <Application>[];
@override
Widget build(BuildContext context) {
return _buildApps();
}
Widget _buildApps() {
getApps();
return ListView.builder(itemBuilder: (BuildContext context, int index) {
_app.addAll(apps);
return _buildRow(_app[index]);
});
}
Widget _buildRow(ApplicationWithIcon app) {
bool selected = false;
return ListTile(
leading: Image.memory(app.icon, height: 40),
trailing:
Icon(selected ? Icons.check_circle : Icons.check_circle_outline),
title: Text(app.appName),
onTap: () {
selected = !selected;
// print("$selected");
// print("${app.apkFilePath}");
setState(() {});
},
);
}
}
最佳答案
itemCount:
中缺少ListView.Builder
Widget _buildApps() {
getApps();
return ListView.builder(itemBuilder: (BuildContext context, int index) {
_app.addAll(apps);
return _buildRow(_app[index]);
}, itemCount: _app.length);
}
也,class _AppScreenCState extends State<AppScreenC> {
bool selected = false; // this should be at the top as it will persist the value
关于android - 无法在Flutter中设置SetState(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63682102/