首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ListView.builder错误:[对象在布局过程中被赋予无限大的大小],[BoxConstraints强制无限宽]

ListView.builder错误:[对象在布局过程中被赋予无限大的大小],[BoxConstraints强制无限宽]
EN

Stack Overflow用户
提问于 2022-11-26 10:42:57
回答 1查看 21关注 0票数 0

我刚开始开发一个可以在WorkbookDrawingPage widget上写行的应用程序,在WorkbookDrawingPage小部件下,pdfviewer小部件是通过使用stack放置的。

因此,我希望将WorkbookDrawingPage作为一个ListView,当用户水平滚动或单击next页面按钮时,WorkbookDrawingPage小部件的下一页显示,WorkbookDrawingPage小部件适合屏幕。

我用一个sfPdfViewer小部件和WorkbookDrawingPage封装了一个InteractiveViewer,这样它就可以缩放和平移。

但是当我使用ListView.Builder小部件时,会出现错误。

像这样

代码语言:javascript
运行
复制
The following assertion was thrown during performLayout():
BoxConstraints forces an infinite width.

These invalid constraints were provided to RenderCustomPaint's layout() function by the following function, which probably computed the invalid constraints in question:
  RenderBox.layout (package:flutter/src/rendering/box.dart:2418:11)
The offending constraints were: BoxConstraints(w=Infinity, h=1374.2)
The relevant error-causing widget was: 
  Container Container:file:///C:/Users/park/AndroidStudioProjects/firebase_img_writing_test/lib/screen/workbook_view_page.dart:111:24

he following assertion was thrown during performLayout():
RenderBox was not laid out: RenderConstrainedBox#2a0e8 relayoutBoundary=up5 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
'package:flutter/src/rendering/box.dart':
Failed assertion: line 2001 pos 12: 'hasSize'


Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=2_bug.md

The relevant error-causing widget was: 
  ListView ListView:file:///C:/Users/park/AndroidStudioProjects/firebase_img_writing_test/lib/screen/workbook_view_page.dart:107:24

The following assertion was thrown during performLayout():
RenderIndexedSemantics object was given an infinite size during layout.

This probably means that it is a render object that tries to be as big as possible, but it was put inside another render object that allows its children to pick their own size.
The nearest ancestor providing an unbounded width constraint is: RenderIndexedSemantics#359f9 relayoutBoundary=up3 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...  needs compositing
...  parentData: index=0; layoutOffset=0.0 (can use size)
...  constraints: BoxConstraints(0.0<=w<=Infinity, h=1374.2)
...  size: Size(Infinity, 1374.2)
...  index: 0
The constraints that applied to the RenderIndexedSemantics were: BoxConstraints(0.0<=w<=Infinity, h=1374.2)
The exact size it was given was: Size(Infinity, 1374.2)

See https://flutter.dev/docs/development/ui/layout/box-constraints for more information.

The relevant error-causing widget was: 
  ListView ListView:file:///C:/Users/park/AndroidStudioProjects/firebase_img_writing_test/lib/screen/workbook_view_page.dart:107:24

当我删除InteractiveViewer时,它会显示相同的错误。

这是我的密码

WorkbookViewPage.dart

代码语言:javascript
运行
复制
class WorkbookViewPage extends StatefulWidget {
  String downloadedURL;
  String workbookName;

  WorkbookViewPage(this.downloadedURL, this.workbookName, {super.key});

  @override
  State<WorkbookViewPage> createState() => _WorkbookViewPageState();
}

class _WorkbookViewPageState extends State<WorkbookViewPage> {
  final GlobalKey<SfPdfViewerState> _pdfViewerKey = GlobalKey();
  late PdfViewerController _pdfViewerController;
  int _lastClosedPage = 1;

  bool _memoMode = false;

  // Widget Key
  GlobalKey _viewKey = GlobalKey();

  // pen size control widget Entry
  bool isOverlayVisible = false;
  OverlayEntry? entry;

  // Listener for stylus
  bool isPenTouched = false;

  /// Hide Overlay Widget
  void hideOverlay() {
    entry?.remove();
    entry = null;
    isOverlayVisible = false;
  }

  /// get Last closed page of workbook
  void _loadLastClosedPage() async {
    final pagePrefs = await SharedPreferences.getInstance();
    _lastClosedPage = pagePrefs.getInt('${widget.workbookName}') ?? 1;
  }

