简体   繁体   English

Flutter,如何创建嵌入文本的边框?

[英]Flutter, how to create border with text embedded?

Does anyone know how to create a border with text at the top like the following where is says "Create an account?有谁知道如何在顶部创建一个带有文本的边框,如下所示,“创建一个帐户?

流感

        Stack(
          children: <Widget>[
            Container(
              width: double.infinity,
              height: 200,
              margin: EdgeInsets.fromLTRB(20, 20, 20, 10),
              padding: EdgeInsets.only(bottom: 10),
              decoration: BoxDecoration(
                border: Border.all(
                    color: Color.fromARGB(255, 51, 204, 255), width: 1),
                borderRadius: BorderRadius.circular(5),
                shape: BoxShape.rectangle,
              ),
            ),
            Positioned(
                left: 50,
                top: 12,
                child: Container(
                  padding: EdgeInsets.only(bottom: 10, left: 10, right: 10),
                  color: Colors.white,
                  child: Text(
                    'Create an account',
                    style: TextStyle(color: Colors.black, fontSize: 12),
                  ),
                )),
          ],
        )

在此处输入图片说明

If you are using the material package, there is already a built-in solution.如果您正在使用材料包,则已经有一个内置的解决方案。 For example if you are designing a form and want one of the text fields to have the style you mentioned, you can do something like this for the text field:例如,如果您正在设计一个表单并希望其中一个文本字段具有您提到的样式,您可以对文本字段执行以下操作:

TextFormField(
  style: TextStyle(fontSize: 20),
  decoration: InputDecoration(
    hintText: "Last Name",
    hintStyle: TextStyle(fontSize: 14),
    border: OutlineInputBorder(), // <-- This is the key
    labelText: "Last Name",
  ),
  validator: _FormValidators.validateName,
),

Emulator screenshot:模拟器截图:

在此处输入图片说明

Sources:资料来源:

https://flutter-examples.com/create-text-input-textfield-widget-in-flutter/ https://flutter-examples.com/create-text-input-textfield-widget-in-flutter/

I don't think there's an Out-of-the-box widget to do so, but you can use a workaround, I have two suggestions here, but you may need to take care of the fixed measurements:我不认为有一个开箱即用的小部件可以这样做,但您可以使用一种解决方法,我在这里有两个建议,但您可能需要注意固定测量:

在此处输入图片说明

1- To use Stack/Positioned. 1- 使用堆叠/定位。 here's the code:这是代码:

import 'package:flutter/material.dart';

class Demo extends StatelessWidget {
  // To be sure that Scaffold Background & text background always the same.
  Color backgroundColor = Colors.white;

  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        backgroundColor: backgroundColor,
        body: Stack(
          children: <Widget>[
            Container(
              height: 300,
              margin: EdgeInsets.all(20.0),
              decoration:
                  BoxDecoration(border: Border.all(color: Colors.black26)),
            ),
            Positioned(
              top: 5.0,
              left: 30.0,
              right: 0.0,
              child: Row(
                children: <Widget>[
                  Flexible(
                    child: Container(
                      padding: EdgeInsets.symmetric(
                        horizontal: 8.0,
                      ),
                      decoration: new BoxDecoration(
                        color: backgroundColor,
                      ),
                      child: Text(
                        'مرحباً',
                        style: TextStyle(
                          color: Colors.black45,
                          fontSize: 18.0,
                          fontWeight: FontWeight.w500
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}


2- Another way is to set the Title as the first element in the Content(Column or what ever) then use ~negative padding to pull it up to cross the line using: 2- 另一种方法是将标题设置为内容(列或其他任何内容)中的第一个元素,然后使用 ~negative padding 将其拉起来以越线使用:

// Negative padding
transform: Matrix4.translationValues(5.0, -14.0, 0.0),

here's an example:这是一个例子:

import 'package:flutter/material.dart';

class Demo extends StatelessWidget {
  Color backgroundColor = Colors.white;

  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        backgroundColor: backgroundColor,
        body: Stack(
          children: <Widget>[
            Container(
              height: 300,
              width: 400,
              margin: EdgeInsets.all(20.0),
              decoration:
                BoxDecoration(border: Border.all(color: Colors.black26),),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Container(
                    // Negative padding
                    transform: Matrix4.translationValues(5.0, -14.0, 0.0),
                    padding: EdgeInsets.symmetric(
                      horizontal: 8.0,
                    ),
                    decoration: new BoxDecoration(
                      color: backgroundColor,
                    ),
                    child: Text(
                      'مرحباً',
                      style: TextStyle(
                          color: Colors.black45,
                          fontSize: 18.0,
                          fontWeight: FontWeight.w500
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

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

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