简体   繁体   中英

How to align Text widgets in Row?

How to display hello world in Center?

在此处输入图像描述 Copy-paste code:

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_navigation/src/root/get_material_app.dart';


void main() {
  runApp(MyApp());
 
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      debugShowCheckedModeBanner: false,
      home: HomeView(),
      themeMode: ThemeMode.system,
    );
  }
}

class HomeView extends StatelessWidget {
  @override 
  Widget build(BuildContext context) {
    return Scaffold( 
          appBar: AppBar(title: Text("Demo App"),),
          body: SafeArea( 
              // child: NavigationBarController.to.currentPage,
              child: Container(

                child: Row(children: [
                  WidgetTwo(),
                  WidgetOne(),
                ],),

              )
            ),

  );

  }
}

class WidgetOne extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(child: Column(children: [
      Text("aaa"),
      Text("bbb"),
      Text("ccc"),
      Text("ddd"),
      Text("eee"),
    ],)
    
    
    );
  }
}

class WidgetTwo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(child: Text("Hello Word!"),);
  }
}

Set container property alignment: Alignment.center

class WidgetTwo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
            alignment: Alignment.center
            child: Text("Hello Word!"),);
    }
}

The issue here is you have used a column widget in widget1 which has no cross axis alignment which will default to center. And a row widget in widget2 without main axis alignment which will default to start.

Try adding crossAxisAlignment:CrossAxisAlignment.start in the column widget and you will notice all text are left aligned. if you add mainAxisAlignment:MainAxisAlignment.center in the row widget the hello world will appear in the center of the row. Make sure to add mainaxisalignment in both widget2 and the main row which holds both widget1 and widget2

You can easily fix that by adding a mainAxisSize to your Column widget:

class WidgetOne extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          Text("aaa"),
          Text("bbb"),
          Text("ccc"),
          Text("ddd"),
          Text("eee"),
        ],
        mainAxisSize: MainAxisSize.min,
      ),
    );
  }
}

By default, the column will take up all the available vertical space. When you set the mainAxisSize to min, it will only take the needed space.

结果

Read more about mainAxisSize .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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