📜  Flutter – 启动键盘时出现像素溢出错误

📅  最后修改于: 2022-05-13 01:55:04.365000             🧑  作者: Mango

Flutter – 启动键盘时出现像素溢出错误

Flutter中很常见的像素溢出错误,当一个 Column 中的小部件太多并且在打开键盘时它们无法完全显示时,就会收到这类 Pixel Overflow 错误。例子 :

此 UI 的代码是:

Dart
import 'package:flutter/material.dart';
  
import 'components.dart';
  
void main() {
  runApp(const MyApp());
}
  
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.max,
            children: [
              GFGLogo(),
              SizedBox(height: 50),
              MyTextField(label: "username"),
              SizedBox(height: 15),
              MyTextField(label: "password"),
              SizedBox(height: 15),
              MyTextField(label: "confirm password"),
              SizedBox(height: 50),
              MyButton(),
            ],
          ),
        ),
      ),
    );
  }
}


Dart
import 'package:flutter/material.dart';
  
import 'components.dart';
  
void main() {
  runApp(const MyApp());
}
  
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Center(
            child: SingleChildScrollView(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                mainAxisSize: MainAxisSize.max,
                children: [
                  GFGLogo(),
                  SizedBox(height: 50),
                  MyTextField(label: "username"),
                  SizedBox(height: 15),
                  MyTextField(label: "password"),
                  SizedBox(height: 15),
                  MyTextField(label: "confirm password"),
                  SizedBox(height: 50),
                  MyButton(),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}


解决方案 :

解决此溢出错误的解决方案是使您的整个小部件或在我们的例子中的列可滚动。我们可以通过将 Column 包装在 SingleChildScrollView 中来做到这一点。此外,将 SingleChildScrollView 与 Center 包装在一起,以便整个 UI 居中。之后,一切正常,不会出现溢出错误。

Dart

import 'package:flutter/material.dart';
  
import 'components.dart';
  
void main() {
  runApp(const MyApp());
}
  
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Center(
            child: SingleChildScrollView(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                mainAxisSize: MainAxisSize.max,
                children: [
                  GFGLogo(),
                  SizedBox(height: 50),
                  MyTextField(label: "username"),
                  SizedBox(height: 15),
                  MyTextField(label: "password"),
                  SizedBox(height: 15),
                  MyTextField(label: "confirm password"),
                  SizedBox(height: 50),
                  MyButton(),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

输出: