如何在Flutter中围绕FlatButton复制OutlineInputBorder

时间:2019-05-02 21:18:30

标签: dart flutter

我有几个字段从用户那里获取信息。其中一些是TextFormField,其他是按钮(FlatButton和PopupMenuButton)。我想复制在TextFormFields周围显示的OutlineInputBorder提示样式,以在我的按钮字段周围显示。我已经很接近了:

empty fields

以及字段中的信息

filled fields

如何使“选择生日”的帮助文本像“名字”一样进入边框?这是相关代码:

Padding(
                padding: EdgeInsets.all(paddingStandard),
                child: TextFormField(
                  textCapitalization: TextCapitalization.words,
                  decoration: InputDecoration(
                    contentPadding: EdgeInsets.all(0),
                    prefixIcon: Icon(Icons.person, color: colorMuted),
                    focusedBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: colorPrimary)),
                    border: OutlineInputBorder(
                        borderSide: BorderSide(color: colorMuted)),
                    labelText: "First Name",
                    labelStyle: textMuted,
                  ),
                  controller: nameController,
                  autofocus: false,
                ),
              ),
              Padding(
                padding: EdgeInsets.all(paddingStandard),
                child: Container(
                    decoration: BoxDecoration(
                      border: Border.all(color: colorMuted),
                      borderRadius: BorderRadius.circular(5),
                    ),
                    child: FlatButton(
                        padding: EdgeInsets.all(paddingStandard),
                        onPressed: () async {
                          var selectedDate = await _getBirthday();
                          selectedDate ??= birthday;
                          setState(() {
                            birthday = selectedDate;
                          });
                        },
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Row(
                                children: <Widget>[
                                  Padding(
                                    padding: EdgeInsets.only(
                                        right: paddingStandard * 2),
                                    child: Icon(Icons.cake, color: colorMuted),
                                  ),
                                  RichText(
                                      textAlign: TextAlign.left,
                                      text: TextSpan(
                                          text: birthday == null
                                              ? "Select Birthday"
                                              : DateFormat('dd-MMM-yyyy')
                                              .format(birthday),
                                          style: birthday == null
                                              ? textMuted
                                              : textDark))
                                ]),
                            Icon(
                              Icons.keyboard_arrow_down,
                              color: textColorMuted,
                            )
                          ],
                        ))),
              )

1 个答案:

答案 0 :(得分:2)

您可以将生日按钮包装成一堆,如果Birthday!= null,则在边框顶部显示一些文本。

这是一个代码演示,(用生日按钮容器替换它):

            Stack(
              children: <Widget>[
                Container(
                    decoration: BoxDecoration(
                      border: Border.all(color: colorMuted),
                      borderRadius: BorderRadius.circular(5),
                    ),
                    child: FlatButton(
                        padding: EdgeInsets.all(paddingStandard),
                        onPressed: () async {
                          var selectedDate = await _getBirthday();
                          selectedDate ??= birthday;
                          setState(() {
                            birthday = selectedDate;
                          });
                        },
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Row(children: <Widget>[
                              Padding(
                                padding:
                                    EdgeInsets.only(right: paddingStandard * 2),
                                child: Icon(Icons.cake, color: colorMuted),
                              ),
                              RichText(
                                  textAlign: TextAlign.left,
                                  text: TextSpan(
                                      text: birthday == null
                                          ? "Select Birthday"
                                          : DateFormat('dd-MMM-yyyy')
                                              .format(birthday),
                                      style: birthday == null
                                          ? textMuted
                                          : textDark))
                            ]),
                            Icon(
                              Icons.keyboard_arrow_down,
                              color: textColorMuted,
                            )
                          ],
                        ))),
                Align(
                  alignment: Alignment.topLeft,
                  child: Transform.translate(
                    offset: Offset(50, -12),
                    child: birthday != null
                        ? Container(
                            padding: EdgeInsets.symmetric(horizontal: 3),
                            color: Theme.of(context).canvasColor,
                            child: Text("birthday"),
                          )
                        : Container(),
                  ),
                ),
              ],
            ),