【问题标题】:How can I make my textfield fill the available vertical space?如何让我的文本字段填充可用的垂直空间?
【发布时间】:2022-01-24 13:47:27
【问题描述】:

我有以下小部件

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 Scaffold(
      backgroundColor: Colors.green,
      body: Align(
        alignment: Alignment.bottomCenter,
        child: Container(
          height: 100,
          color: Colors.black,
          child: const ResponsiveInput(),
        ),
      ),
    );
  }
}

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

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

下面是哪个

[![在此处输入图片描述][1]][1]

但是如何让文本字段填充黑色容器中的剩余空间?

我尝试使用内容填充,但如果容器具有响应高度,则文本将被截断。即使使用稠密。

【问题讨论】:

    标签: flutter dart flutter-textformfield


    【解决方案1】:

    你可以在TextFormField上使用expands: true, maxLines: null,

    expands 如果设置为 true 并包装在 Expanded 或 SizedBox 等父窗口小部件中,则输入将展开以填充父窗口。

    Expanded(
      child: TextFormField(
        expands: true,
        maxLines: null,
        decoration: const InputDecoration(
          fillColor: Colors.white,
          filled: true,
        ),
      ),
    ),
    

    更多关于expands

    【讨论】:

    • 感谢您的回答,这不是我正在寻找的答案,但我的示例和问题是错误的。它确实回答了这个问题。但我知道扩展属性。我会提出一个新问题。
    猜你喜欢
    • 2020-05-27
    • 2017-06-23
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    • 2010-11-23
    • 2015-05-16
    • 1970-01-01
    相关资源
    最近更新 更多