Add OverflowBox example and update existing examples under basic.dart (#121213)

Add `OverflowBox` example and update existing examples under `basic.dart`
This commit is contained in:
Taha Tesser
2023-02-27 20:52:00 +02:00
committed by GitHub
parent 5c334d10df
commit 9a4e897965
35 changed files with 828 additions and 304 deletions

View File

@@ -0,0 +1,29 @@
// Copyright 2014 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:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_api_samples/widgets/basic/absorb_pointer.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('AbsorbPointer prevents hit testing on its child', (WidgetTester tester) async {
await tester.pumpWidget(
const example.AbsorbPointerApp(),
);
// Get the center of the stack.
final Offset center = tester.getCenter(find.byType(Stack).first);
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse, pointer: 1);
// Add the point to the center of the stack where the AbsorbPointer is.
await gesture.addPointer(location: center);
expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic);
// Move the pointer to the left of the stack where the AbsorbPointer is not.
await gesture.moveTo(center + const Offset(-100, 0));
expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.click);
});
}

View File

@@ -0,0 +1,25 @@
// Copyright 2014 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:flutter/material.dart';
import 'package:flutter_api_samples/widgets/basic/aspect_ratio.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('AspectRatio applies 16 / 9 aspect ratio on its child', (WidgetTester tester) async {
const double height = 100.0;
await tester.pumpWidget(
const example.AspectRatioApp(),
);
final Size parentContainer = tester.getSize(find.byType(Container).first);
expect(parentContainer.width, 800.0);
expect(parentContainer.height, height);
final Size childContainer = tester.getSize(find.byType(Container).last);
expect(childContainer.width, height * 16 / 9);
expect(childContainer.height, height);
});
}

View File

@@ -0,0 +1,23 @@
// Copyright 2014 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:flutter/material.dart';
import 'package:flutter_api_samples/widgets/basic/aspect_ratio.1.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('AspectRatio applies 2.0 aspect ratio on its child', (WidgetTester tester) async {
const Size containerSize = Size(100, 100);
await tester.pumpWidget(
const example.AspectRatioApp(),
);
final Size parentContainer = tester.getSize(find.byType(Container).first);
expect(parentContainer, containerSize);
final Size childContainer = tester.getSize(find.byType(Container).last);
expect(childContainer, Size(containerSize.height, containerSize.height / 2.0));
});
}

View File

@@ -0,0 +1,23 @@
// Copyright 2014 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:flutter/material.dart';
import 'package:flutter_api_samples/widgets/basic/aspect_ratio.2.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('AspectRatio applies 0.5 aspect ratio on its child', (WidgetTester tester) async {
const Size containerSize = Size(100, 100);
await tester.pumpWidget(
const example.AspectRatioApp(),
);
final Size parentContainer = tester.getSize(find.byType(Container).first);
expect(parentContainer, containerSize);
final Size childContainer = tester.getSize(find.byType(Container).last);
expect(childContainer, Size(containerSize.height * 0.5, containerSize.height));
});
}

View File

@@ -0,0 +1,37 @@
// Copyright 2014 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:flutter/material.dart';
import 'package:flutter_api_samples/widgets/basic/expanded.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Expanded widget in a Column', (WidgetTester tester) async {
const double totalHeight = 600;
const double appBarHeight = 56.0;
const double columnWidth = 100.0;
const double columnHeight = totalHeight - appBarHeight;
const double containerOneHeight = 100;
const double containerTwoHeight = columnHeight - 200;
const double containerThreeHeight = 100;
await tester.pumpWidget(
const example.ExpandedApp(),
);
final Size column = tester.getSize(find.byType(Column));
expect(column, const Size(columnWidth, columnHeight));
final Size containerOne = tester.getSize(find.byType(Container).at(0));
expect(containerOne, const Size(columnWidth, containerOneHeight));
// This Container is wrapped in an Expanded widget, so it should take up
// the remaining space in the Column.
final Size containerTwo = tester.getSize(find.byType(Container).at(1));
expect(containerTwo, const Size(columnWidth, containerTwoHeight));
final Size containerThree = tester.getSize(find.byType(Container).at(2));
expect(containerThree, const Size(columnWidth, containerThreeHeight));
});
}

