Skip to content

Default focus traversal does not work on reversed scrollables #128749

@Piinks

Description

@Piinks

Found while implementing ensureVisible for 2D

When focus is traversing a scrollable widget, Scrollable.ensureVisible is called to bring a newly focused node into view if it is not currently. This calls ScrollPosition.ensureVisible and then RenderViewport.getOffsetToReveal.
When the default focus request callback does this, it extrapolates the ScrollPositionAlignmentPolicy (where the node should be located when it comes into view) based on the directionality of the keyboard input. This does not however account for the actual axis direction of the scrollable. The focus callback does not actually know anything about the scrollable or if one exists (ensureVisible is a static method).

In the below case, the alignment policy was being set to keepVisibleAtStart, but in the case of a reversed scrollable, this should be flipped.

Expected:

Screen.Recording.2023-06-12.at.5.51.53.PM.mov

Actual:

Screen.Recording.2023-06-12.at.5.44.07.PM.mov
import 'dart:math' as math;
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

void main() {
  runApp(const SimpleTest());
}

class SimpleTest extends StatelessWidget {
  const SimpleTest({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body:
            ListView(
              reverse: true,
              primary: true,
              children: List.generate(50, (int index) {
                return SizedBox(
                      height: 200.0,
                      width: 200.0,
                      child: Column(
                        children: [
                          Checkbox(value: false, onChanged: (value){}),
                          Text(index.toString()),
                        ],
                      ),
                    );
              })
            ),
      ),
    );
  }
}

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work lista: desktopRunning on desktopa: text inputEntering text in a text field or keyboard related problemsf: focusFocus traversal, gaining or losing focusf: scrollingViewports, list views, slivers, etc.frameworkflutter/packages/flutter repository. See also f: labels.waiting for PR to land (fixed)A fix is in flight

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions