如何为小部件 Flutter 添加垂直间距

Posted

技术标签:

【中文标题】如何为小部件 Flutter 添加垂直间距【英文标题】:How to add vertical spacing to widgets Flutter 【发布时间】:2021-08-08 03:18:39 【问题描述】:

我这里有一段代码,它构成了登录和注册屏幕的主体。一切都在我想要的位置,但我想把它们隔开一点。两个输入字段,标题、副标题、按钮和文本按钮在垂直方向上靠得太近。 我尝试使用 mainaxisalignment.spacebetween,但出现约束错误,之后屏幕上的所有内容都消失了,但背景除外。

注册和登录屏幕使用的正文:

Widget build(BuildContext context) 
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topLeft,
            end: Alignment.bottomLeft,
            colors: [Color(0xFF1F1F1F), Color(0xFF1F1F1F)],
          ),
          image: DecorationImage(
            image: AssetImage("lib/assets/images/register.png"),
            alignment: Alignment.topCenter,
          ),
        ),
        child: Stack(
          children: [
            Container(
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage('lib/assets/images/bottomleft.png'),
                  alignment: Alignment.bottomLeft,
                ),
              ),
            ),
            Container(
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage('lib/assets/images/toptop.png'),
                  alignment: Alignment.topRight,
                ),
              ),
            ),
            Container(
              decoration: BoxDecoration(
                image: DecorationImage(
                    image: AssetImage('lib/assets/images/topbig.png'),
                    alignment: Alignment.topCenter,
                    repeat: ImageRepeat.repeat),
              ),
            ),
            Container(
              decoration: BoxDecoration(
                image: DecorationImage(
                    image: AssetImage('lib/assets/images/top.png'),
                    alignment: Alignment.topCenter,
                    repeat: ImageRepeat.repeatX),
              ),
            ),
            Padding(
              padding:
                  const EdgeInsets.symmetric(horizontal: 25, vertical: 250),
              child: ListView(
                children: [
                  if (onBackPressed == null) verticalSpaceLarge,
                  if (onBackPressed != null) verticalSpaceRegular,
                  if (onBackPressed != null)
                    IconButton(
                      padding: EdgeInsets.zero,
                      onPressed: onBackPressed,
                      alignment: Alignment.centerLeft,
                      icon: Icon(
                        Icons.arrow_back_ios,
                        color: Colors.black,
                      ),
                    ),
                  Center(
                    child: Text(
                      title!,
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 30.0,
                        fontWeight: FontWeight.bold,
                        fontFamily: 'Montserrat',
                      ),
                    ),
                  ),
                  Align(
                    alignment: Alignment.center,
                    child: SizedBox(
                      child: Center(
                        child: Text(
                          subtitle!,
                          style: ktsMediumGreyBodyText,
                        ),
                      ),
                    ),
                  ),
                  form!,
                  if (onForgotPassword != null)
                    Align(
                      alignment: Alignment.centerRight,
                      child: GestureDetector(
                        onTap: onForgotPassword,
                        child: Center(
                          child: Text(
                            'Forgot Password?',
                            style: ktsMediumGreyBodyText.copyWith(),
                          ),
                        ),
                      ),
                    ),
                  if (validationMessage != null)
                    Center(
                      child: Text(
                        validationMessage!,
                        style: TextStyle(
                            color: Colors.red, fontSize: kBodyTextSize),
                      ),
                    ),
                  if (validationMessage != null) verticalSpaceRegular,
                  GestureDetector(
                    onTap: onMainButtonTapped,
                    child: Container(
                      width: double.infinity,
                      height: 50,
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        color: kcPrimaryColor,
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: busy!
                          ? CircularProgressIndicator(
                              valueColor: AlwaysStoppedAnimation(Colors.white),
                            )
                          : Text(
                              mainButtonTitle!,
                              style: TextStyle(
                                  color: Colors.white,
                                  fontWeight: FontWeight.bold,
                                  fontSize: 14),
                            ),
                    ),
                  ),
                  if (onCreateAccountTapped != null)
                    GestureDetector(
                      onTap: onCreateAccountTapped,
                      child: Center(
                        child: Column(
                          children: [
                            Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Text(
                                  "Don\'t have an account?",
                                  style: TextStyle(color: Colors.white),
                                ),
                                Text(
                                  "SIGN UP",
                                  style: TextStyle(color: kcPrimaryColor),
                                ),
                              ],
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Text(
                                  "OR SIGN IN WITH",
                                  style: TextStyle(color: kcMediumGreyColor),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ),
                  if (showTermsText!)
                    Center(
                      child: Text(
                        "By signing up you agree to our terms, conditions and privacy policy",
                        style: ktsMediumGreyBodyText,
                        textAlign: TextAlign.center,
                      ),
                    ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  


登录屏幕:

Widget build(BuildContext context) 
    return ViewModelBuilder<LoginScreenModel>.reactive(
      builder: (context, model, child) => Scaffold(
        body: AuthenticationLayout(
          busy: model.isBusy,
          onCreateAccountTapped: () ,
          //validationMessage: model.validationMessage,
          title: 'SIGN IN',
          subtitle: 'please fill the form below',
          mainButtonTitle: 'SIGN IN',
          form: Column(
            children: [
              TextField(
                decoration: InputDecoration(
                  hintText: 'Username',
                  hintStyle: TextStyle(color: Colors.white),
                  prefixIcon: const Icon(
                    Icons.person,
                    color: Colors.white,
                  ),
                ),
              ),
              TextField(
                decoration: InputDecoration(
                  hintText: 'Password',
                  hintStyle: TextStyle(color: Colors.white),
                  prefixIcon: const Icon(
                    Icons.lock,
                    color: Colors.white,
                  ),
                ),
              ),
            ],
          ),
          onForgotPassword: () ,
        ),
      ),
      viewModelBuilder: () => LoginScreenModel(),
    );
  


【问题讨论】:

【参考方案1】:
     Column(
      children: <Widget>[
      FirstWidget(),
      SizedBox(height: 100),
       SecondWidget(),
      ],
     ),

【讨论】:

【参考方案2】:

您应该在两个小部件之间添加 SizedBox(),或为您的小部件设置填充,或者您也使用容器,如下所示:

使用 SizedBox()

Column(
  children:[
    Widget 1(),
    SizedBox(height:10),
    Widget 2(),
    SizedBox(height:10),
    Widget 3(),
  ],
),

使用 Padding()

Column(
children:[
   Padding(
      padding:EdgeInsets.all(8.0)
      child: Widget 1(),
   ),
   Padding(
      padding:EdgeInsets.all(8.0)
      child: Widget 2(),
   ),
   Padding(
      padding:EdgeInsets.all(8.0)
      child: Widget 3(),
   ),
 ],
),

使用 Container()

Column(
children:[
   Container(
      padding:EdgeInsets.all(8.0)
      child: Widget 1(),
   ),
   Container(
      padding:EdgeInsets.all(8.0)
      child: Widget 2(),
   ),
   Container(
      padding:EdgeInsets.all(8.0)
      child: Widget 3(),
   ),
 ],
),

您应该在您的选择上使用填充,例如:

    EdgeInsets.all(8.0),
    EdgeInsets.only(left:8.0,top:8.0,right:8.0,bottom:8.0),
    EdgeInsets.symmetric(vertical: 8.0,horizontal:8.0),

【讨论】:

【参考方案3】:

您可以在组件之间添加一些 SizedBox() 或设置边距。

【讨论】:

【参考方案4】:

使用 SizedBox 小部件为小部件添加垂直间距。 例如

SizedBox(height: 16),

【讨论】:

以上是关于如何为小部件 Flutter 添加垂直间距的主要内容,如果未能解决你的问题,请参考以下文章

Flutter:如何为 Draggable 小部件设置边界?

如何为我的小部件添加边距?了解 EdgeInsets 的效果

如何为可扩展的 Flutter 小部件设置动画以将其滑出屏幕

Flutter 应用程序中我的文本小部件内部的额外间距

如何为小部件边框/阴影添加霓虹发光效果?

如何为主窗口创建的小部件创建 QCloseEvent