View File

@@ -0,0 +1,37 @@
// Copyright 2014 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:flutter/material.dart';
import 'package:flutter_api_samples/widgets/basic/expanded.1.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Expanded widgets in a Row', (WidgetTester tester) async {
const double rowWidth = 800.0;
const double rowHeight = 100.0;
const double containerOneWidth = (rowWidth - 50) * 2 / 3;
const double containerTwoWidth = 50;
const double containerThreeWidth = (rowWidth - 50) * 1 / 3;
await tester.pumpWidget(
const example.ExpandedApp(),
);
final Size row = tester.getSize(find.byType(Row));
expect(row, const Size(rowWidth, rowHeight));
// This container is wrapped in an Expanded widget, so it should take up
// two thirds of the remaining space in the Row.
final Size containerOne = tester.getSize(find.byType(Container).at(0));
expect(containerOne, const Size(containerOneWidth, rowHeight));
final Size containerTwo = tester.getSize(find.byType(Container).at(1));
expect(containerTwo, const Size(containerTwoWidth, rowHeight));
// This container is wrapped in an Expanded widget, so it should take up
// one third of the remaining space in the Row.
final Size containerThree = tester.getSize(find.byType(Container).at(2));
expect(containerThree, const Size(containerThreeWidth, rowHeight));
});
}

View File

@@ -0,0 +1,28 @@
// Copyright 2014 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 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_api_samples/widgets/basic/fitted_box.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
setUpAll(() {
HttpOverrides.global = null;
});
testWidgets('FittedBox scales the image to fill the parent container', (WidgetTester tester) async {
await tester.pumpWidget(
const example.FittedBoxApp(),
);
final Size containerSize = tester.getSize(find.byType(Container));
expect(containerSize, const Size(300, 400));
// FittedBox should scale the image to fill the parent container.
final FittedBox fittedBox = tester.widget(find.byType(FittedBox));
expect(fittedBox.fit, BoxFit.fill);
});
}

View File

@@ -0,0 +1,35 @@
// Copyright 2014 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:flutter/material.dart';
import 'package:flutter_api_samples/widgets/basic/flow.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Clicking on the menu icon opens the Flow menu', (WidgetTester tester) async {
await tester.pumpWidget(
const example.FlowApp(),
);
// The menu icon is in the top left corner of the screen.
Offset menuIcon = tester.getCenter(find.byIcon(Icons.menu));
expect(menuIcon, const Offset(80.0, 144.0));
// The home icon is also in the top left corner of the screen.
Offset homeIcon = tester.getCenter(find.byIcon(Icons.home));
expect(homeIcon, const Offset(80.0, 144.0));
// Tap the menu icon to open the flow menu.
await tester.tapAt(menuIcon);
await tester.pumpAndSettle();
// The home icon is still in the top left corner of the screen.
homeIcon = tester.getCenter(find.byIcon(Icons.home));
expect(homeIcon, const Offset(80.0, 144.0));
// The menu icon is now in the top right corner of the screen.
menuIcon = tester.getCenter(find.byIcon(Icons.menu));
expect(menuIcon, const Offset(720.0, 144.0));
});
}

View File

@@ -0,0 +1,27 @@
// Copyright 2014 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:flutter/material.dart';
import 'package:flutter_api_samples/widgets/basic/fractionally_sized_box.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('FractionallySizedBox sizes DecoratedBox', (WidgetTester tester) async {
const double appBarHeight = 56.0;
const double widthFactor = 0.5;
const double heightFactor = 0.5;
await tester.pumpWidget(
const example.FractionallySizedBoxApp(),
);
final FractionallySizedBox fractionallySizedBox = tester.widget(find.byType(FractionallySizedBox));
expect(fractionallySizedBox.widthFactor, widthFactor);
expect(fractionallySizedBox.heightFactor, heightFactor);
final Size boxSize = tester.getSize(find.byType(DecoratedBox));
expect(boxSize.width, 800 * widthFactor);
expect(boxSize.height, (600 - appBarHeight) * heightFactor);
});
}

View File

