How to align a Column's child to the bottom

后端 未结 4 2302
后悔当初
后悔当初 2021-02-18 14:30

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

相关标签:
4条回答
  • 2021-02-18 15:04

    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()
         
          ]
        )
    
    0 讨论(0)
  • 2021-02-18 15:07

    If you are flexible to change column to a stack, you can do the following.

    body: Container(
        child: Stack(children: <Widget>[
          Text('Text 1'),
          Text('Text 2'),
          Align(
            alignment: Alignment.bottomCenter,
            child: Text(
              "Text in bottom",
            ),
          ),
        ]),
      ),
    
    0 讨论(0)
  • 2021-02-18 15:08

    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

    0 讨论(0)
  • 2021-02-18 15:21

    You can try wrapping the widgets which needs to be at the top inside another Column widget, thereby making the root widget containing only two children. 1st child containing all the widgets which need to be aligned at the top and the 2nd child containing widget which is to be placed at the bottom. Now you use mainAxisAlignment: MainAxisAlignment.spaceBetween to align 1st child to the top and second to the bottom.

    Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      mainAxisSize: MainAxisSize.max,
      children: <Widget>[
        Column(
          children: <Widget>[
            ChildA(),
            ChildB(),
          ]
        ),
        BottomAlignedChild(),
     ]
    );
    
    0 讨论(0)
提交回复
热议问题