【问题标题】:How to align flutter text如何对齐颤动的文本
【发布时间】:2021-06-21 10:17:32
【问题描述】:

我是 Flutter 的初学者,我已将我的 Flutter 页面添加到文本字段和文本中,任何人都知道如何对齐姓氏,就像填充右侧的文本一样。

这是我的代码:

import 'dart:ui';

import 'package:crmapp/pages/create_password/password_screen.dart';
import 'package:crmapp/widgets/components/alert.dart';
import 'package:crmapp/widgets/components/page_animation.dart';
import 'package:crmapp/widgets/theme/constants.dart';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:crmapp/provider/theme_provider.dart';
class SignUpScreen extends StatefulWidget {
  @override
  _SignUpScreenState createState() => _SignUpScreenState();
}

class _SignUpScreenState extends State< SignUpScreen > {
  final _controller = TextEditingController();
  //validation controller
  TextEditingController fNameController = new TextEditingController();
  TextEditingController lNameController = new TextEditingController();
  TextEditingController nickNameController = new TextEditingController();
  TextEditingController phoneController = new TextEditingController();

  bool _isButtonEnabled = false;
  //final _controller = TextEditingController();
  bool isConfirm=false;
  check (BuildContext context){
    if(fNameController.text.isNotEmpty &&
        lNameController.text.isNotEmpty &&
        nickNameController.text.isNotEmpty &&
        phoneController.text.isNotEmpty){
      setState(() {
        _isButtonEnabled = true;
      });
    } else {
      setState(() {
        _isButtonEnabled = false;
      });
    }


  }
  checks (BuildContext context){

    if(phoneController.text.isEmpty){
      showDialog<void>(
          context: context,
          builder: (BuildContext dialogContext) {
            return Alert(title: "Alert !!!",subTile: "Phone number is required !",);
          }
      );
    }else if(phoneController.text.length > 0 && phoneController.text.length < 10){
      isConfirm = true;
      showDialog<void>(
          context: context,
          builder: (BuildContext dialogContext) {
            return Alert(title: "Alert !!!",subTile: "Invalid phone number !",);
          }
      );
    }
  }
  String? validateMobile(String value) {
    String pattern = r'(^[0-9]*$)';
    RegExp regExp = new RegExp(pattern);
    if (value.length == 0 ) {
      return "Invalid phone no";
    }
    return null;
  }
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    /*   double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;*/
    Provider.of<ThemeProvider>(context).themeMode == ThemeMode.dark
        ? 'DarkTheme'
        : 'LightTheme';



