我的代码:
var _body = Builder(builder: (BuildContext context) {
return Padding(
padding: EdgeInsets.only(
top: 340),
child: Column(
children: [
SizedBox(height: 10),
_customWidget,
SizedBox(height: 10),
Expanded(
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
CustomWidget(
txt: 'item1',
txt2: 'info',
callback: () {
_showDialog(context);
},
),
CustomWidget(
txt: 'item1',
txt2: 'info',
callback: () {
_showDialog(context);
},
},
),
CustomWidget(
txt: 'item1',
txt2: 'info',
callback: () {
_showDialog(context);
},
),
_customWidget,
_customWidget,
],
)),
)
],
),
);
抛出了另一个异常:RenderFlex 子级具有非零 Flex,但传入的宽度约束是无界的。 抛出另一个异常:RenderBox 未布局:RenderFlex#97324 relayoutBoundary=up40 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
我尝试过将孩子包裹成展开的,但这不起作用。 我无法向滚动视图添加任何高度,因为滚动视图内的子项仅在条件正确时才会显示。因此列表的高度取决于将显示多少个子项。
我想你想做一个高度有限的
SingleChildScrollView
。
您可以用
Container
或 SizedBox
包裹它并指定您想要的高度。
例如这样:
import 'package:flutter/material.dart';
void main() {
runApp(const TstApp());
}
class TstApp extends StatelessWidget {
const TstApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Tst',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
HomePage({super.key});
final Widget _customWidget = CustomWidget(
txt: 'txt',
txt2: 'txt2',
callBack: () {},
);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.only(top: 10), // was: 340
child: Column(
children: [
const SizedBox(height: 10),
_customWidget,
const SizedBox(height: 10),
SizedBox(
height: 300,
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.start,
children: [
CustomWidget(
txt: 'item1',
txt2: 'info',
callBack: () {
_showDialog(context);
},
),
CustomWidget(
txt: 'item2',
txt2: 'info',
callBack: () {
_showDialog(context);
},
),
CustomWidget(
txt: 'item3',
txt2: 'info',
callBack: () {
_showDialog(context);
},
),
_customWidget,
_customWidget,
],
),
),
),
],
),
),
);
}
void _showDialog(BuildContext context) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Alert'),
content: const Text('This is a simple alert dialog.'),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('OK'),
),
],
);
},
);
}
}
class CustomWidget extends StatelessWidget {
const CustomWidget({
super.key,
required this.txt,
required this.txt2,
this.callBack,
});
final String txt;
final String txt2;
final VoidCallback? callBack;
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(txt),
subtitle: Text(txt2),
onTap: callBack,
);
}
}