Example #1
0
 public static void _focusAndEnsureVisible(
     FocusNode node,
     ScrollPositionAlignmentPolicy alignmentPolicy = ScrollPositionAlignmentPolicy.explicitPolicy
     )
 {
     node.requestFocus();
     Scrollable.ensureVisible(node.context, alignment: 1.0f, alignmentPolicy: alignmentPolicy);
 }
Example #2
0
        public Future ensureVisible(
            RenderObject renderObject,
            float alignment   = 0.0f,
            TimeSpan?duration = null,
            Curve curve       = null,
            ScrollPositionAlignmentPolicy alignmentPolicy = ScrollPositionAlignmentPolicy.explicitPolicy
            )
        {
            D.assert(renderObject.attached);
            RenderAbstractViewport viewport = RenderViewportUtils.of(renderObject);

            D.assert(viewport != null);

            float target = 0f;

            switch (alignmentPolicy)
            {
            case ScrollPositionAlignmentPolicy.explicitPolicy:
                target = viewport.getOffsetToReveal(renderObject, alignment).offset.clamp(minScrollExtent, maxScrollExtent);
                break;

            case ScrollPositionAlignmentPolicy.keepVisibleAtEnd:
                target = viewport.getOffsetToReveal(renderObject, 1.0f).offset.clamp(minScrollExtent, maxScrollExtent);
                if (target < pixels)
                {
                    target = pixels;
                }
                break;

            case ScrollPositionAlignmentPolicy.keepVisibleAtStart:
                target = viewport.getOffsetToReveal(renderObject, 0.0f).offset.clamp(minScrollExtent, maxScrollExtent);
                if (target > pixels)
                {
                    target = pixels;
                }
                break;
            }
            if (foundation_.FloatEqual(target, pixels))
            {
                return(Future.value());
            }

            duration = duration ?? TimeSpan.Zero;
            if (duration == TimeSpan.Zero)
            {
                jumpTo(target);
                return(Future.value());
            }

            curve = curve ?? Curves.ease;
            return(animateTo(target, duration: duration.Value, curve: curve));
        }
Example #3
0
        public static Future ensureVisible(
            BuildContext context,
            float alignment   = 0.0f,
            TimeSpan?duration = null,
            Curve curve       = null,
            ScrollPositionAlignmentPolicy alignmentPolicy = ScrollPositionAlignmentPolicy.explicitPolicy
            )
        {
            duration = duration ?? TimeSpan.Zero;
            curve    = curve ?? Curves.ease;
            List <Future> futures = new List <Future>();

            ScrollableState scrollable = of(context);

            while (scrollable != null)
            {
                futures.Add(scrollable.position.ensureVisible(
                                context.findRenderObject(),
                                alignment: alignment,
                                duration: duration,
                                curve: curve,
                                alignmentPolicy: alignmentPolicy
                                ));
                context    = scrollable.context;
                scrollable = of(context);
            }

            if (futures.isEmpty() || duration == TimeSpan.Zero)
            {
                return(Future.value());
            }

            if (futures.Count == 1)
            {
                return(futures.Single());
            }

            return(Future.wait <object>(futures));
        }
Example #4
0
        public bool _popPolicyDataIfNeeded(TraversalDirection direction, FocusScopeNode nearestScope, FocusNode focusedChild)
        {
            _DirectionalPolicyData policyData = _policyData[nearestScope];

            if (policyData != null && policyData.history.isNotEmpty() && policyData.history.First().direction != direction)
            {
                if (policyData.history.Last().node.parent == null)
                {
                    invalidateScopeData(nearestScope);
                    return(false);
                }
                bool popOrInvalidate(TraversalDirection _direction)
                {
                    FocusNode lastNode = policyData.history.removeLast().node;

                    if (Scrollable.of(lastNode.context) != Scrollable.of(FocusManagerUtils.primaryFocus.context))
                    {
                        invalidateScopeData(nearestScope);
                        return(false);
                    }
                    ScrollPositionAlignmentPolicy alignmentPolicy = ScrollPositionAlignmentPolicy.explicitPolicy;

                    switch (_direction)
                    {
                    case TraversalDirection.up:
                    case TraversalDirection.left:
                        alignmentPolicy = ScrollPositionAlignmentPolicy.keepVisibleAtStart;
                        break;

                    case TraversalDirection.right:
                    case TraversalDirection.down:
                        alignmentPolicy = ScrollPositionAlignmentPolicy.keepVisibleAtEnd;
                        break;
                    }
                    FocusTravesalUtils._focusAndEnsureVisible(
                        lastNode,
                        alignmentPolicy: alignmentPolicy
                        );
                    return(true);
                }

                switch (direction)
                {
                case TraversalDirection.down:
                case TraversalDirection.up:
                    switch (policyData.history.First().direction)
                    {
                    case TraversalDirection.left:
                    case TraversalDirection.right:
                        invalidateScopeData(nearestScope);
                        break;

                    case TraversalDirection.up:
                    case TraversalDirection.down:
                        if (popOrInvalidate(direction))
                        {
                            return(true);
                        }
                        break;
                    }
                    break;

                case TraversalDirection.left:
                case TraversalDirection.right:
                    switch (policyData.history.First().direction)
                    {
                    case TraversalDirection.left:
                    case TraversalDirection.right:
                        if (popOrInvalidate(direction))
                        {
                            return(true);
                        }
                        break;

                    case TraversalDirection.up:
                    case TraversalDirection.down:
                        invalidateScopeData(nearestScope);
                        break;
                    }
                    break;
                }
            }
            if (policyData != null && policyData.history.isEmpty())
            {
                invalidateScopeData(nearestScope);
            }
            return(false);
        }