简体   繁体   English

如何在颤动中使列居中?

[英]How to Center a Column in flutter?

I am very new to flutter and I am trying to implement a UI with 3 buttons.我对颤振非常陌生,我正在尝试实现一个带有 3 个按钮的 UI。 There, I want to center those buttons in both vertically and horizontally.在那里,我想将这些按钮垂直和水平居中。 But I can't achieve it但我无法实现

I tried the given example in here and also tried using Center tag but neither worked for me.我在这里尝试了给定的示例,也尝试使用Center标签,但都不适合我。 I think I have added those lines in a wrong place but I can't figure out if it is the problem我想我在错误的地方添加了这些行,但我不知道这是否是问题所在

Below is my implementation :-以下是我的实现:-

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
import 'package:pan_asia_bank_app/screens/ChangePassword.dart';
import 'package:pan_asia_bank_app/widgets/NavDrawer.dart';

class ManageAccount extends StatelessWidget{

  Widget _buttons(name, BuildContext context){
    return Center(
        child: ButtonBar(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            ButtonTheme(
              minWidth: 200,
                child:RaisedButton(
                  child: new Text(name),
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(100),
                      side: BorderSide(color: Colors.red)
                  ),
                  color: Colors.red,
                  textColor: Colors.white,
                  onPressed: (){
                    if(name == 'Change Password'){
                      Navigator.push(context, MaterialPageRoute(builder: (context) => ChangePassword()));
                    }
                  },
                )
            ),
          ],
        )
    );
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        drawer: NavDrawer(),
        appBar: AppBar(
          // Here we take the value from the MyHomePage object that was created by
          // the App.build method, and use it to set our appbar title.
          title: Image.asset("assets/logo.png", fit: BoxFit.cover),
        ),
        body: Container(
            margin: const EdgeInsets.only(top: 10),
            padding: EdgeInsets.symmetric(horizontal: 20),
            child: SingleChildScrollView(
                child: Center(
                  child: Column(
                    children: [
                      Container(
                        alignment: Alignment.topLeft,
                        margin: const EdgeInsets.only(left: 15, top: 20, bottom: 20),
                        child: HtmlWidget("""<h2 style='color:red;'>Manage Account</h2>"""),
                      ),
                      Container(
//                        margin: const EdgeInsets.only(left: 25, top: 200, right: 25),
                        alignment: Alignment.center,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            _buttons('Change Username', context)
                          ],
                        ),
                      ),
                      Container(
//                        margin: const EdgeInsets.only(left: 110, top: 25, right: 25),
                        alignment: Alignment.centerLeft,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            _buttons('Change Password', context)
                          ],
                        ),
                      ),
                      Container(
//                        margin: const EdgeInsets.only(left: 110, top: 25, right: 25),
                        alignment: Alignment.centerLeft,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            _buttons('Change Contact No', context)
                          ],
                        ),
                      ),
                    ],
                  ),

                ),
            ),
        )
    );
//    Container(
//      child: Align(
//        alignment: Alignment.center,
//          _buttons()
//      ),
//    );
  }

}

What are the changes need to be done in order to get the expected UI?为了获得预期的 UI,需要进行哪些更改? Massive thanks for your help!非常感谢您的帮助!

Wrap your Column with Row widget and write in row :Row小部件包裹您的Column并在 row 中写入:

mainAxisAlignment: MainAxisAlignment.center

See below example for mor idea:有关更多想法,请参见以下示例:

Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Column(
              children: <Widget>[

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM