【问题标题】:How can I make my textfield be the same size as the send button如何使我的文本字段与发送按钮的大小相同
【发布时间】:2022-01-24 14:47:23
【问题描述】:

我有以下文本字段

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Playground',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      backgroundColor: Colors.green,
      body: Align(
        alignment: Alignment.bottomCenter,
        child: ResponsiveInput(),
      ),
    );
  }
}

class ResponsiveInput extends StatelessWidget {
  const ResponsiveInput({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.end,
      children: [
        Expanded(
          child: TextFormField(
            maxLines: 8,
            minLines: 1,
            decoration: const InputDecoration(
              fillColor: Colors.white,
              filled: true,
            ),
          ),
        ),
        TextButton(
          onPressed: () => false,
          child: const Text('Send'),
          style: ButtonStyle(
              backgroundColor: MaterialStateProperty.all(Colors.orange)),
        )
      ],
    );
  }
}

看起来像

texfield 最多可以有 8 行文本和 1 最小行。但是当它为空时,我希望它与发送按钮的高度相同。但是现在文本按钮的上下似乎有某种边距。

【问题讨论】:

  • 你为什么不把它包在 SizedBox 里?
  • 因为文本字段在多行时会扩展

标签: flutter dart flutter-textformfield


【解决方案1】:

你快到了,只需使用扩展你的文本字段和按钮,并用容器包装你的按钮并设置高度。

class ResponsiveInput extends StatelessWidget {
  const ResponsiveInput({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.end,
      children: [
        Expanded(
          flex: 3,
          child: TextFormField(
            maxLines: 8,
            minLines: 1,
            decoration: const InputDecoration(
              fillColor: Colors.white,
              filled: true,
            ),
          ),
        ),
        Expanded(
          flex: 3,
          child: Container(
            height: 48,
            child: TextButton(
              onPressed: () => false,
              child: const Text('Send'),
              style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(Colors.orange)),
            ),
          ),
        )
      ],
    );
  }
}

更新答案:

Row(
      crossAxisAlignment: CrossAxisAlignment.end,
      children: [
        Expanded(
          child: TextFormField(
            maxLines: 8,
            minLines: 1,
            decoration: const InputDecoration(
              border: OutlineInputBorder(),
              contentPadding: EdgeInsets.all(8),
              labelText: 'Even Densed TextFiled',
              isDense: true,
              fillColor: Colors.white,
              filled: true,// Added this

            ),
          ),
        ),
        TextButton(
          onPressed: () => false,
          child: const Text('Send'),
          style: ButtonStyle(
              backgroundColor: MaterialStateProperty.all(Colors.orange)),
        )
      ],
    )

【讨论】:

  • 嘿,我想你误会了。按钮的宽度很好。这只是需要匹配的高度。 48的身高是怎么来的,这似乎是正确的身高。这适用于所有设备吗?
  • 请稍等一下,它适用于所有设备,因为文本字段默认高度为 48,请阅读本文groups.google.com/g/flutter-announce/c/RDyeXZK0fO8/m/…
  • 否则您可以在文本字段内进行内容填充,按照更新的代码部分进行操作
  • 啊啊我看到身高总是48,我不知道。谢谢!
  • 不客气,对于文本按钮高度,您可以按照我之前在 SO stackoverflow.com/questions/66900829/… 中的回答进行操作
猜你喜欢
  • 2011-02-12
  • 2015-01-30
  • 2022-08-06
  • 2021-05-15
  • 1970-01-01
  • 2017-09-21
  • 2016-09-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多