【发布时间】:2023-03-25 22:25:02
【问题描述】:
我正在尝试使用来自 Horizontally scrollable cards with Snap effect in flutter 的 PageView、PageController 和 ListView 制作轮播。但是它抛出了这个异常......
══╡渲染库╞═══════════════════════════════ ══════════════════════ I/flutter (17678):在 performResize() 期间引发了以下断言: I/flutter (17678):水平视口的高度没有限制。 I/flutter (17678):视口在交叉轴上扩展以填充其容器并约束其子项以匹配 I/flutter (17678):它们在横轴上的范围。在这种情况下,水平视口被赋予了无限量的 I/flutter (17678):要扩展的垂直空间。
有人可以帮我解决吗?
我想在 Stack 中添加这个 Carousel,其中填充了背景图像、变换类和淡入淡出过渡。
@override
void initState() {
super.initState();
controller = PageController(
initialPage: 0,
keepPage: true,
);
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
builder: (BuildContext context, Widget child) {
return Scaffold(
//BODY
body: ListView(children: <Widget>[
new Stack(
children: <Widget>[
new AspectRatio(...),
new Transform(...),
//THIS IS
new ListView.builder(
itemCount: 3,
scrollDirection: Axis.horizontal,
padding: EdgeInsets.symmetric(vertical: 16.0),
itemBuilder: (BuildContext context, int index) {
if (index % 3 == 0) {
return _buildCarousel(context, index ~/ 3);
} else {
return Divider();
}
},
),
}
}
}
Widget _buildCarousel(BuildContext context, int carouselIndex) {
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text('Carousel $carouselIndex'),
SizedBox(
// you may want to use an aspect ratio here for tablet support
height: 200.0,
child: PageView.builder(
// store this controller in a State to save the carousel scroll position
controller: PageController(viewportFraction: 0.8),
itemBuilder: (BuildContext context, int itemIndex) {
return _buildCarouselItem(context, carouselIndex, itemIndex);
},
),
)
],
);
Widget _buildCarouselItem(
BuildContext context, int carouselIndex, int itemIndex) {
return Padding(
padding: EdgeInsets.symmetric(horizontal: 4.0),
child: Container(
decoration: BoxDecoration(
color: Colors.grey,
borderRadius: BorderRadius.all(Radius.circular(4.0)),
),
),
);
【问题讨论】:
-
请分享一个工作代码,你的代码有很多格式问题...
-
这是完整代码pastebin.com/xXRkaWuR