scroll_activity_test.dart 13.3 KB
Newer Older
1 2 3 4
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/gestures.dart';
6 7 8 9 10 11
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_test/flutter_test.dart';

List<Widget> children(int n) {
  return List<Widget>.generate(n, (int i) {
12
    return SizedBox(height: 100.0, child: Text('$i'));
13 14 15 16
  });
}

void main() {
17
  testWidgets('Scrolling with list view changes, leaving the overscroll', (WidgetTester tester) async {
18
    final ScrollController controller = ScrollController();
19
    await tester.pumpWidget(MaterialApp(home: ListView(controller: controller, children: children(30))));
20 21 22 23 24
    final double thirty = controller.position.maxScrollExtent;
    controller.jumpTo(thirty);
    await tester.pump();
    controller.jumpTo(thirty + 100.0); // past the end
    await tester.pump();
25
    await tester.pumpWidget(MaterialApp(home: ListView(controller: controller, children: children(31))));
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
    expect(controller.position.pixels, thirty + 100.0); // has the same position, but no longer overscrolled
    expect(await tester.pumpAndSettle(), 1); // doesn't have ballistic animation...
    expect(controller.position.pixels, thirty + 100.0); // and ends up at the end
  });

  testWidgets('Scrolling with list view changes, remaining overscrolled', (WidgetTester tester) async {
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(MaterialApp(home: ListView(controller: controller, children: children(30))));
    final double thirty = controller.position.maxScrollExtent;
    controller.jumpTo(thirty);
    await tester.pump();
    controller.jumpTo(thirty + 200.0); // past the end
    await tester.pump();
    await tester.pumpWidget(MaterialApp(home: ListView(controller: controller, children: children(31))));
    expect(controller.position.pixels, thirty + 200.0); // has the same position, still overscrolled
41
    expect(await tester.pumpAndSettle(), 8); // now it goes ballistic...
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    expect(controller.position.pixels, thirty + 100.0); // and ends up at the end
  });

  testWidgets('Ability to keep a PageView at the end manually (issue 62209)', (WidgetTester tester) async {
    await tester.pumpWidget(const MaterialApp(home: PageView62209()));
    expect(find.text('Page 1'), findsOneWidget);
    expect(find.text('Page 100'), findsNothing);
    await tester.drag(find.byType(PageView62209), const Offset(-800.0, 0.0));
    await tester.pump();
    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 2'), findsOneWidget);
    expect(find.text('Page 100'), findsNothing);
    await tester.drag(find.byType(PageView62209), const Offset(-800.0, 0.0));
    await tester.pump();
    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 3'), findsOneWidget);
    expect(find.text('Page 100'), findsNothing);
    await tester.drag(find.byType(PageView62209), const Offset(-800.0, 0.0));
    await tester.pump();
    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 4'), findsOneWidget);
    expect(find.text('Page 100'), findsNothing);
    await tester.drag(find.byType(PageView62209), const Offset(-800.0, 0.0));
    await tester.pump();
    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 5'), findsOneWidget);
    expect(find.text('Page 100'), findsNothing);
    await tester.drag(find.byType(PageView62209), const Offset(-800.0, 0.0));
    await tester.pump();
    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 5'), findsNothing);
    expect(find.text('Page 100'), findsOneWidget);
74
    await tester.tap(find.byType(TextButton)); // 6
75 76 77 78 79
    await tester.pump();
    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 6'), findsNothing);
    expect(find.text('Page 5'), findsNothing);
    expect(find.text('Page 100'), findsOneWidget);
80
    await tester.tap(find.byType(TextButton)); // 7
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    await tester.pump();
    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 6'), findsNothing);
    expect(find.text('Page 7'), findsNothing);
    expect(find.text('Page 5'), findsNothing);
    expect(find.text('Page 100'), findsOneWidget);
    await tester.drag(find.byType(PageView62209), const Offset(800.0, 0.0));
    await tester.pump();
    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 5'), findsOneWidget);
    expect(find.text('Page 100'), findsNothing);
    await tester.drag(find.byType(PageView62209), const Offset(800.0, 0.0));
    await tester.pump();
    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 4'), findsOneWidget);
    expect(find.text('Page 5'), findsNothing);
    expect(find.text('Page 100'), findsNothing);
98
    await tester.tap(find.byType(TextButton)); // 8
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    await tester.pump();
    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 8'), findsNothing);
    expect(find.text('Page 4'), findsOneWidget);
    expect(find.text('Page 5'), findsNothing);
    expect(find.text('Page 100'), findsNothing);
    await tester.drag(find.byType(PageView62209), const Offset(800.0, 0.0));
    await tester.pump();
    expect(find.text('Page 3'), findsOneWidget);
    await tester.drag(find.byType(PageView62209), const Offset(800.0, 0.0));
    await tester.pump();
    expect(find.text('Page 2'), findsOneWidget);
    await tester.drag(find.byType(PageView62209), const Offset(800.0, 0.0));
    await tester.pump();
    expect(find.text('Page 6'), findsOneWidget);
    await tester.drag(find.byType(PageView62209), const Offset(800.0, 0.0));
    await tester.pump();
    expect(find.text('Page 7'), findsOneWidget);
    await tester.drag(find.byType(PageView62209), const Offset(800.0, 0.0));
    await tester.pump();
    expect(find.text('Page 8'), findsOneWidget);
    await tester.drag(find.byType(PageView62209), const Offset(800.0, 0.0));
    await tester.pump();
    expect(find.text('Page 1'), findsOneWidget);
123
    await tester.tap(find.byType(TextButton)); // 9
124 125 126 127 128 129 130
    await tester.pump();
    expect(find.text('Page 1'), findsOneWidget);
    expect(find.text('Page 9'), findsNothing);
    await tester.drag(find.byType(PageView62209), const Offset(-800.0, 0.0));
    await tester.pump();
    expect(find.text('Page 9'), findsOneWidget);
  });
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

  testWidgets('Pointer is not ignored during trackpad scrolling.', (WidgetTester tester) async {
    final ScrollController controller = ScrollController();
    int? lastTapped;
    int? lastHovered;
    await tester.pumpWidget(MaterialApp(
      home: ListView(
        controller: controller,
        children: List<Widget>.generate(30, (int i) {
          return SizedBox(height: 100.0, child: MouseRegion(
            onHover: (PointerHoverEvent event) {
              lastHovered = i;
            },
            child: GestureDetector(
              onTap: () {
                lastTapped = i;
              },
              child: Text('$i')
            )
          ));
        })
      )
    ));
    final TestGesture touchGesture = await tester.createGesture(kind: PointerDeviceKind.touch); // ignore: avoid_redundant_argument_values
    // Try mouse hovering while scrolling by touch
    await touchGesture.down(tester.getCenter(find.byType(ListView)));
    await tester.pump();
    await touchGesture.moveBy(const Offset(0, 200));
    await tester.pump();
    final TestGesture hoverGesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
    await hoverGesture.addPointer(
      location: tester.getCenter(find.text('3'))
    );
    await hoverGesture.moveBy(const Offset(1, 1));
    await hoverGesture.removePointer(
      location: tester.getCenter(find.text('3'))
    );
    await tester.pumpAndSettle();
    expect(controller.position.activity?.shouldIgnorePointer, isTrue); // Pointer is ignored for touch scrolling.
    expect(lastHovered, isNull);
    await touchGesture.up();
    await tester.pump();
    // Try mouse clicking during inertia after scrolling by touch
    await tester.fling(find.byType(ListView), const Offset(0, -200), 1000);
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 100));
    expect(controller.position.activity?.shouldIgnorePointer, isTrue); // Pointer is ignored following touch scrolling.
    await tester.tap(find.text('3'), warnIfMissed: false);
    expect(lastTapped, isNull);
    await tester.pumpAndSettle();

    controller.jumpTo(0);
    await tester.pump();
    final TestGesture trackpadGesture = await tester.createGesture(kind: PointerDeviceKind.trackpad);
    // Try mouse hovering while scrolling with a trackpad
    await trackpadGesture.panZoomStart(tester.getCenter(find.byType(ListView)));
    await tester.pump();
    await trackpadGesture.panZoomUpdate(tester.getCenter(find.byType(ListView)), pan: const Offset(0, 200));
    await tester.pump();
    await hoverGesture.addPointer(
      location: tester.getCenter(find.text('3'))
    );
    await hoverGesture.moveBy(const Offset(1, 1));
    await hoverGesture.removePointer(
      location: tester.getCenter(find.text('3'))
    );
    await tester.pumpAndSettle();
    expect(controller.position.activity?.shouldIgnorePointer, isFalse); // Pointer is not ignored for trackpad scrolling.
    expect(lastHovered, equals(3));
    await trackpadGesture.panZoomEnd();
    await tester.pump();
    // Try mouse clicking during inertia after scrolling with a trackpad
    await tester.trackpadFling(find.byType(ListView), const Offset(0, -200), 1000);
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 100));
    expect(controller.position.activity?.shouldIgnorePointer, isFalse); // Pointer is not ignored following trackpad scrolling.
    await tester.tap(find.text('3'));
    expect(lastTapped, equals(3));
    await tester.pumpAndSettle();
  });