@@ -0,0 +1,33 @@
// Copyright 2014 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:flutter/gestures.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_api_samples/widgets/basic/ignore_pointer.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('IgnorePointer ignores pointer on the ElevatedButton', (WidgetTester tester) async {
const String clickButtonText = 'Click me!';
await tester.pumpWidget(
const example.IgnorePointerApp(),
);
// The ElevatedButton is clickable.
expect(find.text('Ignoring: false'), findsOneWidget);
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse, pointer: 1);
await gesture.addPointer(location: tester.getCenter(find.text(clickButtonText)));
// On hovering the ElevatedButton, the cursor should be SystemMouseCursors.click.
expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.click);
// Tap to set ignoring pointer to true.
await tester.tap(find.text('Set ignoring to true'));
await tester.pump();
// The ElevatedButton is not clickable so the cursor should be SystemMouseCursors.basic.
expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic);
});
}

View File

@@ -8,7 +8,7 @@ import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('has correct forward rendering mechanism', (WidgetTester tester) async {
await tester.pumpWidget(const example.MyApp());
await tester.pumpWidget(const example.IndexedStackApp());
final Finder gesture2 = find.byKey(const Key('gesture2'));
final Element containerFinder = find.byKey(const Key('Dash')).evaluate().first;
@@ -31,7 +31,7 @@ void main() {
expect(containerFinder2.renderObject!.debugNeedsPaint, false);
});
testWidgets('has correct backward rendering mechanism', (WidgetTester tester) async {
await tester.pumpWidget(const example.MyApp());
await tester.pumpWidget(const example.IndexedStackApp());
final Finder gesture1 = find.byKey(const Key('gesture1'));
final Element containerFinder = find.byKey(const Key('Dash')).evaluate().first;
@@ -51,7 +51,7 @@ void main() {
expect(containerFinder2.renderObject!.debugNeedsPaint, false);
});
testWidgets('has correct element addition handling', (WidgetTester tester) async {
await tester.pumpWidget(const example.MyApp());
await tester.pumpWidget(const example.IndexedStackApp());
expect(find.byType(example.PersonTracker), findsNWidgets(3));
final Finder textField = find.byType(TextField);
@@ -66,7 +66,7 @@ void main() {
expect(find.byType(example.PersonTracker), findsNWidgets(5));
});
testWidgets('has state preservation', (WidgetTester tester) async {
await tester.pumpWidget(const example.MyApp());
await tester.pumpWidget(const example.IndexedStackApp());
final Finder gesture1 = find.byKey(const Key('gesture1'));
final Finder gesture2 = find.byKey(const Key('gesture2'));

View File

@@ -0,0 +1,34 @@
// Copyright 2014 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:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_api_samples/widgets/basic/listener.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Listener detects press & release, and cursor location', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(home: example.ListenerExample()),
);
expect(find.text('0 presses\n0 releases'), findsOneWidget);
expect(find.text('The cursor is here: (0.00, 0.00)'), findsOneWidget);
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
await gesture.addPointer();
await gesture.down(tester.getCenter(find.byType(ColoredBox)));
await tester.pump();
expect(find.text('1 presses\n0 releases'), findsOneWidget);
expect(find.text('The cursor is here: (400.00, 300.00)'), findsOneWidget);
await gesture.up();
await tester.pump();
expect(find.text('1 presses\n1 releases'), findsOneWidget);
expect(find.text('The cursor is here: (400.00, 300.00)'), findsOneWidget);
});
}

View File

@@ -0,0 +1,39 @@
// Copyright 2014 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:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_api_samples/widgets/basic/mouse_region.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('MouseRegion detects mouse entries, exists, and location', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(home: example.MouseRegionApp()),
);
expect(find.text('0 Entries\n0 Exits'), findsOneWidget);
expect(find.text('The cursor is here: (0.00, 0.00)'), findsOneWidget);
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
await gesture.addPointer();
await gesture.moveTo(tester.getCenter(find.byType(ColoredBox)));
await tester.pump();
expect(find.text('1 Entries\n0 Exits'), findsOneWidget);
expect(find.text('The cursor is here: (400.00, 328.00)'), findsOneWidget);
await gesture.moveTo(
tester.getCenter(find.byType(ColoredBox)) + const Offset(50.0, 30.0),
);
await tester.pump();
expect(find.text('The cursor is here: (450.00, 358.00)'), findsOneWidget);
await gesture.moveTo(Offset.zero);
await tester.pump();
expect(find.text('1 Entries\n1 Exits'), findsOneWidget);
});
}

