gpt4 book ai didi

Flutter 展开/占用 TextFormField 以填充屏幕的其余部分

转载 作者:IT王子 更新时间:2023-10-29 06:54:32 31 4
gpt4 key购买 nike

我有一个带有一些 TextFormField 的表单,我想扩展最后一个 TextFormField 以占据屏幕的其余部分。最后一个 TextFormField 可以有多行文本。

我没能做到这一点,并尝试了 SizedBox.expand()Expanded 小部件,但没有成功。

enter image description here

下面是当前代码:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"What is this new classroom?"
),
SizedBox(height: 8.0,),
Expanded(
child: Form(
key: _formKey,
child: ListView(
padding: EdgeInsets.all(8.0),
children: <Widget>[
Container(
padding: EdgeInsets.symmetric(vertical: 8.0),
child: TextFormField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: "Classroom Name",
hintText: "What's name of the new classroom?",
),
)
),
SizedBox(height: 8.0,),
Container(
padding: EdgeInsets.symmetric(vertical: 8.0),
child: TextFormField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: "Description",
hintText: "Description of the new classroom",
),
//maxLines: 5,
),
),
]
),
),
),
],
),
),
);
}
}

最佳答案

我稍微编辑了你的代码。但是没用。请引用以下代码。我将尝试在代码下方解释我的理解

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("What is this new classroom?"),
SizedBox(
height: 8.0,
),
Expanded(
child: Form(
key: _formKey,
child: Column(children: <Widget>[
Container(
padding: EdgeInsets.symmetric(vertical: 8.0),
child: TextFormField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: "Classroom Name",
hintText: "What's name of the new classroom?",
),
)),
SizedBox(
height: 8.0,
),
Expanded(
child: Container(
padding: EdgeInsets.symmetric(vertical: 8.0),
child: TextFormField(
maxLines: null,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: "Description",
hintText: "Description of the new classroom",
),
),
),
),
]),
)),
],
),
),
);
}
}

我用你的代码检查了 View 。位于 TextFormField 内的 TextField 没有占据屏幕的其余部分。所以我编辑了 TextField 来显示屏幕的其余部分。上面的代码就是这样做的。查看检查 View enter image description here

但是有 InputDecorator(它是我们的 TextField 的子级)绘制边界线。在我们的例子中,它根据内容绘制边界线。

可能的解决方法是:

  1. maxLines = null 这会将 TextField 作为内容组增长。但初始 View 将是一行。
  2. 给出固定的 maxLines(如 10 或 20),这看起来可能会占据屏幕。但它不是动态的(不会根据屏幕尺寸/屏幕方向而改变)

关于Flutter 展开/占用 TextFormField 以填充屏幕的其余部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53027772/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com