如何使我的文本字段与发送按钮大小相同

Mik*_*ink 5 dart flutter flutter-textformfield

我有以下文本字段

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)),
        )
      ],
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

看起来像

在此输入图像描述

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

Jah*_*lam 0

您几乎就在那里,只需使用扩展的文本字段和按钮,然后用容器包裹按钮并设置高度即可。

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)),
            ),
          ),
        )
      ],
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

更新的答案:

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)),
        )
      ],
    )
Run Code Online (Sandbox Code Playgroud)