Reputation: 6752
I've a SingleChildScrollView
and inside that a Container
. What I want to achieve is that a Container's
height should be a full viewport height.
My code:
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,
),
),
);
}
}
If I run my code, I've got an exception:
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)
Upvotes: 20
Views: 19008
Reputation: 658037
Perhaps
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)),
),
),
),
);
}
}
Upvotes: 17
Reputation: 384
In my case just used a ListView instead of a SingleChildScrollView with a Container with height.
Upvotes: 1
Reputation: 3963
i had the same problem but the answer didn't work for me, so i had t to wrap the SingleChildScrollView() in container and give it height: double.infinity
Container(
height: double.infinity,
color: Colors.green,
child: SingleChildScrollView()
)
Upvotes: 24