211 212 213
}

class PageView62209 extends StatefulWidget {
214
  const PageView62209({super.key});
215 216

  @override
217
  State<PageView62209> createState() => _PageView62209State();
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
}

class _PageView62209State extends State<PageView62209> {
  int _nextPageNum = 1;
  final List<Carousel62209Page> _pages = <Carousel62209Page>[];

  @override
  void initState() {
    super.initState();
    for (int i = 0; i < 5; i++) {
      _pages.add(Carousel62209Page(
        key: Key('$_nextPageNum'),
        number: _nextPageNum++,
      ));
    }
    _pages.add(const Carousel62209Page(number: 100));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: <Widget>[
          Expanded(child: Carousel62209(pages: _pages)),
242
          TextButton(
243 244 245 246 247 248 249 250 251 252 253 254
            child: const Text('ADD PAGE'),
            onPressed: () {
              setState(() {
                _pages.insert(
                  1,
                  Carousel62209Page(
                    key: Key('$_nextPageNum'),
                    number: _nextPageNum++,
                  ),
                );
              });
            },
255
          ),
256 257 258 259 260 261 262
        ],
      ),
    );
  }
}

class Carousel62209Page extends StatelessWidget {
263
  const Carousel62209Page({required this.number, super.key});
264 265 266 267 268 269 270 271 272 273

