Flutter fill available space in scroll view

Viewed 1086

In the following code I have a SingleChildScrollView, I used ConstrainedBox to set a minHeight for its content

I want to force blue content to fill available space, I cannot use Expanded because we are in scroll view, As I? set minHeight before we can determine the available space some how

Actually I want to set a minHeight constraint to blue container, here red container has height: 500 but in real case it is unknown

  • make blue container as big as possible if device height it big enough and we have no scroll
  • it device height is not big enough, and we have scroll, set height of blue box as is (auto)
import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new LayoutBuilder(
      builder: (BuildContext context, BoxConstraints viewportConstraints) {
        return SingleChildScrollView(
          child: ConstrainedBox(
            constraints:
                BoxConstraints(minHeight: viewportConstraints.maxHeight),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Container(
                  color: Colors.green[300],
                  height: 500,
                  child: Placeholder(),
                ),
                Container(
                  color: Colors.blue[300],
                  child: Text('Expand me!'),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
1 Answers

You need to wrap column with IntrinsicHeight.

Related