Flutter、Overflow、SingleChildScrollView。这是屏幕内的一个表单,这次 SingleChildScollView Fix 对我有用吗?

Flutter, Overflow, SingleChildScrollView. It's a form inside a screen and SingleChildScollView Fix working for me this time?

我在学习编码的同时构建了一些 flutter 应用程序,并修复了 SingleChildScrollView 过去的“溢出”问题。这个问题不同,我无法在这里或其他地方找到解决方法。

我认为问题是由于 'form' 在 'screen' 中。

屏幕 'golf_cart_reg_new.dart' 有 appBar 和提交或取消按钮。

'golf_cart_reg_new_form.dart' 有 TextFormFields 和验证。溢出错误似乎出现在表单中而不是屏幕中。我在所有我能想到的地方都试过 SingleChildScrollView,没有任何改变。

我也确定我的代码很臃肿,但我还是边学边学。

我将 post 为下面的两个屏幕编写代码。任何帮助将不胜感激。

---- 这是下面的屏幕代码----

import 'package:WelakaOne/logic/golf_cart_new_form.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:WelakaOne/appbar/app_bar_title.dart';
import 'package:WelakaOne/drawer/drawer.dart';
import 'package:WelakaOne/drawer/end_drawer.dart';
import 'package:WelakaOne/logic/custom_colors.dart';

import 'golf_cart_reg_home.dart';

class GolfCartNewScreen extends StatefulWidget {
  @override
  _GolfCartNewScreenState createState() => _GolfCartNewScreenState();
}

class _GolfCartNewScreenState extends State<GolfCartNewScreen> {
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        systemOverlayStyle: SystemUiOverlayStyle.dark,
        backgroundColor: CustomColors.WelakaOneBlack,
        title: AppBarTitle(),
        leading: Builder(
          builder: (context) {
            return IconButton(
              onPressed: () {
                Scaffold.of(context).openDrawer();
              },
              icon: Icon(Icons.menu),
            );
          },
        ),
        actions: <Widget>[
          Builder(
            builder: (context) {
              return IconButton(
                onPressed: () {
                  Scaffold.of(context).openEndDrawer();
                },
                icon: Icon(Icons.person),
              );
            },
          ),
        ],
      ),
      drawer: new MyDrawer(),
      endDrawer: new MyEndDrawer(),
      body: Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        decoration: const BoxDecoration(
          gradient: LinearGradient(
            colors: [
              CustomColors.WelakaOneBlack,
              CustomColors.WelakaOneBlueDark,
            ],
            begin: FractionalOffset(0.0, 0.0),
            end: FractionalOffset(1.6, 1.0),
            stops: [0.3, 1.0],
            tileMode: TileMode.clamp,
          ),
        ),
        child: Container(
          child: Center(
            child: Column(
              children: <Widget>[
                Container(
                  padding: EdgeInsets.fromLTRB(0, 15, 0, 0),
                  child: Text(
                    'GOLF CART',
                    style: TextStyle(
                      color: CustomColors.WelakaOneYellow,
                      fontSize: 18,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                Container(
                  padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
                  child: Text(
                    'NEW REGISTRATION',
                    style: TextStyle(
                      color: CustomColors.WelakaOneYellow,
                      fontSize: 18,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                SizedBox(height: 30),
                Container(
                  padding: EdgeInsets.fromLTRB(30, 0, 30, 0),
                  child: GolfCartNewForm(),
                ),
                SizedBox(height: 30),
                Container(
                  child: Center(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        ElevatedButton(
                          onPressed: () {
                            Navigator.push(
                              context,
                              new MaterialPageRoute(
                                builder: (context) => new GolfCartHomeScreen(),
                              ),
                            );
                          },
                          child: const Text(
                            'CANCEL',
                            style: TextStyle(
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                              color: CustomColors.WelakaOneBlueDark,
                            ),
                          ),
                          style: ElevatedButton.styleFrom(
                            primary: CustomColors.WelakaOneWhite,
                            fixedSize: const Size(220, 40),
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(50),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

---- 这是下面的表格----

import 'package:WelakaOne/logic/golf_cart_new_form.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:WelakaOne/appbar/app_bar_title.dart';
import 'package:WelakaOne/drawer/drawer.dart';
import 'package:WelakaOne/drawer/end_drawer.dart';
import 'package:WelakaOne/logic/custom_colors.dart';

import 'golf_cart_reg_home.dart';

class GolfCartNewScreen extends StatefulWidget {
  @override
  _GolfCartNewScreenState createState() => _GolfCartNewScreenState();
}

class _GolfCartNewScreenState extends State<GolfCartNewScreen> {
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        systemOverlayStyle: SystemUiOverlayStyle.dark,
        backgroundColor: CustomColors.WelakaOneBlack,
        title: AppBarTitle(),
        leading: Builder(
          builder: (context) {
            return IconButton(
              onPressed: () {
                Scaffold.of(context).openDrawer();
              },
              icon: Icon(Icons.menu),
            );
          },
        ),
        actions: <Widget>[
          Builder(
            builder: (context) {
              return IconButton(
                onPressed: () {
                  Scaffold.of(context).openEndDrawer();
                },
                icon: Icon(Icons.person),
              );
            },
          ),
        ],
      ),
      drawer: new MyDrawer(),
      endDrawer: new MyEndDrawer(),
      body: Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        decoration: const BoxDecoration(
          gradient: LinearGradient(
            colors: [
              CustomColors.WelakaOneBlack,
              CustomColors.WelakaOneBlueDark,
            ],
            begin: FractionalOffset(0.0, 0.0),
            end: FractionalOffset(1.6, 1.0),
            stops: [0.3, 1.0],
            tileMode: TileMode.clamp,
          ),
        ),
        child: Container(
          child: Center(
            child: Column(
              children: <Widget>[
                Container(
                  padding: EdgeInsets.fromLTRB(0, 15, 0, 0),
                  child: Text(
                    'GOLF CART',
                    style: TextStyle(
                      color: CustomColors.WelakaOneYellow,
                      fontSize: 18,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                Container(
                  padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
                  child: Text(
                    'NEW REGISTRATION',
                    style: TextStyle(
                      color: CustomColors.WelakaOneYellow,
                      fontSize: 18,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                SizedBox(height: 30),
                Container(
                  padding: EdgeInsets.fromLTRB(30, 0, 30, 0),
                  child: GolfCartNewForm(),
                ),
                SizedBox(height: 30),
                Container(
                  child: Center(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        ElevatedButton(
                          onPressed: () {
                            Navigator.push(
                              context,
                              new MaterialPageRoute(
                                builder: (context) => new GolfCartHomeScreen(),
                              ),
                            );
                          },
                          child: const Text(
                            'CANCEL',
                            style: TextStyle(
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                              color: CustomColors.WelakaOneBlueDark,
                            ),
                          ),
                          style: ElevatedButton.styleFrom(
                            primary: CustomColors.WelakaOneWhite,
                            fixedSize: const Size(220, 40),
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(50),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

您似乎插入了两次相同的代码。请确保更新您从 form.

分享的代码

我遇到了类似的问题,这里是解决问题的顺序:

Flexible-->SingleChildScrollView-->Form--> Column

Flexible(
   child: SingleChildScrollView(
      child: Form(
         key: _formKey, 
         child: Column(
            children:[Container(
               alignment: Alignment.centerLeft, 
               padding: EdgeInsets.only(left:10, top: 20, bottom: 20), 
                  child: Text("xxx")

如果这没有帮助,请告诉我。