我需要使用嵌套列表,但我不能给它们一个高度,因为列表可以有自己的长度,这取决于来自服务器的信息量。我试过绘制一个变体,但它不起作用,我得到了一个错误-不正确地使用ParentDataWidget.
下面是我的密码-
return Drawer(
child: Container(
margin: const EdgeInsets.symmetric(vertical: 13, horizontal: 13),
child: ListView.builder(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: catalogDrawer.length,
itemBuilder: (BuildContext context, int index) {
var one = catalogDrawer[index].one;
return Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(catalogDrawer[index].name, style: TextStyle(fontSize: 15, fontWeight: FontWeight.w600),),
Expanded(
child: ListView.builder(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: catalogDrawer[index].one.length,
itemBuilder: (BuildContext context, int i) {
var two = catalogDrawer[index].one[i].two;
return Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
margin: const EdgeInsets.only(left: 10),
padding: const EdgeInsets.all(10),
color: Colors.yellow,
child: Text(one[i].name,style: TextStyle(fontWeight: FontWeight.w500, fontSize: 15),),
),
Expanded(
// height: two.length > 1 ? 76 : 38,
child: ListView.builder(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: catalogDrawer[index].one[i].two.length,
itemBuilder: (BuildContext context, int a) {
return Container(
margin: const EdgeInsets.only(left: 30),
padding: const EdgeInsets.all(10),
color: Colors.red,
child: Text(two[a].name, style: TextStyle(fontWeight: FontWeight.w500, fontSize: 15),),
);
},
),
),
],
)
);
},
),
),
],
)
);
},
),
),
);
我的问题可以重新表述--如何使用嵌套列表,但不要指定高度
发布于 2022-11-07 10:09:00
当您将shrinkWrap
设置为true时,您不需要在listview
中使用Expanded
小部件,所以请删除如下所示的所有Expanded
:
Drawer(
child: Container(
margin: const EdgeInsets.symmetric(vertical: 13, horizontal: 13),
child: ListView.builder(
shrinkWrap: true,
itemCount: catalogDrawer.length,
itemBuilder: (BuildContext context, int index) {
var one = catalogDrawer[index].one;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(catalogDrawer[index].name, style: TextStyle(fontSize: 15, fontWeight: FontWeight.w600),),
ListView.builder(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: catalogDrawer[index].one.length,
itemBuilder: (BuildContext context, int i) {
var two = catalogDrawer[index].one[i].two;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
margin: const EdgeInsets.only(left: 10),
padding: const EdgeInsets.all(10),
color: Colors.yellow,
child: Text(one[i].name,style: TextStyle(fontWeight: FontWeight.w500, fontSize: 15),),
),
ListView.builder(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: catalogDrawer[index].one[i].two.length,
itemBuilder: (BuildContext context, int a) {
return Container(
margin: const EdgeInsets.only(left: 30),
padding: const EdgeInsets.all(10),
color: Colors.red,
child: Text(two[a].name, style: TextStyle(fontWeight: FontWeight.w500, fontSize: 15),),
);
},
),
],
);
},
),
],
);
},
),
),
)
发布于 2022-11-07 10:05:13
因为扩展的需要父控件是列、行或Flex,所以您可以尝试:容器(边距: const EdgeInsets.symmetric(垂直: 13,水平: 13),子容器: ListView.builder( shrinkWrap: true,itemCount: 3,itemBuilder:(BuildContext context,int index) ){返回列( crossAxisAlignment: CrossAxisAlignment.start,子项:[ const Text( "catalogDrawerindex.name",style: TextStyle(fontSize: 15,fontWeight: FontWeight.w600) ),ListView.builder(物理: const NeverScrollableScrollPhysics(),shrinkWrap: true,itemCount: 2,shrinkWrap:(上下文),{返回列( crossAxisAlignment: CrossAxisAlignment.start,子列):[容器(边距: const EdgeInsets.only(左: 10),填充: const EdgeInsets.all(10),颜色: Colors.yellow,子: const文本( "onei.name",style: TextStyle( fontWeight: FontWeight.w500,fontSize: 15),) ),包装(子容器:"1","2“.map(E) =>容器(边距: const EdgeInsets.only(左: 30),填充: const EdgeInsets.all(10),颜色: Colors.red,儿童: const文本( "twoa.name",风格: TextStyle( fontWeight: FontWeight.w500,fontSize: 15),) .toList(),);},),],);},)
发布于 2022-11-07 10:17:21
class DemoDemo extends StatefulWidget {
const DemoDemo({Key? key}) : super(key: key);
@override
State<DemoDemo> createState() => _DemoDemoState();
}
class _DemoDemoState extends State<DemoDemo> {
late AppsflyerSdk _appsflyerSdk;
@override
void initState() {
// TODO: implement initState
initialize();
super.initState();
}
Future<bool> initialize() async {
final AppsFlyerOptions options = AppsFlyerOptions(
afDevKey: dotenv.env["DEV_KEY"]!,
appId: dotenv.env["APP_ID"]!,
showDebug: true,
timeToWaitForATTUserAuthorization: 15);
_appsflyerSdk = AppsflyerSdk(options);
final networkUserId = await _appsflyerSdk.getAppsFlyerUID();
_appsflyerSdk.onInstallConversionData((data) {
Adapty.updateAttribution(data,
source: AdaptyAttributionNetwork.appsflyer,
networkUserId: networkUserId);
});
await _appsflyerSdk.initSdk(
registerConversionDataCallback: true,
registerOnAppOpenAttributionCallback: true,
registerOnDeepLinkingCallback: true);
return Future<bool>.value(true);
}
@override
Widget build(BuildContext context) {
final List<CatModel1> catalogDrawer = [
CatModel1(one: [
CatModel2(two: [
CatModel3(tree: [CatModel4(name: "CatModel4")], name: "CatModel3")
], name: "CatModel2")
], name: "CatModel1")
];
return Scaffold(
body: Container(),
drawer: Drawer(
child: Container(
margin: const EdgeInsets.symmetric(vertical: 13, horizontal: 13),
child: ListView.builder(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: catalogDrawer.length,
itemBuilder: (BuildContext context, int index) {
var one = catalogDrawer[index].one;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
catalogDrawer[index].name,
style: TextStyle(fontSize: 15, fontWeight: FontWeight.w600),
),
ListView.builder(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: catalogDrawer[index].one.length,
itemBuilder: (BuildContext context, int i) {
var two = catalogDrawer[index].one[i].two;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
margin: const EdgeInsets.only(left: 10),
padding: const EdgeInsets.all(10),
color: Colors.yellow,
child: Text(
one[i].name,
style: TextStyle(
fontWeight: FontWeight.w500, fontSize: 15),
),
),
ListView.builder(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: catalogDrawer[index].one[i].two.length,
itemBuilder: (BuildContext context, int a) {
return Container(
margin: const EdgeInsets.only(left: 30),
padding: const EdgeInsets.all(10),
color: Colors.red,
child: Text(
two[a].name,
style: TextStyle(
fontWeight: FontWeight.w500, fontSize: 15),
),
);
},
),
],
);
},
),
],
);
},
),
)),
appBar: AppBar(),
);
}
}
class CatModel1 {
List<CatModel2> one;
String name;
CatModel1({required this.one, required this.name});
}
class CatModel2 {
List<CatModel3> two;
String name;
CatModel2({required this.two, required this.name});
}
class CatModel3 {
List<CatModel4> tree;
String name;
CatModel3({required this.tree, required this.name});
}
class CatModel4 {
String name;
CatModel4({required this.name});
}
https://stackoverflow.com/questions/74344644
复制相似问题