Flutter TextField Label文本中心

rel*_*ope 13 center textfield dart flutter

期望的效果是以 Kartennummer 和 Passwort 为中心。
这怎么可能?

错误图像

我为此使用自定义类:

import 'package:flutter/material.dart';
import 'package:impex_shop/styles/impex_styles.dart';

class ImpexTextField extends StatefulWidget {
  final TextEditingController controller;
  final bool obscureText;
  final TextInputType keyboardType;
  final String labelText;
  final IconData prefixIcon;
  final int maxLines;
  final TextInputAction textInputAction;
  final void Function(String) onSubmitted;
  final bool autofocus;

  const ImpexTextField(
      {Key key,
      this.controller,
      this.obscureText,
      this.keyboardType,
      this.labelText,
      this.prefixIcon,
      this.maxLines = 1,
      this.textInputAction,
      this.onSubmitted,
      this.autofocus = false})
      : super(key: key);

  @override
  _ImpexTextFieldState createState() => _ImpexTextFieldState();
}

class _ImpexTextFieldState extends State<ImpexTextField> {
  FocusNode _focusNode = FocusNode();
  Paint paint;

  InputDecoration buildTextInputDecoration(
      String labelText, TextEditingController controller, IconData prefixIcon) {
    return InputDecoration(
      labelText: labelText,
      labelStyle: TextStyle(
        color: ImpexColors.mainColor,
        height: 0.8, // 0,1 - label will sit on top of border
        background: paint,
      ),
      fillColor: ImpexColors.lightGrey,
      filled: true,
      enabledBorder: OutlineInputBorder(
        borderSide: const BorderSide(
          color: ImpexColors.grey,
          width: 1.0,
        ),
      ),
      focusedBorder: OutlineInputBorder(
        borderSide: const BorderSide(
          color: ImpexColors.secondaryColor,
          width: 2.0,
        ),
      ),
      suffixIcon: InkWell(
        onTap: () => controller.clear(),
        child: Icon(Icons.cancel),
      ),
      prefixIcon: prefixIcon == null ? null : Icon(prefixIcon),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: ListView(
        shrinkWrap: true,
        physics: ClampingScrollPhysics(),
        children: <Widget>[
          Container(
            height: 12,
          ),
          TextField(
            textAlign: TextAlign.center,
            textAlignVertical: TextAlignVertical.center,
            focusNode: _focusNode,
            controller: widget.controller,
            obscureText: widget.obscureText ?? false,
            maxLines: widget.maxLines,
            textInputAction: widget.textInputAction,
            decoration: buildTextInputDecoration(
                widget.labelText, widget.controller, widget.prefixIcon),
            keyboardType: widget.keyboardType,
            autofocus: widget.autofocus,
            onSubmitted: widget.onSubmitted,
            onTap: () => setState(() {
              FocusScope.of(context).requestFocus(_focusNode);
            }),
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    _focusNode.dispose();
    super.dispose();
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 16

为此有一个很好的决定。
尝试使用alignLabelWithHint: true.
例如:

TextField(
  keyboardType: TextInputType.number, 
  textAlign: TextAlign.center, 
  maxLines: 1, 
  decoration: InputDecoration(
    alignLabelWithHint: true, 
    enabledBorder: InputBorder.none, 
    contentPadding: EdgeInsets.zero, 
    focusedBorder: InputBorder.none, 
    border: InputBorder.none, 
    labelStyle: Theme.of(context).textTheme.headline6, 
    labelText: 'Amount (GPB)'.toUpperCase(),
  ),
),
Run Code Online (Sandbox Code Playgroud)


Nac*_*hil 15

一个非常方便的标签居中解决方案:

因为在之后Label接受,所以你可以将你的小部件包装成 这样的小部件,Widgetflutter 2.5.xTextCenter

TextFormField(
  decoration: InputDecoration(
    label: const Center(
      child: Text("Your Centered Label Text"),
    ),
  ),
)
Run Code Online (Sandbox Code Playgroud)

笔记

  • 如果上边框因此不可见,则:

尝试将小Center部件包裹到Row,并给出mainAxisSize: MainAxisSize.min,这不会覆盖整个边框


voi*_*oid -1

您可以通过使用textAlign的 属性TextField并将其设置为 来实现TextALign.center

检查下面的代码:

TextField(
  // use the text align property
  textAlign: TextAlign.center,
  decoration: InputDecoration(
    labelText: 'Yay, it works',
    hintText: 'Center the text',
  ),
),
Run Code Online (Sandbox Code Playgroud)

输出:

在此输入图像描述

  • 我相信提出问题的人想要将标签居中而不是文本本身 (3认同)