layout_builder_and_parent_data_test.dart 1.55 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/widgets.dart';
6
import 'package:flutter_test/flutter_test.dart';
7 8

class SizeChanger extends StatefulWidget {
9
  const SizeChanger({
10
    super.key,
11
    required this.child,
12
  });
13 14 15 16

  final Widget child;

  @override
17
  SizeChangerState createState() => SizeChangerState();
18 19 20 21 22 23 24 25 26 27 28 29 30
}

class SizeChangerState extends State<SizeChanger> {
  bool _flag = false;

  void trigger() {
    setState(() {
      _flag = true;
    });
  }

  @override
  Widget build(BuildContext context) {
31
    return Row(
32
      textDirection: TextDirection.ltr,
33
      children: <Widget>[
34
        SizedBox(
35 36
          height: _flag ? 50.0 : 100.0,
          width: 100.0,
37 38
          child: widget.child,
        ),
39 40 41 42 43 44
      ],
    );
  }
}

void main() {
45
  testWidgets('Applying parent data inside a LayoutBuilder', (WidgetTester tester) async {
46
    int frame = 1;
47 48
    await tester.pumpWidget(SizeChanger( // when this is triggered, the child LayoutBuilder will build again
      child: LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
49 50 51 52 53 54
        return Column(children: <Widget>[
          Expanded(
            flex: frame, // this is different after the next pump, so that the parentData has to be applied again
            child: Container(height: 100.0),
          ),
        ]);
55
      }),
56 57
    ));
    frame += 1;
58
    tester.state<SizeChangerState>(find.byType(SizeChanger)).trigger();
59 60 61
    await tester.pump();
  });
}