gpt4 book ai didi

flutter - SingleChildScrollView 内的全高容器

转载 作者:IT王子 更新时间:2023-10-29 06:58:44 27 4
gpt4 key购买 nike

我有一个 SingleChildScrollView,里面有一个 Container。我想要实现的是 Container 的 高度应该是一个完整的视口(viewport)高度。

我的代码:

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MyApp',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
fontFamily: 'OpenSans',
),
home: SingleChildScrollView(
child: Container(
color: Colors.white,
height: double.infinity,
),
),
);
}
}

如果我运行我的代码,我有一个异常:

I/flutter ( 6293): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 6293): The following assertion was thrown during performLayout():
I/flutter ( 6293): BoxConstraints forces an infinite height.
I/flutter ( 6293): These invalid constraints were provided to RenderDecoratedBox's layout() function by the following
I/flutter ( 6293): function, which probably computed the invalid constraints in question:
I/flutter ( 6293): RenderConstrainedBox.performLayout (package:flutter/src/rendering/proxy_box.dart:258:13)
I/flutter ( 6293): The offending constraints were:
I/flutter ( 6293): BoxConstraints(w=411.4, h=Infinity)

最佳答案

也许

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final mq = MediaQueryData.fromWindow(WidgetsBinding.instance.window);
return MaterialApp(
title: 'MyApp',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
fontFamily: 'OpenSans',
),
home: SingleChildScrollView(
child: ConstrainedBox(
constraints: BoxConstraints.tightFor(
height: mq.size.height,
),
child: Container(
height: double.infinity,
decoration: BoxDecoration(
color: Colors.red,
border: Border.all(color: Colors.blue, width: 8)),
),
),
),
);
}
}

关于flutter - SingleChildScrollView 内的全高容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54389130/

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