global_keys_moving_test.dart 1.73 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Hixie's avatar
Hixie committed
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/widgets.dart';
6
import 'package:flutter_test/flutter_test.dart';
Adam Barth's avatar
Adam Barth committed
7

Hixie's avatar
Hixie committed
8
class Item {
9 10
  GlobalKey key1 = GlobalKey();
  GlobalKey key2 = GlobalKey();
11 12

  @override
Ian Hickson's avatar
Ian Hickson committed
13
  String toString() => 'Item($key1, $key2)';
Hixie's avatar
Hixie committed
14
}
15
List<Item> items = <Item>[Item(), Item()];
Hixie's avatar
Hixie committed
16

17
class StatefulLeaf extends StatefulWidget {
18
  const StatefulLeaf({ GlobalKey? key }) : super(key: key);
19 20

  @override
21
  StatefulLeafState createState() => StatefulLeafState();
Adam Barth's avatar
Adam Barth committed
22 23 24
}

class StatefulLeafState extends State<StatefulLeaf> {
25
  void markNeedsBuild() { setState(() { }); }
26 27

  @override
Ian Hickson's avatar
Ian Hickson committed
28
  Widget build(BuildContext context) => const Text('leaf', textDirection: TextDirection.ltr);
Hixie's avatar
Hixie committed
29 30
}

31
class KeyedWrapper extends StatelessWidget {
32
  const KeyedWrapper(this.key1, this.key2, { super.key });
33

34
  final Key key1;
35
  final GlobalKey key2;
36 37

  @override
Adam Barth's avatar
Adam Barth committed
38
  Widget build(BuildContext context) {
39
    return Container(
Hixie's avatar
Hixie committed
40
      key: key1,
41
      child: StatefulLeaf(
42
        key: key2,
43
      ),
Hixie's avatar
Hixie committed
44 45 46 47 48
    );
  }
}

Widget builder() {
49
  return Column(
50
    children: <Widget>[
51 52
      KeyedWrapper(items[1].key1, items[1].key2),
      KeyedWrapper(items[0].key1, items[0].key2),
53
    ],
54
  );
Hixie's avatar
Hixie committed
55 56 57
}

void main() {
58
  testWidgets('moving subtrees with global keys - smoketest', (WidgetTester tester) async {
59
    await tester.pumpWidget(builder());
60
    final StatefulLeafState leaf = tester.firstState(find.byType(StatefulLeaf));
61
    leaf.markNeedsBuild();
62
    await tester.pump();
63
    final Item lastItem = items[1];
64 65
    items.remove(lastItem);
    items.insert(0, lastItem);
66
    await tester.pumpWidget(builder()); // this marks the app dirty and rebuilds it
Hixie's avatar
Hixie committed
67 68
  });
}