    return Scaffold(
      resizeToAvoidBottomInset: false,

      body: SafeArea(

        child: Stack(
          fit: StackFit.expand,
          children: [


            _signUp(),

          ],


        ),

      ),


    );

  }


  Widget _signUp() {

    return Container(
      constraints: BoxConstraints.expand(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
      ),
      decoration: BoxDecoration(
        gradient: LinearGradient(
          colors: [
            Color(0xFF2A3476),
            Color(0xFF2A3476),
          ],
          begin: Alignment.topLeft,
          end: Alignment.centerRight,
        ),
      ),
       child: Form(
    key: formKey,

    child: Container(
      child: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Padding(
              padding:
              const EdgeInsets.symmetric(vertical: 36.0, horizontal: 24.0),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.end,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    "Create Account",
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 30.0,fontFamily: "medium",
                      fontWeight: FontWeight.w800,
                    ),
                  ),

                ],
              ),
            ),
            Container(
              width: double.infinity,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(30),
                  topRight: Radius.circular(30),
                ),
              ),
              child: Padding(
                padding: const EdgeInsets.all(24.0),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [

                    Text(
                      "Hello, sign up to",
                      style: TextStyle(
                        fontSize: 22,
                        fontFamily: "regular",
                        fontWeight: FontWeight.w300,
                        color: Colors.black,
                      ),
                    ),
                    Text(
                      "continue",
                      style: TextStyle(
                        fontSize: 22,
                        fontFamily: "regular",
                        fontWeight: FontWeight.w300,
                        color: Colors.black,
                      ),
                    ),
                    SizedBox(
                      height: 20.0,
                    ),
                    Row(
                      children: <Widget>[
                        Flexible(
                          flex: 0,
                          child:       Text(
                            'First Name',
                            style:
                            TextStyle(

                              fontSize: 15,
                              fontFamily: "regular",
                            ),
                          )
                        ),
                        SizedBox(
                          width:0,
                        ),
                        Flexible(
                          flex: 5,
                          child:   Text(
                            'Last Name',
                            style:
                            TextStyle(

                              fontSize: 15,
                              fontFamily: "regular",
                            ),
                          )
                        ), SizedBox(
                          width: 0,
                        ),
                      ],
                    ), SizedBox(
                      height: 12.0,
                    ),
                    Row(
                      children: <Widget>[
                        Flexible(
                          flex: 1,
                          child:   TextField(
                            controller: lNameController,
                            onChanged: (val){
                              check(context);
                            },
                            /* keyboardType: TextInputType.emailAddress,*/
                            decoration: InputDecoration(
                              contentPadding: EdgeInsets.all(8),
                              border: OutlineInputBorder(
                                borderRadius: BorderRadius.circular(8.0),
                                borderSide: BorderSide.none,

                              ),
                              filled: true,
                              fillColor: Color(0xFFE1E8F7),
                              hintText: "",
                              /*   prefixIcon: Icon(
                            Icons.people_outline_rounded,
                            color: Colors.grey[600],
                          )*/),
                          ),
                        ),
                        SizedBox(
                          width: 10,
                        ),
                        Flexible(
                          flex: 1,
                          child:       TextField(
                            controller: lNameController,
                            onChanged: (val){
                              check(context);
                            },
                            /* keyboardType: TextInputType.emailAddress,*/
                            decoration: InputDecoration(
                              contentPadding: EdgeInsets.all(8),
                              border: OutlineInputBorder(
                                borderRadius: BorderRadius.circular(8.0),
                                borderSide: BorderSide.none,

                              ),
                              filled: true,
                              fillColor: Color(0xFFE1E8F7),
                              hintText: "",
                              /*   prefixIcon: Icon(
                            Icons.people_outline_rounded,
                            color: Colors.grey[600],
                          )*/),
                          ),
                        ),
                      ],
                    ),


                    SizedBox(
                      height: 20.0,
                    ),

                    Text(
                      'Nick Name',
                      style:
                      TextStyle(  fontSize: 15,
                        fontFamily: "regular",),
                    ),
                    SizedBox(
                      height: 12.0,
                    ),
                    TextField(
                      /* keyboardType: TextInputType.emailAddress,*/

                      controller: nickNameController,
                      onChanged: (val){
                        check(context);
                      },
                      decoration: InputDecoration(
                        contentPadding: EdgeInsets.all(8),
                          border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(8.0),
                            borderSide: BorderSide.none,
                          ),
                          filled: true,
                          fillColor: Color(0xFFE1E8F7),
                          hintText: "",
                          /*prefixIcon: Icon(
                            Icons.people_outline_rounded,
                            color: Color(0xFFE1E8F7),
                          )*/),
                    ),

                    SizedBox(
                      height: 20.0,
                    ),
                    Text(
                      'Mobile Number',
                      style:
                      TextStyle(  fontSize: 15,
                        fontFamily: "regular",),
                    ),
                    SizedBox(
                      height: 12.0,
                    ),
                    TextFormField(

                      controller: phoneController,
                      onChanged: (val){
                        check(context);
                      },
                      /*maxLength: 10,*/
                     /* validator: (value) {
                        if (value == null || value.isEmpty) {
                          return 'Please enter some text';
                        }
                        return null;
                      },*/
                      keyboardType: TextInputType.phone,
                      /* keyboardType: TextInputType.emailAddress,*/
                      decoration: InputDecoration(
                        contentPadding: EdgeInsets.all(8),
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(8.0),
                          borderSide: BorderSide.none,
                        ),
                        filled: true,

                        fillColor: Color(0xFFE1E8F7),
                        hintText: "071 *** ****",

                      ),
                    ),

                    SizedBox(
                      height: 20.0,
                    ),




                    Container(    alignment: Alignment.bottomCenter,
                      padding: EdgeInsets.symmetric(horizontal: 0),
                      child: Row(
                        children: [
                          Expanded(
                            child: MaterialButton(
                              height: 44,
                              onPressed: () {
                                FocusScope.of(context).requestFocus(FocusNode());
                             /*   Navigator.push(context, SlidePageRoute(page:PasswordScreen()));*/
                              },
                              shape: RoundedRectangleBorder(
                                  borderRadius:
                                  BorderRadius.circular(8)),
                              color:   _isButtonEnabled ? Color(0xFF2A3476) : Color(0x201E1E99),
                              elevation: 0,
                              highlightElevation: 0,
                              child: Container(
                                child: Text(
                                  "Next",
                                    style: TextStyle(color: m_fillColor,fontSize: 18,fontWeight: FontWeight.w600  ,
                                      fontFamily: "regular",),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),

                    ),
                   /* Container(
                      child: Container(

                        width: MediaQuery.of(context).size.width,
                        padding: EdgeInsets.symmetric(vertical: 15),
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.all(Radius.circular(5)),
                            boxShadow: <BoxShadow>[
                              BoxShadow(
                                  color: Colors.grey.shade200,
                                  offset: Offset(2, 4),
                                  blurRadius: 5,
                                  spreadRadius: 2)
                            ],
                            gradient: LinearGradient(
                                begin: Alignment.centerLeft,
                                end: Alignment.centerRight,
                                colors: [
                                  Color(0xFF2A3476),
                                  Color(0xFF2A3476)
                                ])),
                        child: Text(
                          'Next',
                          style: TextStyle(fontSize: 20, color: Colors.white),

                        ),

                      ),

                    ),*/

                    SizedBox(
                      height: 4.0,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        RichText(
                          textAlign: TextAlign.center,
                          text: TextSpan(children: <TextSpan>[
                            TextSpan(
                                text: "Already have an account?",
                                style: TextStyle(color: m_titleColor,fontWeight: FontWeight.normal, fontFamily: "regular")),
                            TextSpan(
                                text: " Sign in",
                                style: TextStyle(
                                    color:  Color(0xFF2A3476),
                                    fontWeight: FontWeight.w600,
                                    fontFamily: "medium")),
                          ]),
                        )
                      ],
                    ),  SizedBox(
                      height:200.0,
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    ),

    ),
    );
  }


}

