Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align a Column's child to the bottom

Tags:

flutter

dart

I'm trying to build a generic home page and I want to align the last child of my column (which contains all the widgets for the page) to the bottom of the screen but the widget wrapped in the Align is not moving. The following is what makes the most sense to me:

Column(   mainAxisSize: MainAxisSize.max,   children: <Widget>[     ChildA(),     ChildB(),     Align(       alignment: Alignment.bottomCenter,       child: BottomAlignedChild()     )   ] ) 

What am I doing wrong?

like image 917
Mattia C. Avatar asked May 24 '18 11:05

Mattia C.


People also ask

How do I align my child in Flutter?

To align a child widget within its parent you use the Align widget. If you know how to use the Center widget then you are on the right track because Center is just a special case of Align. Wrap the widget you wish to align with the Align widget and set its alignment property.

How do you align the button to the bottom in Flutter?

Flutter Bottom Button Using FloatingActionButton You can align FloatingActionButton position to center or almost anywhere using Scaffold's floatingActionButtonLocation property. Use FloatingActionButtonLocation. centerFloat to make FAB to align to bottom.


2 Answers

You can use Expanded to make the last widget expand to the whole remaining space.

import 'package:flutter/material.dart';  void main() => runApp(MyApp());  class MyApp extends StatelessWidget {   @override   Widget build(BuildContext context) {     return MaterialApp(       title: 'My Layout',       theme: ThemeData(         primarySwatch: Colors.blue,       ),       home: MyHomePage(),     );   } }  class MyHomePage extends StatelessWidget {   @override   Widget build(BuildContext context) {     return new Scaffold(       appBar: new AppBar(         title: new Text("Align Bottom Demo"),       ),       body: new Column(children: <Widget>[         new Text("Text 1"),         new Text("Text 2"),         new Expanded(             child: new Align(                 alignment: Alignment.bottomCenter,                 child: new Row(                   mainAxisAlignment: MainAxisAlignment.center,                   children: <Widget>[                     new Icon(Icons.star),                     new Text("Bottom Text")                   ],                 )))       ]),     );   } } 

Here is the result

enter image description here

like image 174
Phuc Tran Avatar answered Sep 23 '22 00:09

Phuc Tran


I have always used Spacer for these kind of cases in Column or Row. Spacer takes up all the available space between two widgets of Row/Column.

For given example, you can try following

     Column(       mainAxisSize: MainAxisSize.max,       children: <Widget>[         ChildA(),         ChildB(),         Spacer(),         BottomAlignedChild()             ]     ) 
like image 41
Irshad Kumail Avatar answered Sep 21 '22 00:09

Irshad Kumail