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

5
import 'package:flutter/src/rendering/sliver.dart';
6
import 'package:flutter/widgets.dart';
7
import 'package:flutter_test/flutter_test.dart';
8 9

class Wrapper extends StatelessWidget {
10
  const Wrapper({
11
    super.key,
12
    required this.child,
13
  });
14 15 16 17 18 19 20 21

  final Widget child;

  @override
  Widget build(BuildContext context) => child;
}

class StatefulWrapper extends StatefulWidget {
22
  const StatefulWrapper({
23
    super.key,
24
    required this.child,
25
  });
26 27 28 29

  final Widget child;

  @override
30
  StatefulWrapperState createState() => StatefulWrapperState();
31 32 33 34 35 36 37 38 39
}

class StatefulWrapperState extends State<StatefulWrapper> {

  void trigger() {
    setState(() { /* for test purposes */ });
  }

  @override
40
  Widget build(BuildContext context) => widget.child;
41 42 43 44
}

void main() {
  testWidgets('Moving global key inside a LayoutBuilder', (WidgetTester tester) async {
45
    final GlobalKey<StatefulWrapperState> key = GlobalKey<StatefulWrapperState>();
46
    await tester.pumpWidget(
47 48 49
      LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
        return Wrapper(
          child: StatefulWrapper(key: key, child: Container(height: 100.0)),
50 51 52 53
        );
      }),
    );
    await tester.pumpWidget(
54
      LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
55
        key.currentState!.trigger();
56
        return StatefulWrapper(key: key, child: Container(height: 100.0));
57 58
      }),
    );
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

    expect(tester.takeException(), null);
  });

  testWidgets('Moving global key inside a SliverLayoutBuilder', (WidgetTester tester) async {
    final GlobalKey<StatefulWrapperState> key = GlobalKey<StatefulWrapperState>();

    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: CustomScrollView(
          slivers: <Widget>[
            SliverLayoutBuilder(
              builder: (BuildContext context, SliverConstraints constraint) {
                return SliverToBoxAdapter(
                  child: Wrapper(child: StatefulWrapper(key: key, child: Container(height: 100.0))),
                );
              },
            ),
          ],
        ),
      ),
    );

    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: CustomScrollView(
          slivers: <Widget>[
            SliverLayoutBuilder(
              builder: (BuildContext context, SliverConstraints constraint) {
90
                key.currentState!.trigger();
91 92 93 94 95 96 97 98 99 100 101
                return SliverToBoxAdapter(
                  child: StatefulWrapper(key: key, child: Container(height: 100.0)),
                );
              },
            ),
          ],
        ),
      ),
    );

    expect(tester.takeException(), null);
102 103
  });
}