android - 如何在android虚拟键盘顶部设置 'A RenderFlex overflowed by 61 pixels on the bottom.'

标签 android flutter dart flutter-layout

这是用户界面。下面是 main.dart 文件和错误信息  this is UI for the error 我在我的 Flutter 应用程序中使用文本字段,它在虚拟键盘顶部给出了渲染弹性错误。请检查以下代码。 在底部我添加了错误消息。 之前我尝试了所有其他方法来解决这个问题,但我不同意

import 'package:flutter/material.dart';
import './transaction.dart';
import 'package:intl/intl.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Expenses Tracker',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  final List<Transaction> transactions = [
    Transaction(
      id: 't1',
      title: 'New Shoes',
      amount: 87.25,
      date: DateTime.now(),
    ),
    Transaction(
      id: 't2',
      title: 'Weekly Groceries',
      amount: 83.25,
      date: DateTime.now(),
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Expenses Tracker'),
      ),
      body: Column(
        children: <Widget>[
          Container(
            width: double.infinity,
            child: Card(
              child: Text("Chart!"),
              color: Colors.deepOrange,
              elevation: 9,
            ),
          ),
          Card(
            elevation: 5,
            child: Container(
              padding: EdgeInsets.all(10),
              child: Column(
                children: <Widget>[
                  TextField(
                    decoration: InputDecoration(labelText: 'Title'),
                  ),
                  TextField(
                    decoration: InputDecoration(labelText: 'Amount'),
                  ),
                ],
              ),
            ),
          ),
          Column(
            children: transactions.map((tx) {
              return Card(
                child: Row(
                  children: <Widget>[
                    Container(
                      margin: EdgeInsets.symmetric(
                        vertical: 10,
                        horizontal: 15,
                      ),
                      decoration: BoxDecoration(
                        border: Border.all(
                          color: Colors.purple,
                          width: 3,
                        ),
                      ),
                      padding: EdgeInsets.all(10),
                      child: Text(
                        '\$ ${tx.amount}',
                        style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 20,
                          color: Colors.purple,
                        ),
                      ),
                    ),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text(
                          tx.title,
                          style: TextStyle(
                            fontWeight: FontWeight.bold,
                            fontSize: 18,
                          ),
                        ),
                        Text(
                          DateFormat.yMMMEd().format(tx.date),
                          style: TextStyle(color: Colors.grey, fontSize: 14),
                        ),
                      ],
                    ),
                  ],
                ),
              );
            }).toList(),
          ),
        ],
      ),
    );
  }
}


下面是错误。

════════════════════════════════════════════════════════════════════════════════
══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
The following assertion was thrown during layout:
A RenderFlex overflowed by 61 pixels on the bottom.
The relevant error-causing widget was:
  Column 
lib\main.dart:41
The overflowing RenderFlex has an orientation of Axis.vertical.
The edge of the RenderFlex that is overflowing has been marked in the rendering with a yellow and
black striped pattern. This is usually caused by the contents being too big for the RenderFlex.
Consider applying a flex factor (e.g. using an Expanded widget) to force the children of the
RenderFlex to fit within the available space instead of being sized to their natural size.
This is considered an error condition because it indicates that there is content that cannot be
seen. If the content is legitimately bigger than the available space, consider clipping it with a
ClipRect widget before putting it in the flex, or using a scrollable container rather than a Flex,
like a ListView.
The specific RenderFlex in question is: RenderFlex#0559b relayoutBoundary=up1 OVERFLOWING:
  needs compositing
  creator: Column ← _BodyBuilder ← MediaQuery ← LayoutId-[<_ScaffoldSlot.body>] ←
    CustomMultiChildLayout ← AnimatedBuilder ← DefaultTextStyle ← AnimatedDefaultTextStyle ←
    _InkFeatures-[GlobalKey#39d3c ink renderer] ← NotificationListener<LayoutChangedNotification> ←
    PhysicalModel ← AnimatedPhysicalModel ← ⋯
  parentData: offset=Offset(0.0, 80.0); id=_ScaffoldSlot.body (can use size)
  constraints: BoxConstraints(0.0<=w<=360.0, 0.0<=h<=263.0)
  size: Size(360.0, 263.0)
  direction: vertical
mainAxisAlignment: start
  mainAxisSize: max
  crossAxisAlignment: center
  verticalDirection: down

════════════════════════════════════════════════════════════════════════════════════════════════════

最佳答案

  1. 一个快速的解决方案是阻止 Scaffold 中的小部件在键盘打开时调整自己的大小,但是这样,

some widgets can be obscured by the keyboard

我们可以使用 Scaffold 小部件上的 resizeToAvoidBottomInset 属性来做到这一点。

例子:

 return Scaffold(
      resizeToAvoidBottomInset: false,   //new line
      appBar: AppBar(
        title: Text('Expenses Tracker'),
      ),
      body: Column(
          children: <Widget>[
            ...... // other widgets 
          ],
      ),
    );

  1. 另一种解决方案是将 Column 小部件包装到可滚动的小部件中。 Flutter 提供的内置小部件 SingleChildScrollView 效果很好。这是避免键盘打开时出现 “Bottom overflowed” 错误的最佳解决方案。
 return Scaffold(
      appBar: AppBar(
        title: Text('Expenses Tracker'),
      ),
      body: SingleChildScrollView( // wrap with a scrollable widget
        child: Column(
          children: <Widget>[
            ...... // other widgets 
          ],
        ),
      ),
    );

关于android - 如何在android虚拟键盘顶部设置 'A RenderFlex overflowed by 61 pixels on the bottom.',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63743330/

相关文章:

dart - 是否可以在 Dart 中实现多个接口(interface)?如果是这样,是否可以限制通用参数来实现两者?

android - 如何在 LinearLayoutManager 中重叠项目 - RecyclerView(如堆叠卡片)

android - 使用 native 滚动时使滚动条在 ionic 内容中可见

flutter - 如何在 Flutter 中自定义 Slider 小部件?

dart - 你如何在 Polymer 中调度和监听自定义事件?

flutter - Flutter Pop最佳做法

java - android sdk 等待动画完成

android - 使用联合登录 (OpenID) 从 Android 应用程序对 App Engine 进行身份验证

flutter - 如何使用Flutter和Hive DB更新数据?

dart - 在 ListView 中使用视频播放器 flutter ,返回多个错误