tabbed_scrollview_warp_test.dart 2.48 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
6
import 'package:flutter_test/flutter_test.dart';
7
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
8

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// This is a regression test for https://github.com/flutter/flutter/issues/10549
// which was failing because _SliverPersistentHeaderElement.visitChildren()
// didn't check child != null before visiting its child.

class MySliverPersistentHeaderDelegate extends SliverPersistentHeaderDelegate {
  @override
  double get minExtent => 50.0;

  @override
  double get maxExtent => 150.0;

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) => const Placeholder(color: Colors.teal);

  @override
  bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) => false;
}

class MyHomePage extends StatefulWidget {
28
  const MyHomePage({ super.key });
29

30
  @override
31
  State<MyHomePage> createState() => _MyHomePageState();
32 33 34 35
}

class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
  static const int tabCount = 3;
36
  late TabController tabController;
37 38 39 40

  @override
  void initState() {
    super.initState();
41
    tabController = TabController(length: tabCount, vsync: this);
42 43 44 45 46 47 48 49 50 51
  }

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

  @override
  Widget build(BuildContext context) {
52 53 54
    return Scaffold(
      appBar: AppBar(
        bottom: TabBar(
55
          controller: tabController,
56
          tabs: List<Widget>.generate(tabCount, (int index) => Tab(text: 'Tab $index')).toList(),
57 58
        ),
      ),
59
      body: TabBarView(
60
        controller: tabController,
61 62
        children: List<Widget>.generate(tabCount, (int index) {
          return CustomScrollView(
63
            // The bug only occurs when this key is included
64
            key: ValueKey<String>('Page $index'),
65
            slivers: <Widget>[
66 67
              SliverPersistentHeader(
                delegate: MySliverPersistentHeaderDelegate(),
68 69 70 71 72 73 74 75 76 77
              ),
            ],
          );
        }).toList(),
      ),
    );
  }
}

void main() {
78
  testWidgetsWithLeakTracking('Tabbed CustomScrollViews, warp from tab 1 to 3', (WidgetTester tester) async {
79
    await tester.pumpWidget(const MaterialApp(home: MyHomePage()));
80 81 82 83

    // should not crash.
    await tester.tap(find.text('Tab 2'));
    await tester.pumpAndSettle();
84
  });
85
}