【问题讨论】:

  • Row (children: [ Text('First Name'), Spacer(), Text('Last Name'),],) 和使用mainAxisAlignment一样:MainAxisAlignment.spaceBetween,但是没有间隔()。
  • @SimonSot 我添加了 ut 不起作用,看看我的图片 ibb.co/r2RSk6J
  • 我给你贴了一个代码。它在 cmets 中不方便..

标签: flutter flutter-layout


【解决方案1】:

您可以使用两个 Column 布局并用 Row 包裹:

child: Padding(
  padding: const EdgeInsets.all(24.0),
  child: Column(
    mainAxisAlignment: MainAxisAlignment.start,
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      Text(
        "Hello, sign up to",
        style: TextStyle(
          fontSize: 22,
          fontFamily: "regular",
          fontWeight: FontWeight.w300,
          color: Colors.black,
        ),
      ),
      Text(
        "continue",
        style: TextStyle(
          fontSize: 22,
          fontFamily: "regular",
          fontWeight: FontWeight.w300,
          color: Colors.black,
        ),
      ),
      SizedBox(
        height: 20.0,
      ),
      Row(
        children: [
          Expanded(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  'First Name',
                  style: TextStyle(
                    fontSize: 15,
                    fontFamily: "regular",
                  ),
                ),
                TextField(
                  controller: lNameController,
                  onChanged: (val) {
                    check(context);
                  },
                  /* keyboardType: TextInputType.emailAddress,*/
                  decoration: InputDecoration(
                    contentPadding: EdgeInsets.all(8),
                    border: OutlineInputBorder(
                      borderRadius:
                      BorderRadius.circular(8.0),
                      borderSide: BorderSide.none,
                    ),
                    filled: true,
                    fillColor: Color(0xFFE1E8F7),
                    hintText: "",
                    /*   prefixIcon: Icon(
                                  Icons.people_outline_rounded,
                                  color: Colors.grey[600],
                                )*/
                  ),
                ),
              ],
            ),
          ),
          SizedBox(
            width: 16,
          ),
          Expanded(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  'Last Name',
                  style: TextStyle(
                    fontSize: 15,
                    fontFamily: "regular",
                  ),
                ),
                TextField(
                  controller: lNameController,
                  onChanged: (val) {
                    check(context);
                  },
                  /* keyboardType: TextInputType.emailAddress,*/
                  decoration: InputDecoration(
                    contentPadding: EdgeInsets.all(8),
                    border: OutlineInputBorder(
                      borderRadius:
                      BorderRadius.circular(8.0),
                      borderSide: BorderSide.none,
                    ),
                    filled: true,
                    fillColor: Color(0xFFE1E8F7),
                    hintText: "",
                    /*   prefixIcon: Icon(
                              Icons.people_outline_rounded,
                              color: Colors.grey[600],
                          )*/
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
      SizedBox(
        height: 20.0,
      ),
      Text(
        'Nick Name',
        style: TextStyle(
          fontSize: 15,
          fontFamily: "regular",
        ),
      ),

结果:

【讨论】:

    【解决方案2】:

    我们试试

    Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Expanded(
                child: Text(
                  'First Name',
                  style: TextStyle(
                    fontSize: 15,
                    fontFamily: "regular",
                  ),
                )),
    
            Expanded(
                child: Text(
                  'Last Name',
                  style: TextStyle(
                    fontSize: 15,
                    fontFamily: "regular",
                  ),
                )),
            SizedBox(
              width: 0,
            ),
          ],
        )
    

    【讨论】:

    • 感谢您的解决方案和时间
    【解决方案3】:
    import 'package:flutter/material.dart';
     
    void main() => runApp(MyApp());
     
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Material App',
          home: Scaffold(
            body: Center(
              child: Row(children: [
                Expanded(child: Container(color: Colors.lightGreen, child: Text('TextA'),)),
                Expanded(child: Container(color: Colors.red, child: Text('TextB'),)),
              ],)
              ),
            
          ),
        );
      }
    }
    

    【讨论】:

    • 感谢您的解决方案和时间
    【解决方案4】:

    您可以使用 Row 小部件的mainAxisAlignment 属性

              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Text(
                    'First Name',
                    style: TextStyle(
                      fontSize: 15,
                      fontFamily: "regular",
                    ),
                  ),
                  Text(
                    'Last Name',
                    style: TextStyle(
                      fontSize: 15,
                      fontFamily: "regular",
                    ),
                  )
                ])
    

    【讨论】:

    • 我添加了这个,姓氏在右上角,我想对齐右侧,就像一个文本框,看我的图片ibb.co/r2RSk6J
    • 如果你想实现文本字段,那么请根据你的要求选择文本字段并给出大小。
    猜你喜欢
    • 2020-01-21
    • 1970-01-01
    • 2019-10-09
    • 2021-10-20
    • 2021-10-21
    • 1970-01-01
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多