View File

@@ -0,0 +1,27 @@
// Copyright 2014 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:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_api_samples/widgets/basic/mouse_region.on_exit.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('MouseRegion detects mouse hover', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(home: example.MouseRegionApp()),
);
Container container = tester.widget<Container>(find.byType(Container));
expect(container.decoration, const BoxDecoration(color: Colors.blue));
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
await gesture.addPointer();
await gesture.moveTo(tester.getCenter(find.byType(Container)));
await tester.pump();
container = tester.widget<Container>(find.byType(Container));
expect(container.decoration, const BoxDecoration(color: Colors.yellow));
});
}

View File

@@ -0,0 +1,30 @@
// Copyright 2014 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:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_api_samples/widgets/basic/mouse_region.on_exit.1.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('MouseRegion update mouse hover with a delay', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(home: example.MouseRegionApp()),
);
expect(find.text('Not hovering'), findsOneWidget);
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
await gesture.addPointer();
await gesture.moveTo(tester.getCenter(find.byType(Container)));
await tester.pump();
expect(find.text('Hovering'), findsOneWidget);
await gesture.moveTo(Offset.zero);
await tester.pump(const Duration(seconds: 1));
expect(find.text('Not hovering'), findsOneWidget);
});
}

View File

@@ -0,0 +1,31 @@
// Copyright 2014 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:flutter/material.dart';
import 'package:flutter_api_samples/widgets/basic/offstage.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Can off/on stage Flutter logo widget', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(home: example.OffstageApp()),
);
// The Flutter logo is off stage and not visible.
expect(find.text('Flutter logo is offstage: true'), findsOneWidget);
// Tap to get the Flutter logo size.
await tester.tap(find.text('Get Flutter Logo size'));
await tester.pumpAndSettle();
expect(find.text('Flutter Logo size is Size(150.0, 150.0)'), findsOneWidget);
// Tap to toggle the offstage value.
await tester.tap(find.text('Toggle Offstage Value'));
await tester.pumpAndSettle();
// The Flutter logo is on stage and visible.
expect(find.text('Flutter logo is offstage: false'), findsOneWidget);
});
}

View File

@@ -0,0 +1,31 @@
// Copyright 2014 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:flutter/material.dart';
import 'package:flutter_api_samples/widgets/basic/overflowbox.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('OverflowBox allows child widget to overflow parent container', (WidgetTester tester) async {
const Size containerSize = Size(100, 100);
const Size maxSize = Size(200, 200);
await tester.pumpWidget(
const example.OverflowBoxApp(),
);
// The parent container has fixed width and height of 100 pixels.
expect(tester.getSize(find.byType(Container).first), containerSize);
final OverflowBox overflowBox = tester.widget(find.byType(OverflowBox));
// The OverflowBox imposes its own constraints of maxWidth and maxHeight of
// 200 on its child which allows the child to overflow the parent container.
expect(overflowBox.maxWidth, maxSize.width);
expect(overflowBox.maxHeight, maxSize.height);
// The child widget overflows the parent container.
expect(tester.getSize(find.byType(FlutterLogo)), greaterThan(containerSize));
expect(tester.getSize(find.byType(FlutterLogo)), maxSize);
});
}

View File

@@ -0,0 +1,25 @@
// Copyright 2014 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:flutter/material.dart';
import 'package:flutter_api_samples/widgets/basic/physical_shape.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('PhysicalShape is an ancestor of the text widget', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(home: example.PhysicalShapeApp()),
);
final PhysicalShape physicalShape = tester.widget<PhysicalShape>(
find.ancestor(
of:find.text('Hello, World!'),
matching: find.byType(PhysicalShape),
),
);
expect(physicalShape.clipper, isNotNull);
expect(physicalShape.color, Colors.orange);
expect(physicalShape.elevation, 5.0);
});
}