flutter - 在另一个 SingleChildScrollView(Vertical) 中颤动 SingleChildScrollView(Horizontal)
问题描述
如何在下面附加的另一个 SingleChildScrollView(Vertical) 代码中使 Flutter SingleChildScrollView(Horizontal) 工作?
我收到以下异常:
未处理的异常:无法测试没有大小的渲染框。在此 RenderBox 上调用了 hitTest() 方法:_RenderScrollSemantics#0e053:需要合成创建者:_ScrollSemantics-[GlobalKey#35899] ← 可滚动 ← SingleChildScrollView ← ColoredBox ← ConstrainedBox ← Container ← Column ← _SingleChildViewport ← IgnorePointer-[GlobalKey#364d1] ← Semantics ← Listener ← _GestureSemantics ← ⋯ parentData: (可以使用大小) 约束: BoxConstraints(w=414.0, h=896.0) 语义边界大小: MISSING 虽然这个节点没有标记为需要布局,但它的大小没有设置。RenderBox 对象必须具有明确的大小才能进行命中测试。确保有问题的 RenderBox 在布局期间设置其大小。#0 RenderBox.hitTest。(package:flutter/src/rendering/box.dart:2386:9) #1 RenderBox.hitTest (package:flutter/src/rendering/box.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() => runApp(
MaterialApp(
home: HomeScreen(),
),
);
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
SizeConfig().init(context);
return Scaffold(
appBar: AppBar(
title: Text('Grid Demo'),
),
body: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Container(
width: SizeConfig.screenWidth,
height: SizeConfig.screenHeight,
color: Colors.grey[200],
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Grid(),
),
),
SizedBox(
height: 50,
),
Container(color: Colors.grey, height: 200),
],
),
),
);
}
}
class Grid extends StatelessWidget {
List<Positioned> getUnits() {
double _unitRadius = 50;
List<Positioned> _units = [];
double _leftCoordinate = 0;
double _bottomCoordinate = 0;
double _margin = 5;
double _stepFromLeft = _unitRadius + _margin;
double _stepFromBottom = _unitRadius + _margin;
int _maxColumns = 10;
int _maxRows = 10;
for (int i = 0; i < _maxRows; i++) {
for (int j = 0; j < _maxColumns; j++) {
_units.add(Positioned(
bottom: _bottomCoordinate,
left: _leftCoordinate,
child: Container(
width: _unitRadius,
height: _unitRadius,
decoration: BoxDecoration(
color: Colors.green,
),
child: Center(child: Text('$i $j')),
)));
_leftCoordinate += _stepFromLeft;
}
_leftCoordinate = 0;
_bottomCoordinate += _stepFromBottom;
}
return _units;
}
@override
Widget build(BuildContext context) {
return Stack(
children: getUnits(),
);
}
}
class SizeConfig {
static MediaQueryData _mediaQueryData;
static double screenWidth;
static double screenHeight;
static double blockSizeHorizontal;
static double blockSizeVertical;
static double _safeAreaHorizontal;
static double _safeAreaVertical;
static double safeBlockHorizontal;
static double safeBlockVertical;
void init(BuildContext context) {
_mediaQueryData = MediaQuery.of(context);
screenWidth = _mediaQueryData.size.width;
screenHeight = _mediaQueryData.size.height;
blockSizeHorizontal = screenWidth;
blockSizeVertical = screenHeight;
_safeAreaHorizontal =
_mediaQueryData.padding.left + _mediaQueryData.padding.right;
_safeAreaVertical =
_mediaQueryData.padding.top + _mediaQueryData.padding.bottom;
safeBlockHorizontal = (screenWidth - _safeAreaHorizontal);
safeBlockVertical = (screenHeight - _safeAreaVertical);
}
}
解决方案
在这里,您的 SingleChildScrollView 项目将垂直滚动,ListView.builder 项目将水平绘制项目(默认情况下),如果是列表视图构建器,您需要修复特定/动态高度以避免错误。
SingleChildScrollView(
child: Container(
height: 100,
child: ListView.builder(
itemBuilder: ...),
),
)
& 在您的代码中,使用:
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.max,
推荐阅读
- error-handling - 在默认流上启动内核是否会“捕获”其执行错误?
- python - QLabel 和自动换行:如何以逗号为基础换行(与空格)
- android - Xamarin- MainActivity.OnNewIntent 在单击时应用程序处于后台时使用错误的参数调用推送通知
- vba - 连接到雪花的 VBA 生成错误
- sql-server - 在 SQL 中生成每日总和的最有效方法?
- python - 在与某些参数匹配的对象列表中查找值的总和
- c++ - 指向常量值的 const 指针示例
- jquery - 如何将特定代码添加到我的链接中?jQuery
- reactjs - React debounce search 在初始化之前无法访问'updateSearchQuery'
- ajax - 如何在基于算法的无限滚动中防止重复