繁体   English   中英

TextFormField 中 LabelText 和 HintText 之间的更多空间(Flutter)

More space between LabelText and HintText in TextFormField (Flutter)

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

我想增加 TextFormField 中 labelText 和 hintText 之间的距离,而 contentPadding: EdgeInsets.fromLTRB(x, x, x, x), 对我一点帮助都没有,它确实应用了填充,但这些元素仍然在一起。

我的预览: 在此处输入图像描述

4 个回复

您可以使用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.
    );
  }
}

在此处输入图像描述

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

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

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

我只需要一个带有 label 且没有提示的 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',
  ),
),
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),
          ),
2 Flutter:在TextFormField中同时显示helpText和labelText

我正在尝试使用Flutter构建登录页面。 我想同时显示我正在使用的TextFormField的hintText和labelText,但是当我单击该字段时(即,当光标位于该字段中时),仅向我显示labelText。 TextFormField( keyboardType: T ...

2019-09-11 16:14:25 2 32   flutter
4 如何限制 TextFormField 中 labelText 的文本

我是编程新手,与我帮助解决较小问题的团队合作。我面临的问题是我的 label 文本正在遍历文本表单字段末尾的平面按钮的文本。 这是有问题的问题的屏幕截图我试图放置 TextOverflow.ellipsis,但它仅适用于 Text 小部件。 我尝试过的另一件事是创建一种方法来为每个单词添加换行符,但 ...

2020-04-27 16:49:49 0 26   flutter
7 TextFormField 中的 Hinttext 未与前缀和后缀 Icon 对齐

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

10 Flutter 中的纯数字 TextFormField

我正在开发一个需要以¥输入价格的应用程序,因此没有小数位。 如果我们使用keyboardType: TextInputType.numberWithOptions()我们可以获得数字键盘输入。 如果我们使用validator: (input) { }我们可以检查输入是否有效,但我们无法阻止它。 问 ...

暂无
暂无

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

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