Flutter : Vertically center column

后端 未结 9 558
失恋的感觉
失恋的感觉 2020-12-13 23:02

How to vertically center a column in Flutter? I have used widget \"new Center\". I have used widget \"new Center\", but it does not vertically center my column ? Any ideas w

相关标签:
9条回答
  • 2020-12-13 23:16

    Try:

    Column(
     mainAxisAlignment: MainAxisAlignment.center,
     crossAxisAlignment: CrossAxisAlignment.center,
     children:children...)
    
    0 讨论(0)
  • 2020-12-13 23:24

    With Column, use:

    mainAxisAlignment: MainAxisAlignment.center
    

    It align its children(s) to center of its parent Space vertically

    0 讨论(0)
  • 2020-12-13 23:29

    Another Solution!

    If you want to set widgets in center vertical form, you can use ListView for it. for eg: I used three buttons and add them inside ListView which followed by

    shrinkWrap: true -> With this ListView only occupies the space which needed.

    import 'package:flutter/material.dart';
    
    class List extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        final button1 =
            new RaisedButton(child: new Text("Button1"), onPressed: () {});
        final button2 =
            new RaisedButton(child: new Text("Button2"), onPressed: () {});
        final button3 =
            new RaisedButton(child: new Text("Button3"), onPressed: () {});
        final body = new Center(
          child: ListView(
            shrinkWrap: true,
            children: <Widget>[button1, button2, button3],
         ),
        );
    
        return new Scaffold(
            appBar: new AppBar(
              title: Text("Sample"),
            ),
            body: body);
      }
    }    
    void main() {
      runApp(new MaterialApp(
        home: List(),
      ));
    }
    

    Output:

    0 讨论(0)
  • 2020-12-13 23:35

    For me the problem was there was was Expanded inside the column which I had to remove and it worked.

    Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Expanded( // remove this
                  flex: 2,
                  child: Text("content here"),
                ),
              ],
            )
    
    0 讨论(0)
  • 2020-12-13 23:36

    Try this one. It centers vertically and horizontally.

    Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: children,
      ),
    )
    
    0 讨论(0)
  • 2020-12-13 23:36

    You could use. mainAxisAlignment:MainAxisAlignment.center This will the material through the center in the column wise. `crossAxisAlignment: CrossAxisAlignment.center' This will align the items in the center in the row wise.

    Container( alignment:Alignment.center, Child: Column () )

    Simply use. Center ( Child: Column () ) or rap with Padding widget . And adjust the Padding such the the column children are in the center.

    0 讨论(0)
提交回复
热议问题