Git Product home page Git Product logo

rect_getter's Introduction

rect_getter

A widget provide a simple way to get child's rectangle information after rendered.

Usage

To use this plugin, add rect_getter as a dependency in your pubspec.yaml file.

Example

// Import package
import 'package:rect_getter/rect_getter.dart';

// Instantiate it

var globalKey = RectGetter.createGlobalKey();
var rectGetter = RectGetter(
    key: globalKey,
    child: _child,
);

or

var rectGetter = RectGetter.defaultKey(
    child: _child,
);


// and add it to your layout .

// then you can get rect by

Rect rect = rectGetter.getRect();

or

Rect rect = RectGetter.getRectFromKey(globalKey);

Getting Started

For help getting started with Flutter, view our online documentation.

For help on editing package code, view the documentation.

rect_getter's People

Contributors

debuggerx01 avatar wayne900204 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

rect_getter's Issues

Error inserting new listview item

I got the error "package:flutter/src/rendering/object.dart': Failed assertion: line 2377 pos 12: 'attached': is not true"

Code:
....
itemBuilder: (context, index){
Rect itemRect = RectGetter.getRectFromKey(myobject.key);
myobject.isShowing = (itemRect != null && !(itemRect.top > this._screenRect.bottom || itemRect.bottom < this._screenRect.top));
....

I got the error after inserting a new item in the listview:
myNewObject.key = RectGetter.createGlobalKey();
this._myList.insert(0, myNewObject);

But if I update all listview items key: the error is avoided...

Is there a solution for this?

进入下个页面时,getRectFromKey报错

我的代码逻辑大致为根据 https://juejin.cn/post/6844903650737782792 实现的滚动监听哪些item可见,根据列表位置判断显示、隐藏顶部TabBar(使用AnimatedSwitcher),
但是,在一定情况下,当点击某个按钮进入下个页面的时候,NotificationListener上的onNotification也会执行,同时RectGetter.getRectFromKey会报错:
周末补上代码PoC。

======== Exception caught by rendering library =====================================================
The following assertion was thrown during performLayout():
RenderBox.size accessed beyond the scope of resize, layout, or permitted parent access. RenderBox can always access its own size, otherwise, the only object that is allowed to read RenderBox.size is its parent, if they have said they will. It you hit this assert trying to access a child's size, pass "parentUsesSize: true" to that child's layout().
'package:flutter/src/rendering/box.dart':
Failed assertion: line 1792 pos 13: 'debugDoingThisResize || debugDoingThisLayout ||
              (RenderObject.debugActiveLayout == parent && _size._canBeUsedByParent)'


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=BUG.md

The relevant error-causing widget was: 
  CustomScrollView-[LabeledGlobalKey<_RectGetterState>#53311] file:///C:/Users/Admin/Documents/repo/edu/lib/ui/pages/home/home_course/class_detail_page.dart:286:30
When the exception was thrown, this was the stack: 
#2      RenderBox.size.<anonymous closure> (package:flutter/src/rendering/box.dart:1792:13)
#3      RenderBox.size (package:flutter/src/rendering/box.dart:1805:6)
#4      RenderFractionalTranslation.applyPaintTransform (package:flutter/src/rendering/proxy_box.dart:2626:24)
#5      RenderObject.getTransformTo (package:flutter/src/rendering/object.dart:2392:24)
#6      RectGetter.getRectFromKey (package:rect_getter/rect_getter.dart:20:31)
...
The following RenderObject was being processed when the exception was fired: RenderViewport#1950b NEEDS-LAYOUT NEEDS-PAINT
...  needs compositing
...  parentData: <none> (can use size)
...  constraints: BoxConstraints(0.0<=w<=392.7, 0.0<=h<=707.8)
...  layer: OffsetLayer#b4153
...    engine layer: OffsetEngineLayer#7c566
...    offset: Offset(0.0, 0.0)
...  size: Size(392.7, 707.8)
...  axisDirection: down
...  crossAxisDirection: right
...  offset: ScrollPositionWithSingleContext#b1a8f(offset: 4511.0, range: 0.0..303.2, viewport: 707.8, ScrollableState, ClampingScrollPhysics -> RangeMaintainingScrollPhysics, BallisticScrollActivity#03274(AnimationController#6e006(▶ 4510.978; for BallisticScrollActivity)), ScrollDirection.idle)
...  anchor: 0.0
RenderObject: RenderViewport#1950b NEEDS-LAYOUT NEEDS-PAINT
  needs compositing
  parentData: <none> (can use size)
  constraints: BoxConstraints(0.0<=w<=392.7, 0.0<=h<=707.8)
  layer: OffsetLayer#b4153
    engine layer: OffsetEngineLayer#7c566
    offset: Offset(0.0, 0.0)
  size: Size(392.7, 707.8)
  axisDirection: down
  crossAxisDirection: right
  offset: ScrollPositionWithSingleContext#b1a8f(offset: 4511.0, range: 0.0..303.2, viewport: 707.8, ScrollableState, ClampingScrollPhysics -> RangeMaintainingScrollPhysics, BallisticScrollActivity#03274(AnimationController#6e006(▶ 4510.978; for BallisticScrollActivity)), ScrollDirection.idle)
  anchor: 0.0
...  center child: RenderSliverToBoxAdapter#d2926 relayoutBoundary=up1 NEEDS-PAINT
...    needs compositing
...    parentData: paintOffset=Offset(0.0, 0.0) (can use size)
...    constraints: SliverConstraints(AxisDirection.down, GrowthDirection.forward, ScrollDirection.idle, scrollOffset: 4511.0, remainingPaintExtent: 707.8, crossAxisExtent: 392.7, crossAxisDirection: AxisDirection.right, viewportMainAxisExtent: 707.8, remainingCacheExtent: 1207.8, cacheOrigin: -250.0)
...    geometry: SliverGeometry(scrollExtent: 360.0, hidden, maxPaintExtent: 360.0, hasVisualOverflow: true)
...      scrollExtent: 360.0
...      hidden
...      maxPaintExtent: 360.0
...      hasVisualOverflow: true
...    child: RenderFlex#ef6f1 relayoutBoundary=up2
...      needs compositing
...      parentData: paintOffset=Offset(0.0, -4511.0) (can use size)
...      constraints: BoxConstraints(w=392.7, 0.0<=h<=Infinity)
...      size: Size(392.7, 360.0)
...      direction: vertical
...      mainAxisAlignment: start
...      mainAxisSize: max
...      crossAxisAlignment: center
...      verticalDirection: down
...      child 1: RenderStack#bdb03 relayoutBoundary=up3
...        parentData: offset=Offset(0.0, 0.0); flex=null; fit=null (can use size)
...        constraints: BoxConstraints(0.0<=w<=392.7, 0.0<=h<=Infinity)
...        size: Size(392.7, 165.0)
...        alignment: AlignmentDirectional.topStart
...        textDirection: ltr
...        fit: loose
...        child 1: RenderPadding#892df relayoutBoundary=up4
...          parentData: not positioned; offset=Offset(0.0, 0.0) (can use size)
...          constraints: BoxConstraints(0.0<=w<=392.7, 0.0<=h<=Infinity)
...          size: Size(392.7, 165.0)
...          padding: EdgeInsets(15.0, 0.0, 15.0, 0.0)
...          textDirection: ltr
...        child 2: RenderLimitedBox#9f74d relayoutBoundary=up4
...          parentData: not positioned; offset=Offset(0.0, 0.0) (can use size)
...          constraints: BoxConstraints(0.0<=w<=392.7, 0.0<=h<=Infinity)
...          size: Size(392.7, 0.0)
...          maxWidth: 0.0
...          maxHeight: 0.0
...      child 2: RenderOffstage#f2a8e
...        parentData: offset=Offset(196.4, 165.0); flex=null; fit=null (can use size)
...        constraints: BoxConstraints(0.0<=w<=392.7, 0.0<=h<=Infinity)
...        size: Size(0.0, 0.0)
...        offstage: true
...        child: RenderLimitedBox#8f7db NEEDS-PAINT
...          parentData: <none>
...          constraints: BoxConstraints(0.0<=w<=392.7, 0.0<=h<=Infinity)
...          size: Size(392.7, 0.0)
...          maxWidth: 0.0
...          maxHeight: 0.0
...      child 3: RenderPadding#bafa6 relayoutBoundary=up3
...        needs compositing
...        parentData: offset=Offset(0.0, 165.0); flex=null; fit=null (can use size)
...        constraints: BoxConstraints(0.0<=w<=392.7, 0.0<=h<=Infinity)
...        size: Size(392.7, 190.0)
...        padding: EdgeInsets(15.0, 12.0, 15.0, 13.0)
...        textDirection: ltr
...        child: RenderFlex#9f4ff relayoutBoundary=up4
...          needs compositing
...          parentData: offset=Offset(15.0, 12.0) (can use size)
...          constraints: BoxConstraints(0.0<=w<=362.7, 0.0<=h<=Infinity)
...          size: Size(362.7, 165.0)
...          direction: vertical
...          mainAxisAlignment: start
...          mainAxisSize: max
...          crossAxisAlignment: center
...          verticalDirection: down
...      child 4: RenderConstrainedBox#e910e relayoutBoundary=up3
...        parentData: offset=Offset(0.0, 355.0); flex=null; fit=null (can use size)
...        constraints: BoxConstraints(0.0<=w<=392.7, 0.0<=h<=Infinity)
...        size: Size(392.7, 5.0)
...        additionalConstraints: BoxConstraints(0.0<=w<=Infinity, h=5.0)
...        child: _RenderColoredBox#8305c relayoutBoundary=up4
...          parentData: <none> (can use size)
...          constraints: BoxConstraints(0.0<=w<=392.7, h=5.0)
...          size: Size(392.7, 5.0)
...          behavior: opaque
...      child 5: RenderLimitedBox#b4f84 relayoutBoundary=up3
...        parentData: offset=Offset(0.0, 360.0); flex=null; fit=null (can use size)
...        constraints: BoxConstraints(0.0<=w<=392.7, 0.0<=h<=Infinity)
...        size: Size(392.7, 0.0)
...        maxWidth: 0.0
...        maxHeight: 0.0
...        child: RenderConstrainedBox#e44a1 relayoutBoundary=up4
...          parentData: <none> (can use size)
...          constraints: BoxConstraints(0.0<=w<=392.7, h=0.0)
...          size: Size(392.7, 0.0)
...          additionalConstraints: BoxConstraints(biggest)
...  child 1: RenderSliverList#886fb relayoutBoundary=up1 NEEDS-PAINT
...    needs compositing
...    parentData: paintOffset=Offset(0.0, 0.0) (can use size)
...    constraints: SliverConstraints(AxisDirection.down, GrowthDirection.forward, ScrollDirection.idle, scrollOffset: 4151.0, remainingPaintExtent: 707.8, crossAxisExtent: 392.7, crossAxisDirection: AxisDirection.right, viewportMainAxisExtent: 707.8, remainingCacheExtent: 1207.8, cacheOrigin: -250.0)
...    geometry: SliverGeometry(scrollExtent: 651.0, hidden, maxPaintExtent: 651.0)
...      scrollExtent: 651.0
...      hidden
...      maxPaintExtent: 651.0
...    currently live children: 2 to 2
...    child with index 2: RenderIndexedSemantics#c7b1c relayoutBoundary=up2 NEEDS-PAINT
...      needs compositing
...      parentData: index=2; layoutOffset=480.0 (can use size)
...      constraints: BoxConstraints(w=392.7, 0.0<=h<=Infinity)
...      semantic boundary
...      size: Size(392.7, 171.0)
...      index: 2
...      child: RenderRepaintBoundary#aa7b5 relayoutBoundary=up3 NEEDS-PAINT
...        needs compositing
...        parentData: <none> (can use size)
...        constraints: BoxConstraints(w=392.7, 0.0<=h<=Infinity)
...        layer: OffsetLayer#af872
...          engine layer: OffsetEngineLayer#3e479
...          offset: Offset(0.0, -3671.0)
...        size: Size(392.7, 171.0)
...        metrics: 94.4% useful (5 bad vs 84 good)
...        diagnosis: this is an outstandingly useful repaint boundary and should definitely be kept
...        child: RenderDecoratedBox#5742d relayoutBoundary=up4 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...          parentData: <none> (can use size)
...          constraints: BoxConstraints(w=392.7, 0.0<=h<=Infinity)
...          size: Size(392.7, 171.0)
...          decoration: BoxDecoration
...          configuration: ImageConfiguration(bundle: PlatformAssetBundle#35372(), devicePixelRatio: 2.8, locale: zh, textDirection: TextDirection.ltr, platform: android)


======== Exception caught by rendering library =====================================================
The following assertion was thrown during performLayout():
'package:flutter/src/widgets/scroll_position.dart': Failed assertion: line 504 pos 12: 'haveDimensions == (_lastMetrics != null)': is not true.


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=BUG.md

The relevant error-causing widget was: 
  CustomScrollView-[LabeledGlobalKey<_RectGetterState>#53311] file:///C:/Users/Admin/Documents/repo/edu/lib/ui/pages/home/home_course/class_detail_page.dart:286:30
When the exception was thrown, this was the stack: 
#2      ScrollPosition.applyContentDimensions (package:flutter/src/widgets/scroll_position.dart:504:12)
#3      RenderViewport.performLayout (package:flutter/src/rendering/viewport.dart:1474:20)
#4      RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:1634:7)
#5      PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:884:18)
#6      RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:436:19)
...

Can we use this package for too many listitems?

I have a ListView.builder() with 10000+ items. And I want to use var globalKey = RectGetter.createGlobalKey(); for each item. Do you think this would be a performance/memory issue to use this package like this?

I want to be able to know the Rect of the item the user presses.

getting scroll position such as top or bottom with RectGetter

with RectGetter widget we can detect visible items into ListView, for example:

List<int> getVisible() {
  var rect = RectGetter.getRectFromKey(listViewKey);
  var _items = <int>[];
  _keys.forEach((index, key) {
    var itemRect = RectGetter.getRectFromKey(key);
    if (itemRect != null && !(itemRect.top > rect.bottom || itemRect.bottom < rect.top)) _items.add(index);
  });

  return _items;
}

in this code i want to detect ListView scroll position achieve on top of list or bottom of list, maybe you guess this issue can be solve with ScrollController, but i check that and that have a bit problem and i can't resolve with that

now how can i get scroll position such as top or bottom with RectGetter?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.