blob: e6ad3300ae8f9346e9adf8cf5ebdb8b596c668b5 [file] [log] [blame]
// Copyright 2013 The Flutter 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:dynamic_layouts/dynamic_layouts.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('DynamicGridView works with simple layout',
(WidgetTester tester) async {
// Can have no children
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: DynamicGridView(
gridDelegate: TestDelegate(crossAxisCount: 2),
),
),
),
);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: DynamicGridView(
gridDelegate: TestDelegate(crossAxisCount: 2),
children: List<Widget>.generate(
50,
(int index) => SizedBox.square(
dimension: TestSimpleLayout.childExtent,
child: Text('Index $index'),
),
),
),
),
),
);
// Only the visible tiles have ben laid out.
expect(find.text('Index 0'), findsOneWidget);
expect(tester.getTopLeft(find.text('Index 0')), Offset.zero);
expect(find.text('Index 1'), findsOneWidget);
expect(tester.getTopLeft(find.text('Index 1')), const Offset(50.0, 0.0));
expect(find.text('Index 2'), findsOneWidget);
expect(tester.getTopLeft(find.text('Index 2')), const Offset(0.0, 50.0));
expect(find.text('Index 47'), findsNothing);
expect(find.text('Index 48'), findsNothing);
expect(find.text('Index 49'), findsNothing);
});
testWidgets('DynamicGridView.builder works with simple layout',
(WidgetTester tester) async {
// Only a few number of tiles
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: DynamicGridView.builder(
gridDelegate: TestDelegate(crossAxisCount: 2),
itemCount: 3,
itemBuilder: (BuildContext context, int index) {
return SizedBox.square(
dimension: TestSimpleLayout.childExtent,
child: Text('Index $index'),
);
},
),
),
),
);
// Only the visible tiles have ben laid out, up to itemCount.
expect(find.text('Index 0'), findsOneWidget);
expect(tester.getTopLeft(find.text('Index 0')), Offset.zero);
expect(find.text('Index 1'), findsOneWidget);
expect(tester.getTopLeft(find.text('Index 1')), const Offset(50.0, 0.0));
expect(find.text('Index 2'), findsOneWidget);
expect(tester.getTopLeft(find.text('Index 2')), const Offset(0.0, 50.0));
expect(find.text('Index 3'), findsNothing);
expect(find.text('Index 4'), findsNothing);
expect(find.text('Index 5'), findsNothing);
// Infinite number of tiles
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: DynamicGridView.builder(
gridDelegate: TestDelegate(crossAxisCount: 2),
itemBuilder: (BuildContext context, int index) {
return SizedBox.square(
dimension: TestSimpleLayout.childExtent,
child: Text('Index $index'),
);
},
),
),
),
);
// Only the visible tiles have ben laid out.
expect(find.text('Index 0'), findsOneWidget);
expect(tester.getTopLeft(find.text('Index 0')), Offset.zero);
expect(find.text('Index 1'), findsOneWidget);
expect(tester.getTopLeft(find.text('Index 1')), const Offset(50.0, 0.0));
expect(find.text('Index 2'), findsOneWidget);
expect(tester.getTopLeft(find.text('Index 2')), const Offset(0.0, 50.0));
expect(find.text('Index 47'), findsNothing);
expect(find.text('Index 48'), findsNothing);
expect(find.text('Index 49'), findsNothing);
});
}
class TestSimpleLayout extends DynamicSliverGridLayout {
TestSimpleLayout({
required this.crossAxisCount,
});
final int crossAxisCount;
static const double childExtent = 50.0;
@override
SliverGridGeometry getGeometryForChildIndex(int index) {
final double crossAxisStart = (index % crossAxisCount) * childExtent;
return SliverGridGeometry(
scrollOffset: (index ~/ crossAxisCount) * childExtent,
crossAxisOffset: crossAxisStart,
mainAxisExtent: childExtent,
crossAxisExtent: childExtent,
);
}
@override
bool reachedTargetScrollOffset(double targetOffset) => true;
@override
SliverGridGeometry updateGeometryForChildIndex(int index, Size childSize) {
return getGeometryForChildIndex(index);
}
}
class TestDelegate extends SliverGridDelegateWithFixedCrossAxisCount {
TestDelegate({required super.crossAxisCount});
@override
SliverGridLayout getLayout(SliverConstraints constraints) {
return TestSimpleLayout(crossAxisCount: crossAxisCount);
}
}