Runtime Terror
Runtime Terror

Reputation: 6752

Full height Container inside SingleChildScrollView

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

Answers (4)

Günter Zöchbauer
Günter Zöchbauer

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

AlexF1
AlexF1

Reputation: 384

In my case just used a ListView instead of a SingleChildScrollView with a Container with height.

Upvotes: 1

kkklc
kkklc

Reputation: 167

you can try SliverFillViewport or PageView

Upvotes: 0

Rida Rezzag
Rida Rezzag

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

Related Questions