  @override
  void initState() {
    super.initState();
    _pdfViewerController = PdfViewerController();
    String workbookName = '${widget.workbookName}';
    _loadLastClosedPage();
    print("======== Loaded workbook name = ${workbookName} ==========");
  }

  @override
  Widget build(BuildContext context) {
    /// Drawing Provider
    var p = context.read<DrawingProvider>();

    /// set _lastClosedPage
    void countLastClosedPage() async {
      final pagePrefs = await SharedPreferences.getInstance();
      setState(() {
        pagePrefs.setInt('${widget.workbookName}', _lastClosedPage);
        _lastClosedPage = (pagePrefs.getInt('${widget.workbookName}') ?? 1);
      });
    }

    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        centerTitle: true,
        title: buildPenFuncWidgets(p, hideOverlay, context),
        elevation: 0,
      ),
      body: SizedBox(
        child: InteractiveViewer(
          panEnabled: _memoMode ? false : true,
          scaleEnabled: _memoMode ? false : true,
          maxScale: 3,
          child: Stack(
            children: [
              IgnorePointer(
                ignoring: true,
                child: SfPdfViewer.network(
                  widget.downloadedURL,
                  controller: _pdfViewerController,
                  key: _pdfViewerKey,
                  pageLayoutMode: PdfPageLayoutMode.single,
                  enableDoubleTapZooming: false,
                  // Save the last closed page number
                  onPageChanged: (details) {
                    _lastClosedPage = details.newPageNumber;
                    countLastClosedPage();
                  },
                  onDocumentLoaded: (details) {
                    _pdfViewerController.jumpToPage(1);
                  },
                  canShowScrollHead: false,
                ),
              ),

              //  PDF view under drawing layer
              ListView.builder(
            scrollDirection: Axis.horizontal,
            itemCount: 30,
            itemBuilder: (context, index) {
            return Container(
              width: double.infinity,
                height: double.infinity,
                child: WorkbookDrawingPage());
              },)
            ],
          ),
        ),
      ),
    );
  }

WorkbookDrawingPage.dart

代码语言:javascript
运行
复制
class _WorkbookDrawingPageState extends State<WorkbookDrawingPage> {

  // OverlayEntry widget key
  GlobalKey _viewKey = GlobalKey();

  // pen size control widget Entry
  bool isOverlayVisible = false;
  OverlayEntry? entry;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var p = Provider.of<DrawingProvider>(context);
    void hideOverlay() {
      entry?.remove();
      entry = null;
      isOverlayVisible = false;
    }

    return CustomPaint(
      painter: DrawingPainter(p.lines),
      child: Listener(
        behavior: HitTestBehavior.translucent,
        // Draw lines when stylus hit the screen
        onPointerDown: (s) {
          if (s.kind == PointerDeviceKind.stylus) {
            p.penMode ? p.penDrawStart(s.localPosition) : null;
            p.highlighterMode
                ? p.highlighterDrawStart(s.localPosition)
                : null;
            p.eraseMode ? p.erase(s.localPosition) : null;

            /// Stylus with button pressed touched the Screen
          } else if (s.kind == PointerDeviceKind.stylus ||
              s.buttons == kPrimaryStylusButton) {
            p.changeEraseModeButtonClicked = true;
          }
        },
        onPointerMove: (s) {
          if (s.kind == PointerDeviceKind.stylus) {
            p.penMode ? p.penDrawing(s.localPosition) : null;
            p.highlighterMode ? p.highlighterDrawing(s.localPosition) : null;
            p.eraseMode ? p.erase(s.localPosition) : null;
          } else if (s.kind == PointerDeviceKind.stylus ||
              s.buttons == kPrimaryStylusButton) {
            p.changeEraseModeButtonClicked = true;
          }
        },
      ),
    );
  }
EN

回答 1

Stack Overflow用户

发布于 2022-11-26 11:31:44

错误发生的原因是约束的删减,我将约束添加到Container Widget中,并修正了MediaQuery.of(context).size.width*1,和错误。

double.infinity没有给出约束

然而,绘制的线条是重复的。

代码语言:javascript
运行
复制
return Container(
      width: MediaQuery.of(context).size.width*1,
      height: MediaQuery.of(context).size.height*1,
      child: CustomPaint(
        painter: DrawingPainter(p.lines),
        child: Listener(
...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74581487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档