简体   繁体   English

Flutter:为什么 Flutter 上的 Text Widget 在放置在 TextField Widget 之前时会自动居中? (内部列小部件)

[英]Flutter : Why Text Widget on Flutter automatically centered when placed before TextField Widget ? ( Inside Column Widget )

So i tried to make my TextWidget aligned to left but when i put TextWidget above ( before ) TextFieldWidget it's automatically become centered following with the TextFieldWidget, here my code所以我试图让我的 TextWidget 与左对齐,但是当我将 TextWidget 放在上面(之前)TextFieldWidget 时,它会自动与 TextFieldWidget 一起居中,这里是我的代码

import 'package:flutter/material.dart';

void main() {
  runApp(new MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Center(
            child: Text("SQLite Application"),
          ),
        ),
        body: Column(
          children: <Widget>[
            SizedBox(
              height: 20,
            ),
            Text("Name of Things"),
            TextField()
          ],
        ),
      ),
    );
  }
}

and here is the result: Code Output结果如下:代码 Output

Is there a solve to make the TextWidget to aligned to the left?有没有办法让 TextWidget 向左对齐? I've tried to use TextAlign.left on TextWidget but it wont change.我尝试在 TextWidget 上使用 TextAlign.left 但它不会改变。

You need to set the mainAxisAlignment to Start您需要将 mainAxisAlignment 设置为 Start

Column(
crossAxisAlignment: CrossAxisAlignment.start

In case you want to place the contents at the Right Side, you can use如果要将内容放在右侧,可以使用

Column(
    crossAxisAlignment: CrossAxisAlignment.end

Use利用

crossAxisAlignment: CrossAxisAlignment.start,

on the ColumnColumn

just add只需添加

Column(
      crossAxisAlignment: CrossAxisAlignment.start,

the result结果

You put your text inside Column widget.您将文本放在 Column 小部件中。 Column by default crossAxisAlignment is centre.默认情况下,crossAxisAlignment 列居中。 That's why your text is always on centre.这就是为什么你的文字总是在中心。

Apply changes on your code according to this.根据此对您的代码应用更改。

Column( crossAxisAlignment: CrossAxisAlignment.start, children:const [ SizedBox( height: 20, ), Text("your text....,"), TextField() ], ), Column(crossAxisAlignment: CrossAxisAlignment.start, children:const [SizedBox(height: 20, ), Text("你的文本....,"), TextField()], ),

And if you wanna align text without Column, use Align with with alignment.topleft.如果您想对齐没有 Column 的文本,请使用与 alignment.topleft 对齐。 It will move on left side它会向左侧移动

Align( alignment: Alignment.topLeft, child: Text("Name of Things", textAlign: TextAlign.left,)),对齐(alignment:Alignment.topLeft,孩子:文本(“事物名称”,textAlign:TextAlign.left,)),

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

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