我问了一个题为“如何将小部件注入自定义子小部件并使用子小部件迭代索引的问题?”我已经有了答案,但是有问题。
答案说我应该:
“您可以传递一个生成器函数,该函数返回一个Widget并根据需要采用参数,例如索引和所需的任何配置,而不是将Widget传递给您的自定义AppList,如下所示:
函数定义:
typedef Widget MyListTileBuilder(String tileText);
然后更改以下内容:
final Widget child;
到
final MyListTileBuilder childBuilder;
当然,您需要在示例类中实现构建器方法:
Widget MyListTileBuilderImplementation (int index) {
return ListTile (
title: Text(installedApps[index]["app_name"]) //this is the text
),
}
在示例类中构建AppList时,您将传递方法
AppList (
childBuilder: MyListTileBuilderImplementation
)
最后在AppList中调用构建器,而不是添加子小部件”:
itemBuilder: (context, index) {
return childBuilder(index); //This is where the ListTile will go.
},
因此,我尝试编辑代码。但是由于某种原因,它无法正常工作。我认为我做错了。有人知道如何解决这个问题吗? (如果您可以编辑我的完整代码,那将非常有帮助。)
完整代码:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:async';
import 'dart:io';
import 'package:flutter_appavailability/flutter_appavailability.dart';
void main() {
SystemChrome.setEnabledSystemUIOverlays([]);
runApp(Example());
}
Future<void> getApp() async {
if (Platform.isAndroid) {
installedApps = await AppAvailability.getInstalledApps();
print(await AppAvailability.checkAvailability("com.android.chrome"));
print(await AppAvailability.isAppEnabled("com.android.chrome"));
}
else if (Platform.isIOS) {
installedApps = iOSApps;
print(await AppAvailability.checkAvailability("calshow://"));
}
}
List<Map<String, String>> installedApp;
List<Map<String, String>> installedApps;
List<Map<String, String>> iOSApps = [
{
"app_name": "Calendar",
"package_name": "calshow://"
},
{
"app_name": "Facebook",
"package_name": "fb://"
},
{
"app_name": "Whatsapp",
"package_name": "whatsapp://"
}
];
class Example extends StatefulWidget {
@override
ExampleState createState() => ExampleState();
}
class ExampleState extends State<Example> {
@override
Widget build(BuildContext context) {
return MaterialApp (
debugShowCheckedModeBanner: false,
home: Scaffold (
body: Container (
color: Colors.black,
child: AppList ()
)
),
);
}
}
class AppList extends StatefulWidget {
@override
AppListState createState() => AppListState();
AppList({Key key, this.child}) : super(key: key);
final Widget child;
}
class AppListState extends State<AppList> {
Widget child;
List<Map<String, String>> _installedApps;
@override
void initState() {
super.initState();
}
getApps() {
setState(() {
installedApps = _installedApps;
getApp();
});
}
@override
Widget build(BuildContext context) {
if (installedApps == null)
getApps();
return ListView.builder(
itemCount: installedApps == null ? 0 : installedApps.length,
itemBuilder: (context, index) {
return ListTile (
title: Text(installedApps[index]["app_name"])
);
},
);
}
}
答案 0 :(得分:2)
这是您所做的更改示例:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:async';
import 'dart:io';
//import 'package:flutter_appavailability/flutter_appavailability.dart';
typedef Widget MyListTileBuilder(int index);
void main() {
SystemChrome.setEnabledSystemUIOverlays([]);
runApp(Example());
}
Future<void> getApp() async {
if (Platform.isAndroid) {
//installedApps = await AppAvailability.getInstalledApps();
//print(await AppAvailability.checkAvailability("com.android.chrome"));
//print(await AppAvailability.isAppEnabled("com.android.chrome"));
}
else if (Platform.isIOS) {
installedApps = iOSApps;
//print(await AppAvailability.checkAvailability("calshow://"));
}
}
List<Map<String, String>> installedApp;
List<Map<String, String>> installedApps=[
{"app_name":"app1"},
{"app_name":"app2"},
{"app_name":"app3"},
];
List<Map<String, String>> iOSApps = [
{
"app_name": "Calendar",
"package_name": "calshow://"
},
{
"app_name": "Facebook",
"package_name": "fb://"
},
{
"app_name": "Whatsapp",
"package_name": "whatsapp://"
}
];
class Example extends StatefulWidget {
@override
ExampleState createState() => ExampleState();
}
class ExampleState extends State<Example> {
Widget MyListTileBuilderImplementation (int index) {
return ListTile (
title: Text(installedApps[index]["app_name"] + " index:" + index.toString()) //this is the text
);
}
@override
Widget build(BuildContext context) {
return MaterialApp (
debugShowCheckedModeBanner: false,
home: Scaffold (
body: Container (
color: Colors.white,
child: AppList (childBuilder: this.MyListTileBuilderImplementation)
)
),
);
}
}
class AppList extends StatefulWidget {
@override
AppListState createState() => AppListState();
AppList({Key key, this.childBuilder}) : super(key: key);
final MyListTileBuilder childBuilder;
}
class AppListState extends State<AppList> {
List<Map<String, String>> _installedApps;
@override
void initState() {
super.initState();
}
getApps() {
setState(() {
installedApps = _installedApps;
getApp();
});
}
@override
Widget build(BuildContext context) {
if (installedApps == null)
getApps();
return ListView.builder(
itemCount: installedApps == null ? 0 : installedApps.length,
itemBuilder: (context, index) {
return widget.childBuilder(index);
},
);
}
}