英文:
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 '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,
)
);
}
答案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 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const ListTest(),
);
}
}
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,
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,
));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论