grid_view_layout_test.dart 2.03 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2015 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/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('Empty GridView', (WidgetTester tester) async {
10
    final List<Widget> children = <Widget>[
11 12 13 14
      const DecoratedBox(decoration: const BoxDecoration()),
      const DecoratedBox(decoration: const BoxDecoration()),
      const DecoratedBox(decoration: const BoxDecoration()),
      const DecoratedBox(decoration: const BoxDecoration()),
15 16 17 18 19 20 21 22 23 24 25 26 27
    ];

    await tester.pumpWidget(new Center(
      child: new Container(
        width: 200.0,
        child: new GridView.extent(
          maxCrossAxisExtent: 100.0,
          shrinkWrap: true,
          children: children,
        ),
      ),
    ));

28 29 30
    expect(tester.renderObjectList<RenderBox>(find.byType(DecoratedBox)), hasLength(4));

    for (RenderBox box in tester.renderObjectList<RenderBox>(find.byType(DecoratedBox))) {
31 32
      expect(box.size.width, equals(100.0), reason: "child width");
      expect(box.size.height, equals(100.0), reason: "child height");
33
    }
34

35
    final RenderBox grid = tester.renderObject(find.byType(GridView));
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    expect(grid.size.width, equals(200.0), reason: "grid width");
    expect(grid.size.height, equals(200.0), reason: "grid height");

    expect(grid.debugNeedsLayout, false);

    await tester.pumpWidget(new Center(
      child: new Container(
        width: 200.0,
        child: new GridView.extent(
          maxCrossAxisExtent: 60.0,
          shrinkWrap: true,
          children: children,
        ),
      ),
    ));

52
    for (RenderBox box in tester.renderObjectList<RenderBox>(find.byType(DecoratedBox))) {
53 54
      expect(box.size.width, equals(50.0), reason: "child width");
      expect(box.size.height, equals(50.0), reason: "child height");
55
    }
56 57 58 59 60

    expect(grid.size.width, equals(200.0), reason: "grid width");
    expect(grid.size.height, equals(50.0), reason: "grid height");
  });
}