gpt4 book ai didi

flutter - Flutter Single Child Scroll View,列中的每个项目占用父级高度的1/3

转载 作者:行者123 更新时间:2023-12-03 03:14:08 24 4
gpt4 key购买 nike

我正在尝试创建占用 parent 空间的1/3且可滚动的三对多小部件。我尝试使用FractionallySizedBox占用父级可用空间的1/3,但是,当我将其放在SingleChildScrollView中时,它会引发错误:
rendering渲染库捕获到异常
未布置RenderBox:RenderPointerListener#6dc20 relayoutBoundary = up8 NEEDS-PAINT
'package:flutter / src / rendering / box.dart':
断言失败:1702行pos 12:'hasSize'
相关的引起错误的小部件是
SingleChildScrollView
这是我的尝试

  @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Title'),
),
body: Container(height: 100, child: SingleChildScrollView(
child: Column(
children: [
for (var i = 0; i < 4; i++)
FractionallySizedBox(heightFactor: 1 / 3, child: Text("Text"),)
],
),
),
));
这就是我要使用HTML和CSS完成的工作

<style>
.container {
height: 200px;
overflow: scroll;
}
.box {
height: 33%
}
</style>

<div class="container">
<div class="box" style="background-color: red"></div>
<div class="box" style="background-color: blue"></div>
<div class="box" style="background-color: green"></div>
<div class="box" style="background-color: yellow"></div>
</div>

最佳答案

enter image description here
enter image description here
我了解您的要求并修改了建议。
每个项目的高度在200高度的容器中为200/3,并且可以滚动。

import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Title'),
),
body: Container(
height: 200,
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints viewportConstraints) {
return SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
for (var i = 0; i < 4; i++)
Container(
height: viewportConstraints.maxHeight / 3,
color: Colors.grey.withOpacity(0.8),
child: Text("Text$i"),
),
],
),
);
},
),
),
);
}
}

关于flutter - Flutter Single Child Scroll View,列中的每个项目占用父级高度的1/3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63786534/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com