layout_builder_and_global_keys_test.dart 1.57 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart' hide TypeMatcher;
import 'package:flutter/widgets.dart';

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

  final Widget child;

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

class StatefulWrapper extends StatefulWidget {
21
  const StatefulWrapper({
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    Key key,
    this.child,
  }) : super(key: key);

  final Widget child;

  @override
  StatefulWrapperState createState() => new StatefulWrapperState();
}

class StatefulWrapperState extends State<StatefulWrapper> {

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

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

void main() {
  testWidgets('Moving global key inside a LayoutBuilder', (WidgetTester tester) async {
44
    final GlobalKey<StatefulWrapperState> key = new GlobalKey<StatefulWrapperState>();
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    await tester.pumpWidget(
      new LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
        return new Wrapper(
          child: new StatefulWrapper(key: key, child: new Container(height: 100.0)),
        );
      }),
    );
    await tester.pumpWidget(
      new LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
        key.currentState.trigger();
        return new StatefulWrapper(key: key, child: new Container(height: 100.0));
      }),
    );
  });
}