  final int number;

  @override
  Widget build(BuildContext context) {
    return Center(child: Text('Page $number'));
  }
}

class Carousel62209 extends StatefulWidget {
274
  const Carousel62209({super.key, required this.pages});
275 276 277 278

  final List<Carousel62209Page> pages;

  @override
279
  State<Carousel62209> createState() => _Carousel62209State();
280 281 282 283
}

class _Carousel62209State extends State<Carousel62209> {
  // page variables
284
  late PageController _pageController;
285 286 287
  int _currentPage = 0;

  // controls updates outside of user interaction
288
  late List<Carousel62209Page> _pages;
289 290 291 292 293 294
  bool _jumpingToPage = false;

  @override
  void initState() {
    super.initState();
    _pages = widget.pages.toList();
295
    _pageController = PageController(keepPage: false);
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
  }

  @override
  void didUpdateWidget(Carousel62209 oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (!_jumpingToPage) {
      int newPage = -1;
      for (int i = 0; i < widget.pages.length; i++) {
        if (widget.pages[i].number == _pages[_currentPage].number) {
          newPage = i;
        }
      }
      if (newPage == _currentPage) {
        _pages = widget.pages.toList();
      } else {
        _jumpingToPage = true;
312
        SchedulerBinding.instance.addPostFrameCallback((_) {
313 314 315 316 317
          if (mounted) {
            setState(() {
              _pages = widget.pages.toList();
              _currentPage = newPage;
              _pageController.jumpToPage(_currentPage);
318
              SchedulerBinding.instance.addPostFrameCallback((_) {
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
                _jumpingToPage = false;
              });
            });
          }
        });
      }
    }
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  bool _handleScrollNotification(ScrollNotification notification) {
    if (notification is ScrollUpdateNotification) {
336
      final int page = _pageController.page!.round();
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
      if (!_jumpingToPage && _currentPage != page) {
        _currentPage = page;
      }
    }
    return true;
  }

  @override
  Widget build(BuildContext context) {
    return NotificationListener<ScrollNotification>(
      onNotification: _handleScrollNotification,
      child: PageView.builder(
        controller: _pageController,
        itemCount: _pages.length,
        itemBuilder: (BuildContext context, int index) {
          return _pages[index];
        },
      ),
    );
  }
}