android - Flutter: RenderBox was not laid out when working with ListView
问题描述
I am trying to display to Listviews, one horizontal and the other is vertical. The horizontal one is working, but the vertical Listview is showing this error "RenderBox was not laid out".
I have read some other answers regarding this error here on stackoverflow, but still cant make it work.
when i run it, i get this error "RenderBox was not laid out: RenderRepaintBoundary#6bd44 relayoutBoundary=up14 NEEDS-PAINT 'package:flutter/src/rendering/box.dart':"
import 'package:flutter/material.dart';
import 'package:appone/screens/detail.dart';
import 'package:http/http.dart' as http;
import 'package:appone/screens/main_drawer.dart';
import 'dart:convert';
import 'package:appone/screens/wp-api.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Teds Pharma',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Teds Pharma'),
),
drawer: MainDrawer(),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.only(left: 22.0, right: 22, top: 40),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(
height: 20,
),
Container(
width: double.infinity,
height: 60,
margin: EdgeInsets.symmetric(horizontal: 0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(.12),
offset: Offset(0, 10),
blurRadius: 30)
]),
child: Center(
child: Padding(
padding: EdgeInsets.only(left: 18, right: 12),
child: TextField(
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Search...",
hintStyle: TextStyle(fontSize: 30),
suffixIcon: Icon(Icons.search)),
),
),
),
),
Padding(
padding: const EdgeInsets.only(top: 18.0, bottom: 18),
child: Container(
height: 180,
child: FutureBuilder(
future: fetchfiveWpPosts(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data.length,
scrollDirection: Axis.horizontal,
itemBuilder: (BuildContext context, int index) {
Map wppost = snapshot.data[index];
var imageurl =
wppost['acf']['store_logo']['url'];
return GestureDetector(
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => Detail()));
},
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: 120,
height: 160,
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(16),
),
child: ClipRRect(
borderRadius:
BorderRadius.circular(8),
child: FadeInImage.assetNetwork(
placeholder: 'assets/bg.jpg',
image: imageurl,
fit: BoxFit.cover,),
),
),
),
);
});
}
return Center(child: CircularProgressIndicator());
}),
),
),
Padding(padding: const EdgeInsets.only(top: 18.0, bottom: 18),
child: Container(
child: FutureBuilder(
future: fetchfiveWpPosts(),
builder: (context,snapshot){
if(snapshot.hasData){
return ListView.builder(
itemCount: snapshot.data.length,
scrollDirection: Axis.vertical,
itemBuilder: (BuildContext context, int index){
Map wppost = snapshot.data[index];
var imageurl = wppost['acf']['store_logo']['url'];
return Card(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
//Image.network(imageurl),
FadeInImage.assetNetwork(placeholder: 'assets/loading.gif', image: imageurl),
Text(wppost['title']['rendered']),
],
),
);
},
);
}
return CircularProgressIndicator();
},
),
),
),
SizedBox(
height: 30,
)
],
),
),
));
}
}
解决方案
当您尝试将 Scrollable 小部件放入无限高度小部件或将无限高度 ( Column
) 小部件放入可滚动 ( SingleChildScrollView
, ListView
) 时,通常会发生这种情况。
这意味着您正在尝试使用SingleChildScrollView
> Column
> ListView
>Column
您应该将以下属性添加Column
到ListView
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min, // Use children total size
children: [
ListView.builder(
itemCount : 10,
shrinkWrap: true, // Use children total size
itemBuilder : (a, b)=>Text("ffffffffffff")
)
],
),);
}
推荐阅读
- swift - 如何在新对象的属性上设置 didSet
- database - 事务传播模式(支持、requires_new 等)如何影响数据库连接池?
- javascript - 为什么当 redux 状态改变时 props.screenProps 变得未定义
- c - 具有奇怪结果的数组分配
- apache-kafka - 卡夫卡连接 FileStreamSink 不完全一次
- html - 网格重叠和 z 索引,我无法选择我的内容
- c# - 在 ASP.NET MVC razor 视图中,ValidationMessageFor 不验证输入类型文本
- microsoft-graph-api - 使用 Microsoft Graph Communications API 监控用户 Teams 通话
- c# - 将包含日期时间和时区的字符串转换为 UTC 日期时间
- javascript - 为什么输入矩阵时我的 Javascript 高斯消除程序不起作用?