如何在下面附加的另一个SingleChildScrollView(垂直)代码中使Flutter SingleChildScrollView(水平)工作?
我得到了以下异常:
未处理的异常:无法命中测试没有大小的渲染框。在此RenderBox上调用了hitTest()方法:_RenderScrollSemantics#0e053: needs creator:_ScrollSemantics-GlobalKey#35899 ColoredBox ConstrainedBox _SingleChildViewport _GestureSemantics parentData ConstrainedBox←←parentData BoxConstraints-GlobalKey#364d1←Semantics←Listener←size:(can Can use size) parentData:(Can Can Use size ) constraints: BoxConstraints(w=414.0,h=896.0)语义边界大小:←虽然此节点未标记为需要布局,但未设置其大小。RenderBox对象必须具有显式大小,然后才能进行命中测试。确保有问题的RenderBox在布局期间设置其大小。#0 RenderBox.hitTest。(package: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: needs creator:_ScrollSemantics-GlobalKey#35899 ColoredBox ConstrainedBox _SingleChildViewport _GestureSemantics parentData ConstrainedBox←←parentData BoxConstraints-GlobalKey#364d1←Semantics←Listener←size:(can Can use size) parentData:(Can Can Use size ) constraints: BoxConstraints(w=414.0,h=896.0)语义边界大小:←虽然此节点未标记为需要布局,但未设置其大小。RenderBox对象必须具有显式大小,然后才能进行命中测试。确保有问题的RenderBox在布局期间设置其大小。#0 RenderBox.hitTest。(package: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);
}
}
发布于 2021-01-16 19:45:58
尝试将垂直的单卷视图放入容器中,并将该容器的宽度和高度设置为屏幕宽度和高度。另一方面,如果您使用ListView进行水平滚动,或者使用ListView.builder进行项目类型相同的滚动,则更合适
ListView(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
children: <Widget>[
//your widget items here
],
),
发布于 2021-01-17 19:17:02
在这里,您的SingleChildScrollView项将垂直滚动,ListView.builder项将水平绘制项,(默认情况下),在列表视图构建器的情况下,您需要固定特定/动态高度以避免错误。
SingleChildScrollView(
child: Container(
height: 100,
child: ListView.builder(
itemBuilder: ...),
),
)
在您的代码中,使用:
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.max,
https://stackoverflow.com/questions/65747849
复制相似问题