ConstrainedBox Widget

What is ConstrainedBox Widget?

The ConstrainedBox is to impose additional constraints on its child widget.

Default Constructor

The default constructor creates a widget that imposes additional constraints on its child.

ConstrainedBox(
    constraints: // required argument
    child:
)

Constructor Parameters

It contains many input parameters which can be configured to change its behavior and appearance.

The constraints parameter is to add constraints to impose on the child.

The child parameter is to add the widget below the ConstrainedBox widget in a tree.

Code snippet

This code snippet shows how to configure ConstrainedBox widget.

ConstrainedBox(
    constraints: const BoxConstraints.expand(),
    child: const Card(child: Text('Hello World!')),
)

Demo Code

This examples show how to make the child widget (a Card with some Text) fill the parent, by applying BoxConstraints.expand constraints.

import 'package:flutter/material.dart';

class ConstrainedBoxDemo extends StatelessWidget {
  
  Widget build(BuildContext context) {
    return ConstrainedBox(
      constraints: const BoxConstraints.expand(),
      child: const Card(child: Text('Hello World!')),
    );
  }
}

// This Widget is the main application widget.
class MyApp extends StatelessWidget {
  static const String _title = 'ConstrainedBox demo';

  
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: Center(
          child: Material(child: ConstrainedBoxDemo()),
        ),
      ),
    );
  }
}

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