Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: min height, Expanded and SingleChildScrollView?

I can't figure out how to do this layout in Flutter. What I'm trying to achieve is:

  • I have a column, containing a fixed height child (title Text widget) and two Expanded widgets. I'm using expanded because I want each to share half the remaining screen.
  • When orientation changes to landscape, there is not enough room to properly show the contents of the expanded widgets. So what I want is to apply a minimum height on these 2 widgets, and for it to become scrollable.

I hope that makes sense - I have no idea how to implement this. I've tried lots of combinations of Expanded, Flexible, Column, SingleChildScrollView, min column axis size etc. but everything I try results in some kind of infinite height exception. Is this even possible?

Here's some example code: the following works fine. How to implement scroll and minimum height on the two placeholders?

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Text("Title"),
            // I need to apply a minimum height on these two widgets,
            // and it should be scrollable if necessary:
            Expanded(
              child: Placeholder(),
            ),
            Expanded(
              child: Placeholder(),
            )
          ],
        ),
      ),
    );
  }
}

like image 282
James Allen Avatar asked Oct 27 '19 19:10

James Allen


People also ask

How do you set the minimum and maximum height of a container in flutter?

Are you trying to set minimum or maximum height or width of Container() widget in a Flutter, then use ' constraints ' attribute and apply BoxConstraints() on it like below.

What is SingleChildScrollView in flutter?

A box in which a single widget can be scrolled. This widget is useful when you have a single box that will normally be entirely visible, for example a clock face in a time picker, but you need to make sure it can be scrolled if the container gets too small in one axis (the scroll direction).


1 Answers

This has worked for me:

import 'dart:math'; // for max function

// Add the following around your column
LayoutBuilder(
  builder: (BuildContext context, BoxConstraints constraints) {
    return SingleChildScrollView(
      child: ConstrainedBox(
        constraints: BoxConstraints.tightFor(height: max(500, constraints.maxHeight)),
        child: Column(), // your column
      ),
    );
  },
);

Replace 500 with the minimum height you want for the Column.

like image 88
Rustem Kakimov Avatar answered Oct 27 '22 00:10

Rustem Kakimov