[Solved] How to align Text widgets in Row?

Question

Asked by Dmitry B on November 17, 2021 (source).

How to display hello world in Center?

enter image description here 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!"),);
  }
}

Answer

Question answered by Midhun M (source).

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.

Result

Read more about mainAxisSize.

DART FLUTTER
SHARE: