【发布时间】:2021-04-21 04:13:58
【问题描述】:
如何在下面附加的另一个 SingleChildScrollView(Vertical) 代码中使 Flutter SingleChildScrollView(Horizontal) 工作?
我收到以下异常:
未处理的异常:无法对没有大小的渲染框进行测试。 在此 RenderBox 上调用了 hitTest() 方法:_RenderScrollSemantics#0e053: 需要合成 创建者:_ScrollSemantics-[GlobalKey#35899] ← Scrollable ← SingleChildScrollView ← ColoredBox ← ConstrainedBox ← Container ← Column ← _SingleChildViewport ← IgnorePointer-[GlobalKey#364d1] ← Semantics ← Listener ← _GestureSemantics ←⋯ parentData:(可以使用大小) 约束:BoxConstraints(w=414.0, h=896.0) 语义边界 尺寸:缺失 虽然这个节点没有标记为需要布局,但它的大小没有设置。 RenderBox 对象必须具有明确的大小才能进行命中测试。确保有问题的 RenderBox 在布局期间设置其大小。 #0 RenderBox.hitTest。 (包:flutter/src/rendering/box.dart:2386:9) #1 RenderBox.hitTest (package:flutter/src/rendering/box.dart:2401:6) #2 RenderProxyBoxMixin.hitTestChildren (p [VERBOSE-2:ui_dart_state.cc(177)] 未处理的异常:无法测试没有大小的渲染框。 在此 RenderBox 上调用了 hitTest() 方法:_RenderScrollSemantics#0e053: 需要合成 创建者:_ScrollSemantics-[GlobalKey#35899] ← Scrollable ← SingleChildScrollView ← ColoredBox ← ConstrainedBox ← Container ← Column ← _SingleChildViewport ← IgnorePointer-[GlobalKey#364d1] ← Semantics ← Listener ← _GestureSemantics ←⋯ parentData:(可以使用大小) 约束:BoxConstraints(w=414.0, h=896.0) 语义边界 尺寸:缺失 虽然这个节点没有标记为需要布局,但它的大小没有设置。 RenderBox 对象必须具有明确的大小才能进行命中测试。确保有问题的 RenderBox 在布局期间设置其大小。 #0 RenderBox.hitTest。 (包:flutter/src/rendering/box.dart:2386:9) #1 RenderBox.hitTest (package:flutter/src/rendering/box.dart:2401:6) #2 RenderProxyBoxMixin.hitTestChildren (p
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);
}
}
【问题讨论】:
标签: flutter