Home > Net >  How do I change the alignments of a widget and all of its descendants in Flutter?
How do I change the alignments of a widget and all of its descendants in Flutter?

Time:01-21

I would like to align all my widgets to the top left in my app. To do this, it seems like I have to keep wrapping them with Align() all over the place. I would like to wrap the top of the tree once with something that makes everything below it aligned with Alignment.topLeft.

Here is the code I have:

                Column(
                  children: [
                    Align(
                        alignment: Alignment.topLeft,
                        child: Text(
                            "Average Time: ${avg(_askTimesMs).round()}ms")),
                    Align(
                        alignment: Alignment.topLeft,
                        child: Text("Timings (ms): $_askTimesMs")),
                  ],
                ),

Here is the code I want, conceptually:

                AlignDescendents(
                  alignment: Alignment.topLeft,
                  child: Column(
                    children: [
                      Text(
                          "Average Time: ${avg(_askTimesMs).round()}ms"),
                      Text("Timings (ms): $_askTimesMs"),
                    ],
                  ),
                ),

How can I do that?

CodePudding user response:

You need to use crossAxisAlignment: CrossAxisAlignment.start, on Column widget,default it is centered. mainAxisAlignment is start by default.

Column(      
  crossAxisAlignment: CrossAxisAlignment.start,
  children: [

Find more about layout

  • Related