【问题标题】:Flutter SingleChildScrollView(Horizontal) within another SingleChildScrollView(Vertical)在另一个 SingleChildScrollView(Vertical) 中颤动 SingleChildScrollView(Horizo​​ntal)
【发布时间】:2021-04-21 04:13:58
【问题描述】:

如何在下面附加的另一个 SingleChildScrollView(Vertical) 代码中使 Flutter SingleChildScrollView(Horizo​​ntal) 工作?

我收到以下异常:

未处理的异常:无法对没有大小的渲染框进行测试。 在此 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


    【解决方案1】:

    在这里,您的 SingleChildScrollView 项目将垂直滚动,ListView.builder 项目将水平绘制项目(默认情况下),如果使用列表视图构建器,您需要修复特定/动态高度以避免错误。

    SingleChildScrollView(
          child: Container(
              height: 100,
                 child: ListView.builder(
                      itemBuilder: ...),
                  ),
                )
    

    & 在您的代码中,使用:

       Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          mainAxisSize: MainAxisSize.max,
    

    【讨论】:

    • 能否请您参考所附代码给我一个示例。谢谢
    【解决方案2】:

    尝试将垂直的singlechildscrollview放到一个容器中,并将这个容器的宽高设置为屏幕宽高。另一方面,如果您使用 ListView 进行水平滚动则更合适,如果您的项目类型相同,则使用 ListView.builder

    ListView(
        scrollDirection: Axis.horizontal,
        shrinkWrap:      true,
        children: <Widget>[
            //your widget items here
        ],
    ),
    

    【讨论】:

    • 这个方法不起作用,它给出的异常和以前一样!
    • 我刚刚编辑了我的帖子,你能试试这个吗
    • 您能否具体提及我附加的代码中的哪一部分来包装 ListView?因为我仍然遇到错误!
    猜你喜欢
    • 2021-10-28
    • 2021-12-18
    • 1970-01-01
    • 2019-03-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2021-08-11
    • 1970-01-01
    相关资源
    最近更新 更多