我想增加 TextFormField 中 labelText 和hintText 之间的距离,而 contentPadding: E​​dgeInsets.fromLTRB(x, x, x, x) 根本没有帮助我,它确实应用了填充,但这些元素保持在一起。

我的预览: 在此处输入图片说明

#1楼 票数:6

您可以使用hintStyle 和LabelStyle 执行此操作,将高度属性设置为您想要的
你可以在图片中看到我的测试结果
代码片段

TextFormField(
          decoration: const InputDecoration(
            icon: Icon(Icons.person),
            hintText: 'What do people call you?',
            hintStyle: TextStyle(height:7, fontWeight: FontWeight.bold),
            labelText: 'Name *',
            labelStyle: TextStyle(height:5, fontWeight: FontWeight.bold),
          ),

完整代码

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Invoke "debug painting" (press "p" in the console, choose the
          // "Toggle Debug Paint" action from the Flutter Inspector in Android
          // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
          // to see the wireframe for each widget.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextFormField(
              decoration: const InputDecoration(
                icon: Icon(Icons.person),
                hintText: 'What do people call you?',
                hintStyle: TextStyle(height:7, fontWeight: FontWeight.bold),
                labelText: 'Name *',
                labelStyle: TextStyle(height:5, fontWeight: FontWeight.bold),
              ),
              onSaved: (String value) {
                // This optional block of code can be used to run
                // code when the user saves the form.
              },
              validator: (String value) {
                return value.contains('@') ? 'Do not use the @ char.' : null;
              },
            ),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

在此处输入图片说明

#2楼 票数:1

如果您使用的是OutlineInputBorder ,您可以使用textAlignVertical: TextAlignVertical.bottom,以及

    decoration: InputDecoration(
      contentPadding: EdgeInsets.only(top: 28),
      border: OutlineInputBorder(
      )
    )

将 contentPadding 调整到您想要的高度。

#3楼 票数:0

我需要一个只有标签而没有提示的 TextField。 我在两者之间创建空间的解决方法是将提示作为空字符串提供。 希望这有助于我花了 20 分钟才弄清楚🙄

TextField(
  decoration: InputDecoration(
    hintStyle: TextStyle(
      height: 3.0, // sets the distance between label and input
    ),
    hintText: '', // needed to create space between label and input
    labelStyle: TextStyle(
      color: kWhiteTextColor,
      fontSize: 20.0,
    ),
    labelText: 'My first name is',
  ),
),

#4楼 票数:0

TextFormField(
            controller: controller.fullNameController,
            focusNode: controller.fullNameFocusNode,
            enabled: false,
            decoration: InputDecoration(
                alignLabelWithHint: true,
                labelText: fullNameLabel,
                hintText: fullNameLabel,
                labelStyle: TextStyle(
                  fontSize: 18,
                  height: 0.5, // Tweak this 1,2 moves label bottom-> -1,-2 moves label upwards
                  fontWeight: FontWeight.w500,
                ),
                floatingLabelBehavior: FloatingLabelBehavior.always),
          ),

  ask by marse translate from so

未解决问题?本站智能推荐:

1回复

Flutter,是否可以在TextFormField'hinttext'属性中添加图标?

是否可以在 TextFormField 'hinttext' 属性中添加图标? 我知道这如下所示 但我目前想要的是提示图标因此,如果用户单击提示,则“hintText”和“hintIcon”将消失有没有办法实现这一目标? 谢谢
1回复

TextFormField中的Hinttext未与前缀和后缀Icon对齐

与前缀和后缀 Icon 相比,textformfield 中的提示文本如何稍微升高(感觉好像与两个图标相比是向上的)。 我还附上了用于此 TextFormfield 的代码: 我也试过用中心小部件包装 textformfield,但它没有解决问题。 如果有人可以提供帮助,我将不胜感激
2回复

如何减少Flutter中TextformField中标签文本和底线之间的填充?

这就是我所拥有的: 这就是我要的: 我想删除标签文本和底线之间的空间,该怎么做?
2回复

Flutter:带有hintText的CupertinoTextField

我想在不导入材质主题的情况下将提示文本添加到CupertinoTextField 。 到目前为止,我尝试将CupertinoTextField与InputDecoration混合, InputDecoration所示: 导致以下错误: The argument type 'InputDe
1回复

为什么If语句在Flutter中处理TextFormField?

在我的应用程序中,我需要预览一个表格,如果用户输入了一些数据,该表格就会出现。 所以我添加了一个 if 语句。 还有一个按钮可以清除表单。 我的 TextFormField 位于表格下方。 在我清除表格之前它不会被处理。 但是在我清除表格后,它会被处理掉。 我在表格下方添加了另一个 TextForm
1回复

如何在不影响边框的情况下垂直居中对齐TextFormField中的Text和labelText?

如果您注意到下图,标签和文本都与顶部对齐。 任何人都可以帮助完成对齐以将这两个垂直居中吗? 此外,如果您注意到“电子邮件”正上方的边框被切断。 有人可以帮助使用代码片段来避免这种情况吗? 还有什么办法,我们可以根据“TextFormField”的内容(值)进行格式化吗? 以下是产生上述图像的
2回复

Flutter输入装饰中为labelText添加强制(*)

我想在InputDecoration labelText 中添加星号并更改它的颜色(如红色),以便用户轻松理解该字段是必需的。TextField( autofocus: true, controller: _nameCtrlr, keyboardType: TextInputTyp
1回复

在Flutter中创建带有圆角且没有ActivationIndicator的TextFormField

我正在尝试为看起来像这样的代码实现用户输入 目前,我所拥有的只是一个容纳一行的容器,该行包含4个容器,每个容器包含一个TextFormField。 我如何实现以上目标