gpt4 book ai didi

gridview - Flutter 混合布局 - 带行的网格

转载 作者:IT王子 更新时间:2023-10-29 07:07:01 26 4
gpt4 key购买 nike

我正在尝试创建一个基本的 Flutter 应用程序,它有一个 2 x 2 的文本输入网格和一个位于其下方的按钮。

最初,我只有网格,没有按钮,没有任何问题:

void main() => runApp(new App());
class App extends StatelessWidget {
...
Widget build(BuildContext context) {
return new MaterialApp(
...
home: new InputWidget()
...
class InputWidget extends StatefulWidget {
Widget build(BuildContext context) {
...
_InputWidgetState createState() => new _InputWidgetState();
...
class _InputWidgetState extends State<InputWidget> {
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(...)
body: new Builder(
builder: (BuildContext context) {
return new GridView.count(
children: <Widget>[
// 4 Text Fields here
]

我需要脚手架内的 GridView 才能将脚手架用于 snackbar 。

现在我想在这个网格下方添加一个按钮。为了实现这一点,我在两者之间添加了几个层,并且我得到了一个“被无穷大溢出”的错误,其逻辑如下:

void main() => runApp(new App());
class App extends StatelessWidget {
...
Widget build(BuildContext context) {
return new MaterialApp(
...
home: new AppContainer()
...
class AppContainer extends StatelessWidget {
Widget build(BuildCOntext context) {
return new Material( // tried Container as well
child: new Column(
children: <Widget>[
new InputWidget()
new BUttonWidget()
...
class ButtonWidget extends StatelessWidget {
Widget build(BuildContext context) {
return new Container(
child: new MaterialButton(...)
...
class InputWidget extends StatefulWidget {
Widget build(BuildContext context) {
...
_InputWidgetState createState() => new _InputWidgetState();
...
class _InputWidgetState extends State<InputWidget> {
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(...)
body: new Builder(
builder: (BuildContext context) {
return new GridView.count(
children: <Widget>[
// 4 Text Fields here
]

Code & Stack Trace .

问题似乎出在“列”部分。似乎我需要为 Column 或 Scaffold 提供一些大小调整,但我似乎无法弄清楚我缺少哪些参数。

我在 SO 或 Flutter 文档上找不到任何我可以遵循的示例 - 或者我完全错过了它。

如果有任何指示可以让此布局排序,我们将不胜感激。

最佳答案

感谢@xqwzts 和@ahmadzein,得到了 Flutter Gitter 的帮助。

解决方案是将每个 child 包裹在“Expanded”中

void main() => runApp(new App());
class App extends StatelessWidget {
...
Widget build(BuildContext context) {
return new MaterialApp(
...
home: new AppContainer()
...
class AppContainer extends StatelessWidget {
Widget build(BuildCOntext context) {
return new Material( // tried Container as well
child: new Column(
children: <Widget>[
new Expanded (
child: new InputWidget() ... ),
new Expanded (
child: new Row (
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[new ButtonWidget()] ... )
...
class ButtonWidget extends StatelessWidget {
Widget build(BuildContext context) {
return new Container(
child: new MaterialButton(...)
...
class InputWidget extends StatefulWidget {
Widget build(BuildContext context) {
...
_InputWidgetState createState() => new _InputWidgetState();
...
class _InputWidgetState extends State<InputWidget> {
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(...)
body: new Builder(
builder: (BuildContext context) {
return new GridView.count(
children: <Widget>[
// 4 Text Fields here
]

关于gridview - Flutter 混合布局 - 带行的网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48481505/

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