How to Set same height for all the textfield of a row of an item in list view builder. and the height should be the maximum amongst all textfield

huangapple go评论57阅读模式
英文:

How to Set same height for all the textfield of a row of an item in list view builder. and the height should be the maximum amongst all textfield

问题

我正在构建一个记录大量数据列表的应用程序,并且我正在使用文本字段进行操作。文本字段的高度应该是动态的,所以我将maxLine设置为null。我还想要另一个功能,即当一个文本字段的高度随着添加数据而增长时,我希望将相同的高度设置为同一行中的其余文本字段。我尝试使用Intrinsic Height,但在输入时无法使文本字段的高度增长。

以下是我编写的所有代码:

import 'package:flutter/material.dart';

class ListTest extends StatefulWidget {
  const ListTest({Key? key}) : super(key: key);

  @override
  State<ListTest> createState() => _ListTestState();
}

class _ListTestState extends State<ListTest> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 30),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text("Welcome"),
            Expanded(
              child: ListView.builder(
                padding: EdgeInsets.zero,
                itemCount: 10,
                itemBuilder: (c, i) {
                  return IntrinsicHeight(
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 30,
                        ),
                        Expanded(
                          child: TextField(
                            maxLines: null,
                            decoration: decoration,
                          ),
                        ),
                        Expanded(
                          child: TextField(
                            maxLines: null,
                            decoration: decoration,
                          ),
                        ),
                        Expanded(
                          child: TextField(
                            maxLines: null,
                            decoration: decoration,
                          ),
                        ),
                        Expanded(
                          child: TextField(
                            maxLines: null,
                            decoration: decoration,
                          ),
                        ),
                        Expanded(
                          child: TextField(
                            maxLines: null,
                            decoration: decoration,
                          ),
                        ),
                      ],
                    ),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  final decoration = InputDecoration(
    border: border,
    focusedBorder: border,
    enabledBorder: border,
  );
  static final border = OutlineInputBorder(
    borderRadius: BorderRadius.circular(10),
    borderSide: const BorderSide(
      color: Colors.grey,
    )
  );
}

希望这有助于您的应用程序开发!

英文:

I was building an application which records list of large data. and i am using textfields for the same. the height of the textfield should be dynamic, so i set maxLine to null. and i want one more feature is that when a textfield height grows as adding data to it,i want to set the same height for the rest of the textfields in the row. i tried with Intrinsic height , which fails to grow textfield height as typing into it.

im adding all the code im written below.

import &#39;package:flutter/material.dart&#39;;

class ListTest extends StatefulWidget {
  const ListTest({Key? key}) : super(key: key);

  @override
  State&lt;ListTest&gt; createState() =&gt; _ListTestState();
}

class _ListTestState extends State&lt;ListTest&gt; {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 30),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text(&quot;Welcome&quot;),
            Expanded(
              child: ListView.builder(
                padding: EdgeInsets.zero,
                itemCount: 10,
                itemBuilder: (c, i) {
                  return IntrinsicHeight(
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 30,
                        ),
                        Expanded(
                          child: TextField(
                            maxLines: null,
                            decoration: decoration,
                          ),
                        ),
                        Expanded(
                          child: TextField(
                            maxLines: null,
                            decoration: decoration,
                          ),
                        ),
                        Expanded(
                          child: TextField(
                            maxLines: null,
                            decoration: decoration,
                          ),
                        ),
                        Expanded(
                          child: TextField(
                            maxLines: null,
                            decoration: decoration,
                          ),
                        ),
                        Expanded(
                          child: TextField(
                            maxLines: null,
                            decoration: decoration,
                          ),
                        ),
                      ],
                    ),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  final decoration = InputDecoration(
    border: border,
    focusedBorder: border,
    enabledBorder: border,
  );
  static final border = OutlineInputBorder(
    borderRadius: BorderRadius.circular(10),
    borderSide: const BorderSide(
      color: Colors.grey,
    )
  );
}


答案1

得分: 2

以下是您要翻译的部分:

"如果我在一个文本字段中输入文本并且它扩展,那么该行中的所有文本字段都应该扩展。为此,一个简单的解决方案是在代码中的每个文本字段部件上添加expands:true,如下所示:"

英文:

You want that if i write text in one textfield and if it expands, then all the textfields in that row should expand.
For that, one simple solution is to add
expands:true
to every textfield widget in the code as shown below.

import &#39;package:flutter/material.dart&#39;;

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: &#39;Flutter Demo&#39;,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const ListTest(),
    );
  }
}



class ListTest extends StatefulWidget {
  const ListTest({Key? key}) : super(key: key);

  @override
  State&lt;ListTest&gt; createState() =&gt; _ListTestState();
}

class _ListTestState extends State&lt;ListTest&gt; {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 30),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text(&quot;Welcome&quot;),
            Expanded(
              child: ListView.builder(
                padding: EdgeInsets.zero,
                itemCount: 10,
                itemBuilder: (c, i) {
                  return IntrinsicHeight(
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 30,
                        ),
                        Expanded(
                          child: TextField(
                            maxLines: null,
                            expands:true,
                            decoration: decoration,
                          ),
                        ),
                        Expanded(
                          child: TextField(
                            maxLines: null,
                            expands:true,
                            decoration: decoration,
                          ),
                        ),
                        Expanded(
                          child: TextField(
                            maxLines: null,
                            expands:true,
                            decoration: decoration,
                          ),
                        ),
                        Expanded(
                          child: TextField(
                            maxLines: null,
                            expands:true,
                            decoration: decoration,
                          ),
                        ),
                        Expanded(
                          child: TextField(
                            maxLines: null,
                            expands:true,
                            decoration: decoration,
                          ),
                        ),
                      ],
                    ),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  final decoration = InputDecoration(
    border: border,
    focusedBorder: border,
    enabledBorder: border,
  );
  static final border = OutlineInputBorder(
      borderRadius: BorderRadius.circular(10),
      borderSide: const BorderSide(
        color: Colors.grey,
      ));
}

Solution Image

huangapple
  • 本文由 发表于 2023年5月7日 18:50:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定