Adding all the different types of chips, along with their complex behaviors (#16039)
This implements various different chip types: There are now the InputChip, ChoiceChip, FilterChip, ActionChip, and the original vanilla Chip, all of which have different uses. They can be customized in a number of ways. See their docs for more information. Also fixes #16083
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -1820,9 +1820,10 @@ abstract class RenderBox extends RenderObject {
|
||||
/// absorbs the hit (preventing objects below this one from being hit).
|
||||
/// Returns false if the hit can continue to other objects below this one.
|
||||
///
|
||||
/// The caller is responsible for transforming [position] into the local
|
||||
/// coordinate space of the callee. The callee is responsible for checking
|
||||
/// whether the given position is within its bounds.
|
||||
/// The caller is responsible for transforming [position] from global
|
||||
/// coordinates to its location relative to the origin of this [RenderBox].
|
||||
/// This [RenderBox] is responsible for checking whether the given position is
|
||||
/// within its bounds.
|
||||
///
|
||||
/// Hit testing requires layout to be up-to-date but does not require painting
|
||||
/// to be up-to-date. That means a render object can rely upon [performLayout]
|
||||
@@ -1870,6 +1871,11 @@ abstract class RenderBox extends RenderObject {
|
||||
/// Override this method if this render object can be hit even if its
|
||||
/// children were not hit.
|
||||
///
|
||||
/// The caller is responsible for transforming [position] from global
|
||||
/// coordinates to its location relative to the origin of this [RenderBox].
|
||||
/// This [RenderBox] is responsible for checking whether the given position is
|
||||
/// within its bounds.
|
||||
///
|
||||
/// Used by [hitTest]. If you override [hitTest] and do not call this
|
||||
/// function, then you don't need to implement this function.
|
||||
@protected
|
||||
@@ -1882,6 +1888,11 @@ abstract class RenderBox extends RenderObject {
|
||||
/// hit tests at locations where children overlap hit the child that is
|
||||
/// visually "on top" (i.e., paints later).
|
||||
///
|
||||
/// The caller is responsible for transforming [position] from global
|
||||
/// coordinates to its location relative to the origin of this [RenderBox].
|
||||
/// This [RenderBox] is responsible for checking whether the given position is
|
||||
/// within its bounds.
|
||||
///
|
||||
/// Used by [hitTest]. If you override [hitTest] and do not call this
|
||||
/// function, then you don't need to implement this function.
|
||||
@protected
|
||||
|
||||
@@ -2,58 +2,76 @@
|
||||
// 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_test/flutter_test.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/scheduler.dart';
|
||||
|
||||
import 'feedback_tester.dart';
|
||||
|
||||
void main() {
|
||||
/// Tests that a [Chip] that has its size constrained by its parent is
|
||||
/// further constraining the size of its child, the label widget.
|
||||
/// Optionally, adding an avatar or delete icon to the chip should not
|
||||
/// cause the chip or label to exceed its constrained height.
|
||||
Future<Null> _testConstrainedLabel(
|
||||
WidgetTester tester, {
|
||||
CircleAvatar avatar,
|
||||
VoidCallback onDeleted,
|
||||
}) async {
|
||||
const double labelWidth = 100.0;
|
||||
const double labelHeight = 50.0;
|
||||
const double chipParentWidth = 75.0;
|
||||
const double chipParentHeight = 25.0;
|
||||
final Key labelKey = new UniqueKey();
|
||||
Finder findRenderChipElement() {
|
||||
return find.byElementPredicate((Element e) => '${e.runtimeType}' == '_RenderChipElement');
|
||||
}
|
||||
|
||||
await tester.pumpWidget(
|
||||
new MaterialApp(
|
||||
home: new Material(
|
||||
child: new Center(
|
||||
child: new Container(
|
||||
width: chipParentWidth,
|
||||
height: chipParentHeight,
|
||||
child: new Chip(
|
||||
avatar: avatar,
|
||||
label: new Container(
|
||||
key: labelKey,
|
||||
width: labelWidth,
|
||||
height: labelHeight,
|
||||
),
|
||||
onDeleted: onDeleted,
|
||||
dynamic getRenderChip(WidgetTester tester) {
|
||||
if (!tester.any(findRenderChipElement())) {
|
||||
return null;
|
||||
}
|
||||
final Element element = tester.element(findRenderChipElement());
|
||||
return element.renderObject;
|
||||
}
|
||||
|
||||
double getSelectProgress(WidgetTester tester) => getRenderChip(tester)?.checkmarkAnimation?.value;
|
||||
double getAvatarDrawerProgress(WidgetTester tester) => getRenderChip(tester)?.avatarDrawerAnimation?.value;
|
||||
double getDeleteDrawerProgress(WidgetTester tester) => getRenderChip(tester)?.deleteDrawerAnimation?.value;
|
||||
double getEnableProgress(WidgetTester tester) => getRenderChip(tester)?.enableAnimation?.value;
|
||||
|
||||
/// Tests that a [Chip] that has its size constrained by its parent is
|
||||
/// further constraining the size of its child, the label widget.
|
||||
/// Optionally, adding an avatar or delete icon to the chip should not
|
||||
/// cause the chip or label to exceed its constrained height.
|
||||
Future<Null> _testConstrainedLabel(
|
||||
WidgetTester tester, {
|
||||
CircleAvatar avatar,
|
||||
VoidCallback onDeleted,
|
||||
}) async {
|
||||
const double labelWidth = 100.0;
|
||||
const double labelHeight = 50.0;
|
||||
const double chipParentWidth = 75.0;
|
||||
const double chipParentHeight = 25.0;
|
||||
final Key labelKey = new UniqueKey();
|
||||
|
||||
await tester.pumpWidget(
|
||||
new MaterialApp(
|
||||
home: new Material(
|
||||
child: new Center(
|
||||
child: new Container(
|
||||
width: chipParentWidth,
|
||||
height: chipParentHeight,
|
||||
child: new Chip(
|
||||
avatar: avatar,
|
||||
label: new Container(
|
||||
key: labelKey,
|
||||
width: labelWidth,
|
||||
height: labelHeight,
|
||||
),
|
||||
onDeleted: onDeleted,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
),
|
||||
);
|
||||
|
||||
final Size labelSize = tester.getSize(find.byKey(labelKey));
|
||||
expect(labelSize.width, lessThan(chipParentWidth));
|
||||
expect(labelSize.height, lessThanOrEqualTo(chipParentHeight));
|
||||
final Size labelSize = tester.getSize(find.byKey(labelKey));
|
||||
expect(labelSize.width, lessThan(chipParentWidth));
|
||||
expect(labelSize.height, lessThanOrEqualTo(chipParentHeight));
|
||||
|
||||
final Size chipSize = tester.getSize(find.byType(Chip));
|
||||
expect(chipSize.width, chipParentWidth);
|
||||
expect(chipSize.height, chipParentHeight);
|
||||
}
|
||||
final Size chipSize = tester.getSize(find.byType(Chip));
|
||||
expect(chipSize.width, chipParentWidth);
|
||||
expect(chipSize.height, chipParentHeight);
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets('Chip control test', (WidgetTester tester) async {
|
||||
final FeedbackTester feedback = new FeedbackTester();
|
||||
final List<String> deletedChipLabels = <String>[];
|
||||
@@ -182,7 +200,7 @@ void main() {
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(tester.getSize(find.byType(Text)), const Size(40.0, 10.0));
|
||||
expect(tester.getSize(find.byType(Text)), const Size(40.0, 24.0));
|
||||
expect(tester.getSize(find.byType(Chip)), const Size(64.0, 32.0));
|
||||
await tester.pumpWidget(
|
||||
new MaterialApp(
|
||||
@@ -212,17 +230,17 @@ void main() {
|
||||
expect(tester.getSize(find.byType(Chip)), const Size(800.0, 32.0));
|
||||
});
|
||||
|
||||
testWidgets('Chip supports RTL', (WidgetTester tester) async {
|
||||
testWidgets('Chip elements are ordered horizontally for locale', (WidgetTester tester) async {
|
||||
final UniqueKey iconKey = new UniqueKey();
|
||||
final Widget test = new Overlay(
|
||||
initialEntries: <OverlayEntry>[
|
||||
new OverlayEntry(
|
||||
builder: (BuildContext context) {
|
||||
return new Material(
|
||||
child: new Center(
|
||||
child: new Chip(
|
||||
onDeleted: () {},
|
||||
label: const Text('ABC'),
|
||||
),
|
||||
child: new Chip(
|
||||
deleteIcon: new Icon(Icons.delete, key: iconKey),
|
||||
onDeleted: () {},
|
||||
label: const Text('ABC'),
|
||||
),
|
||||
);
|
||||
},
|
||||
@@ -243,8 +261,8 @@ void main() {
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(tester.getCenter(find.text('ABC')).dx, greaterThan(tester.getCenter(find.byType(Icon)).dx));
|
||||
|
||||
await tester.pumpAndSettle(const Duration(milliseconds: 500));
|
||||
expect(tester.getCenter(find.text('ABC')).dx, greaterThan(tester.getCenter(find.byKey(iconKey)).dx));
|
||||
await tester.pumpWidget(
|
||||
new Localizations(
|
||||
locale: const Locale('en', 'US'),
|
||||
@@ -258,7 +276,8 @@ void main() {
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(tester.getCenter(find.text('ABC')).dx, lessThan(tester.getCenter(find.byType(Icon)).dx));
|
||||
await tester.pumpAndSettle(const Duration(milliseconds: 500));
|
||||
expect(tester.getCenter(find.text('ABC')).dx, lessThan(tester.getCenter(find.byKey(iconKey)).dx));
|
||||
});
|
||||
|
||||
testWidgets('Chip responds to textScaleFactor', (WidgetTester tester) async {
|
||||
@@ -320,10 +339,10 @@ void main() {
|
||||
// https://github.com/flutter/flutter/issues/12357
|
||||
expect(tester.getSize(find.text('Chip A')), anyOf(const Size(252.0, 42.0), const Size(251.0, 42.0)));
|
||||
expect(tester.getSize(find.text('Chip B')), anyOf(const Size(252.0, 42.0), const Size(251.0, 42.0)));
|
||||
expect(tester.getSize(find.byType(Chip).first).width, anyOf(310.0, 309.0));
|
||||
expect(tester.getSize(find.byType(Chip).first).height, equals(42.0));
|
||||
expect(tester.getSize(find.byType(Chip).last).width, anyOf(310.0, 309.0));
|
||||
expect(tester.getSize(find.byType(Chip).last).height, equals(42.0));
|
||||
expect(tester.getSize(find.byType(Chip).first).width, anyOf(318.0, 319.0));
|
||||
expect(tester.getSize(find.byType(Chip).first).height, equals(50.0));
|
||||
expect(tester.getSize(find.byType(Chip).last).width, anyOf(318.0, 319.0));
|
||||
expect(tester.getSize(find.byType(Chip).last).height, equals(50.0));
|
||||
|
||||
// Check that individual text scales are taken into account.
|
||||
await tester.pumpWidget(
|
||||
@@ -349,8 +368,8 @@ void main() {
|
||||
// https://github.com/flutter/flutter/issues/12357
|
||||
expect(tester.getSize(find.text('Chip A')), anyOf(const Size(252.0, 42.0), const Size(251.0, 42.0)));
|
||||
expect(tester.getSize(find.text('Chip B')), anyOf(const Size(84.0, 14.0), const Size(83.0, 14.0)));
|
||||
expect(tester.getSize(find.byType(Chip).first).width, anyOf(310.0, 309.0));
|
||||
expect(tester.getSize(find.byType(Chip).first).height, equals(42.0));
|
||||
expect(tester.getSize(find.byType(Chip).first).width, anyOf(318.0, 319.0));
|
||||
expect(tester.getSize(find.byType(Chip).first).height, equals(50.0));
|
||||
expect(tester.getSize(find.byType(Chip).last), anyOf(const Size(132.0, 32.0), const Size(131.0, 32.0)));
|
||||
});
|
||||
|
||||
@@ -390,6 +409,47 @@ void main() {
|
||||
expect(tester.getSize(find.byType(Chip).last), const Size(58.0, 32.0));
|
||||
});
|
||||
|
||||
testWidgets('Avatars can be non-circle avatar widgets', (WidgetTester tester) async {
|
||||
final Key keyA = new GlobalKey();
|
||||
await tester.pumpWidget(
|
||||
new MaterialApp(
|
||||
home: new Material(
|
||||
child: new Column(
|
||||
children: <Widget>[
|
||||
new Chip(
|
||||
avatar: new Container(key: keyA, width: 20.0, height: 20.0),
|
||||
label: const Text('Chip A'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
expect(tester.getSize(find.byKey(keyA)), equals(const Size(20.0, 20.0)));
|
||||
});
|
||||
|
||||
testWidgets('Delete icons can be non-icon widgets', (WidgetTester tester) async {
|
||||
final Key keyA = new GlobalKey();
|
||||
await tester.pumpWidget(
|
||||
new MaterialApp(
|
||||
home: new Material(
|
||||
child: new Column(
|
||||
children: <Widget>[
|
||||
new Chip(
|
||||
deleteIcon: new Container(key: keyA, width: 20.0, height: 20.0),
|
||||
label: const Text('Chip A'),
|
||||
onDeleted: () {},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
expect(tester.getSize(find.byKey(keyA)), equals(const Size(20.0, 20.0)));
|
||||
});
|
||||
|
||||
testWidgets('Chip padding - LTR', (WidgetTester tester) async {
|
||||
final GlobalKey keyA = new GlobalKey();
|
||||
final GlobalKey keyB = new GlobalKey();
|
||||
@@ -410,7 +470,11 @@ void main() {
|
||||
child: new Center(
|
||||
child: new Chip(
|
||||
avatar: new Placeholder(key: keyA),
|
||||
label: new Container(key: keyB, width: 40.0, height: 40.0,),
|
||||
label: new Container(
|
||||
key: keyB,
|
||||
width: 40.0,
|
||||
height: 40.0,
|
||||
),
|
||||
onDeleted: () {},
|
||||
),
|
||||
),
|
||||
@@ -422,12 +486,12 @@ void main() {
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(tester.getTopLeft(find.byKey(keyA)), const Offset(340.0, 284.0));
|
||||
expect(tester.getBottomRight(find.byKey(keyA)), const Offset(372.0, 316.0));
|
||||
expect(tester.getTopLeft(find.byKey(keyA)), const Offset(332.0, 280.0));
|
||||
expect(tester.getBottomRight(find.byKey(keyA)), const Offset(372.0, 320.0));
|
||||
expect(tester.getTopLeft(find.byKey(keyB)), const Offset(380.0, 280.0));
|
||||
expect(tester.getBottomRight(find.byKey(keyB)), const Offset(420.0, 320.0));
|
||||
expect(tester.getTopLeft(find.byType(Icon)), const Offset(428.0, 284.0));
|
||||
expect(tester.getBottomRight(find.byType(Icon)), const Offset(460.0, 316.0));
|
||||
expect(tester.getTopLeft(find.byType(Icon)), const Offset(439.0, 291.0));
|
||||
expect(tester.getBottomRight(find.byType(Icon)), const Offset(457.0, 309.0));
|
||||
});
|
||||
|
||||
testWidgets('Chip padding - RTL', (WidgetTester tester) async {
|
||||
@@ -450,7 +514,11 @@ void main() {
|
||||
child: new Center(
|
||||
child: new Chip(
|
||||
avatar: new Placeholder(key: keyA),
|
||||
label: new Container(key: keyB, width: 40.0, height: 40.0,),
|
||||
label: new Container(
|
||||
key: keyB,
|
||||
width: 40.0,
|
||||
height: 40.0,
|
||||
),
|
||||
onDeleted: () {},
|
||||
),
|
||||
),
|
||||
@@ -463,11 +531,466 @@ void main() {
|
||||
),
|
||||
);
|
||||
|
||||
expect(tester.getTopLeft(find.byKey(keyA)), const Offset(428.0, 284.0));
|
||||
expect(tester.getBottomRight(find.byKey(keyA)), const Offset(460.0, 316.0));
|
||||
expect(tester.getTopLeft(find.byKey(keyA)), const Offset(428.0, 280.0));
|
||||
expect(tester.getBottomRight(find.byKey(keyA)), const Offset(468.0, 320.0));
|
||||
expect(tester.getTopLeft(find.byKey(keyB)), const Offset(380.0, 280.0));
|
||||
expect(tester.getBottomRight(find.byKey(keyB)), const Offset(420.0, 320.0));
|
||||
expect(tester.getTopLeft(find.byType(Icon)), const Offset(340.0, 284.0));
|
||||
expect(tester.getBottomRight(find.byType(Icon)), const Offset(372.0, 316.0));
|
||||
expect(tester.getTopLeft(find.byType(Icon)), const Offset(343.0, 291.0));
|
||||
expect(tester.getBottomRight(find.byType(Icon)), const Offset(361.0, 309.0));
|
||||
});
|
||||
|
||||
testWidgets('Avatar drawer works as expected on RawChip', (WidgetTester tester) async {
|
||||
final GlobalKey labelKey = new GlobalKey();
|
||||
Future<Null> pushChip({Widget avatar}) async {
|
||||
return tester.pumpWidget(
|
||||
new MaterialApp(
|
||||
home: new Material(
|
||||
child: new Wrap(
|
||||
children: <Widget>[
|
||||
new RawChip(
|
||||
avatar: avatar,
|
||||
label: new Text('Chip', key: labelKey),
|
||||
border: const StadiumBorder(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// No avatar
|
||||
await pushChip();
|
||||
expect(tester.getSize(find.byType(RawChip)), equals(const Size(80.0, 32.0)));
|
||||
final GlobalKey avatarKey = new GlobalKey();
|
||||
|
||||
// Add an avatar
|
||||
await pushChip(
|
||||
avatar: new Container(
|
||||
key: avatarKey,
|
||||
color: const Color(0xff000000),
|
||||
width: 40.0,
|
||||
height: 40.0,
|
||||
),
|
||||
);
|
||||
// Avatar drawer should start out closed.
|
||||
expect(tester.getSize(find.byType(RawChip)), equals(const Size(80.0, 32.0)));
|
||||
expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
|
||||
expect(tester.getTopLeft(find.byKey(avatarKey)), equals(const Offset(-20.0, 4.0)));
|
||||
expect(tester.getTopLeft(find.byKey(labelKey)), equals(const Offset(12.0, 9.0)));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 20));
|
||||
// Avatar drawer should start expanding.
|
||||
expect(tester.getSize(find.byType(RawChip)).width, closeTo(81.2, 0.1));
|
||||
expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
|
||||
expect(tester.getTopLeft(find.byKey(avatarKey)).dx, closeTo(-18.8, 0.1));
|
||||
expect(tester.getTopLeft(find.byKey(labelKey)).dx, closeTo(13.2, 0.1));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 20));
|
||||
expect(tester.getSize(find.byType(RawChip)).width, closeTo(86.7, 0.1));
|
||||
expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
|
||||
expect(tester.getTopLeft(find.byKey(avatarKey)).dx, closeTo(-13.3, 0.1));
|
||||
expect(tester.getTopLeft(find.byKey(labelKey)).dx, closeTo(18.6, 0.1));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 20));
|
||||
expect(tester.getSize(find.byType(RawChip)).width, closeTo(94.7, 0.1));
|
||||
expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
|
||||
expect(tester.getTopLeft(find.byKey(avatarKey)).dx, closeTo(-5.3, 0.1));
|
||||
expect(tester.getTopLeft(find.byKey(labelKey)).dx, closeTo(26.7, 0.1));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 20));
|
||||
expect(tester.getSize(find.byType(RawChip)).width, closeTo(99.5, 0.1));
|
||||
expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
|
||||
expect(tester.getTopLeft(find.byKey(avatarKey)).dx, closeTo(-0.5, 0.1));
|
||||
expect(tester.getTopLeft(find.byKey(labelKey)).dx, closeTo(31.5, 0.1));
|
||||
|
||||
// Wait for being done with animation, and make sure it didn't change
|
||||
// height.
|
||||
await tester.pumpAndSettle(const Duration(milliseconds: 200));
|
||||
expect(tester.getSize(find.byType(RawChip)), equals(const Size(104.0, 32.0)));
|
||||
expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
|
||||
expect(tester.getTopLeft(find.byKey(avatarKey)), equals(const Offset(4.0, 4.0)));
|
||||
expect(tester.getTopLeft(find.byKey(labelKey)), equals(const Offset(36.0, 9.0)));
|
||||
|
||||
// Remove the avatar again
|
||||
await pushChip();
|
||||
// Avatar drawer should start out open.
|
||||
expect(tester.getSize(find.byType(RawChip)), equals(const Size(104.0, 32.0)));
|
||||
expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
|
||||
expect(tester.getTopLeft(find.byKey(avatarKey)), equals(const Offset(4.0, 4.0)));
|
||||
expect(tester.getTopLeft(find.byKey(labelKey)), equals(const Offset(36.0, 9.0)));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 20));
|
||||
// Avatar drawer should start contracting.
|
||||
expect(tester.getSize(find.byType(RawChip)).width, closeTo(102.9, 0.1));
|
||||
expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
|
||||
expect(tester.getTopLeft(find.byKey(avatarKey)).dx, closeTo(2.9, 0.1));
|
||||
expect(tester.getTopLeft(find.byKey(labelKey)).dx, closeTo(34.9, 0.1));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 20));
|
||||
expect(tester.getSize(find.byType(RawChip)).width, closeTo(98.0, 0.1));
|
||||
expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
|
||||
expect(tester.getTopLeft(find.byKey(avatarKey)).dx, closeTo(-2.0, 0.1));
|
||||
expect(tester.getTopLeft(find.byKey(labelKey)).dx, closeTo(30.0, 0.1));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 20));
|
||||
expect(tester.getSize(find.byType(RawChip)).width, closeTo(84.1, 0.1));
|
||||
expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
|
||||
expect(tester.getTopLeft(find.byKey(avatarKey)).dx, closeTo(-15.9, 0.1));
|
||||
expect(tester.getTopLeft(find.byKey(labelKey)).dx, closeTo(16.1, 0.1));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 20));
|
||||
expect(tester.getSize(find.byType(RawChip)).width, closeTo(80.0, 0.1));
|
||||
expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
|
||||
expect(tester.getTopLeft(find.byKey(avatarKey)).dx, closeTo(-20.0, 0.1));
|
||||
expect(tester.getTopLeft(find.byKey(labelKey)).dx, closeTo(12.0, 0.1));
|
||||
|
||||
// Wait for being done with animation, make sure it didn't change
|
||||
// height, and make sure that the avatar is no longer drawn.
|
||||
await tester.pumpAndSettle(const Duration(milliseconds: 200));
|
||||
expect(tester.getSize(find.byType(RawChip)), equals(const Size(80.0, 32.0)));
|
||||
expect(tester.getTopLeft(find.byKey(labelKey)), equals(const Offset(12.0, 9.0)));
|
||||
expect(find.byKey(avatarKey), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('Delete button drawer works as expected on RawChip', (WidgetTester tester) async {
|
||||
final UniqueKey labelKey = new UniqueKey();
|
||||
final UniqueKey deleteButtonKey = new UniqueKey();
|
||||
bool wasDeleted = false;
|
||||
Future<Null> pushChip({bool deletable: false}) async {
|
||||
return tester.pumpWidget(
|
||||
new MaterialApp(
|
||||
home: new Material(
|
||||
child: new Wrap(
|
||||
children: <Widget>[
|
||||
new StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
|
||||
return new RawChip(
|
||||
onDeleted: deletable
|
||||
? () {
|
||||
setState(() {
|
||||
wasDeleted = true;
|
||||
});
|
||||
}
|
||||
: null,
|
||||
deleteIcon: new Container(width: 40.0, height: 40.0, key: deleteButtonKey),
|
||||
label: new Text('Chip', key: labelKey),
|
||||
border: const StadiumBorder(),
|
||||
);
|
||||
}),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// No delete button
|
||||
await pushChip();
|
||||
expect(tester.getSize(find.byType(RawChip)), equals(const Size(80.0, 32.0)));
|
||||
|
||||
// Add a delete button
|
||||
await pushChip(deletable: true);
|
||||
// Delete button drawer should start out closed.
|
||||
expect(tester.getSize(find.byType(RawChip)), equals(const Size(80.0, 32.0)));
|
||||
expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
|
||||
expect(tester.getTopLeft(find.byKey(deleteButtonKey)), equals(const Offset(52.0, 4.0)));
|
||||
expect(tester.getTopLeft(find.byKey(labelKey)), equals(const Offset(12.0, 9.0)));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 20));
|
||||
// Delete button drawer should start expanding.
|
||||
expect(tester.getSize(find.byType(RawChip)).width, closeTo(81.2, 0.1));
|
||||
expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
|
||||
expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, closeTo(53.2, 0.1));
|
||||
expect(tester.getTopLeft(find.byKey(labelKey)), equals(const Offset(12.0, 9.0)));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 20));
|
||||
expect(tester.getSize(find.byType(RawChip)).width, closeTo(86.7, 0.1));
|
||||
expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
|
||||
expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, closeTo(58.7, 0.1));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 20));
|
||||
expect(tester.getSize(find.byType(RawChip)).width, closeTo(94.7, 0.1));
|
||||
expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
|
||||
expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, closeTo(66.7, 0.1));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 20));
|
||||
expect(tester.getSize(find.byType(RawChip)).width, closeTo(99.5, 0.1));
|
||||
expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
|
||||
expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, closeTo(71.5, 0.1));
|
||||
|
||||
// Wait for being done with animation, and make sure it didn't change
|
||||
// height.
|
||||
await tester.pumpAndSettle(const Duration(milliseconds: 200));
|
||||
expect(tester.getSize(find.byType(RawChip)), equals(const Size(104.0, 32.0)));
|
||||
expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
|
||||
expect(tester.getTopLeft(find.byKey(deleteButtonKey)), equals(const Offset(76.0, 4.0)));
|
||||
expect(tester.getTopLeft(find.byKey(labelKey)), equals(const Offset(12.0, 9.0)));
|
||||
|
||||
// Test the tap work for the delete button, but not the rest of the chip.
|
||||
expect(wasDeleted, isFalse);
|
||||
await tester.tap(find.byKey(labelKey));
|
||||
expect(wasDeleted, isFalse);
|
||||
await tester.tap(find.byKey(deleteButtonKey));
|
||||
expect(wasDeleted, isTrue);
|
||||
|
||||
// Remove the delete button again
|
||||
await pushChip();
|
||||
// Delete button drawer should start out open.
|
||||
expect(tester.getSize(find.byType(RawChip)), equals(const Size(104.0, 32.0)));
|
||||
expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
|
||||
expect(tester.getTopLeft(find.byKey(deleteButtonKey)), equals(const Offset(76.0, 4.0)));
|
||||
expect(tester.getTopLeft(find.byKey(labelKey)), equals(const Offset(12.0, 9.0)));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 20));
|
||||
// Delete button drawer should start contracting.
|
||||
expect(tester.getSize(find.byType(RawChip)).width, closeTo(103.8, 0.1));
|
||||
expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
|
||||
expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, closeTo(75.8, 0.1));
|
||||
expect(tester.getTopLeft(find.byKey(labelKey)), equals(const Offset(12.0, 9.0)));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 20));
|
||||
expect(tester.getSize(find.byType(RawChip)).width, closeTo(102.9, 0.1));
|
||||
expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
|
||||
expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, closeTo(74.9, 0.1));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 20));
|
||||
expect(tester.getSize(find.byType(RawChip)).width, closeTo(101.0, 0.1));
|
||||
expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
|
||||
expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, closeTo(73.0, 0.1));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 20));
|
||||
expect(tester.getSize(find.byType(RawChip)).width, closeTo(97.5, 0.1));
|
||||
expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
|
||||
expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, closeTo(69.5, 0.1));
|
||||
|
||||
// Wait for being done with animation, make sure it didn't change
|
||||
// height, and make sure that the delete button is no longer drawn.
|
||||
await tester.pumpAndSettle(const Duration(milliseconds: 200));
|
||||
expect(tester.getSize(find.byType(RawChip)), equals(const Size(80.0, 32.0)));
|
||||
expect(tester.getTopLeft(find.byKey(labelKey)), equals(const Offset(12.0, 9.0)));
|
||||
expect(find.byKey(deleteButtonKey), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('Selection with avatar works as expected on RawChip', (WidgetTester tester) async {
|
||||
bool selected = false;
|
||||
final UniqueKey labelKey = new UniqueKey();
|
||||
Future<Null> pushChip({Widget avatar, bool selectable: false}) async {
|
||||
return tester.pumpWidget(
|
||||
new MaterialApp(
|
||||
home: new Material(
|
||||
child: new Wrap(
|
||||
children: <Widget>[
|
||||
new StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
|
||||
return new RawChip(
|
||||
avatar: avatar,
|
||||
onSelected: selectable != null
|
||||
? (bool value) {
|
||||
setState(() {
|
||||
selected = value;
|
||||
});
|
||||
}
|
||||
: null,
|
||||
selected: selected,
|
||||
label: new Text('Chip', key: labelKey),
|
||||
border: const StadiumBorder(),
|
||||
showCheckmark: true,
|
||||
tapEnabled: true,
|
||||
isEnabled: true,
|
||||
);
|
||||
}),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// With avatar, but not selectable.
|
||||
final UniqueKey avatarKey = new UniqueKey();
|
||||
await pushChip(
|
||||
avatar: new Container(width: 40.0, height: 40.0, key: avatarKey),
|
||||
);
|
||||
expect(tester.getSize(find.byType(RawChip)), equals(const Size(104.0, 32.0)));
|
||||
|
||||
// Turn on selection.
|
||||
await pushChip(
|
||||
avatar: new Container(width: 40.0, height: 40.0, key: avatarKey),
|
||||
selectable: true,
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Simulate a tap on the label to select the chip.
|
||||
await tester.tap(find.byKey(labelKey));
|
||||
expect(selected, equals(true));
|
||||
expect(SchedulerBinding.instance.transientCallbackCount, equals(2));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 50));
|
||||
expect(getSelectProgress(tester), closeTo(0.002, 0.01));
|
||||
expect(getAvatarDrawerProgress(tester), equals(1.0));
|
||||
expect(getDeleteDrawerProgress(tester), equals(0.0));
|
||||
await tester.pump(const Duration(milliseconds: 50));
|
||||
expect(getSelectProgress(tester), closeTo(0.54, 0.01));
|
||||
expect(getAvatarDrawerProgress(tester), equals(1.0));
|
||||
expect(getDeleteDrawerProgress(tester), equals(0.0));
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
expect(getSelectProgress(tester), equals(1.0));
|
||||
expect(getAvatarDrawerProgress(tester), equals(1.0));
|
||||
expect(getDeleteDrawerProgress(tester), equals(0.0));
|
||||
await tester.pumpAndSettle();
|
||||
// Simulate another tap on the label to deselect the chip.
|
||||
await tester.tap(find.byKey(labelKey));
|
||||
expect(selected, equals(false));
|
||||
expect(SchedulerBinding.instance.transientCallbackCount, equals(2));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 20));
|
||||
expect(getSelectProgress(tester), closeTo(0.875, 0.01));
|
||||
expect(getAvatarDrawerProgress(tester), equals(1.0));
|
||||
expect(getDeleteDrawerProgress(tester), equals(0.0));
|
||||
await tester.pump(const Duration(milliseconds: 20));
|
||||
expect(getSelectProgress(tester), closeTo(0.13, 0.01));
|
||||
expect(getAvatarDrawerProgress(tester), equals(1.0));
|
||||
expect(getDeleteDrawerProgress(tester), equals(0.0));
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
expect(getSelectProgress(tester), equals(0.0));
|
||||
expect(getAvatarDrawerProgress(tester), equals(1.0));
|
||||
expect(getDeleteDrawerProgress(tester), equals(0.0));
|
||||
});
|
||||
|
||||
testWidgets('Selection without avatar works as expected on RawChip', (WidgetTester tester) async {
|
||||
bool selected = false;
|
||||
final UniqueKey labelKey = new UniqueKey();
|
||||
Future<Null> pushChip({bool selectable: false}) async {
|
||||
return tester.pumpWidget(
|
||||
new MaterialApp(
|
||||
home: new Material(
|
||||
child: new Wrap(
|
||||
children: <Widget>[
|
||||
new StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
|
||||
return new RawChip(
|
||||
onSelected: selectable != null
|
||||
? (bool value) {
|
||||
setState(() {
|
||||
selected = value;
|
||||
});
|
||||
}
|
||||
: null,
|
||||
selected: selected,
|
||||
label: new Text('Chip', key: labelKey),
|
||||
border: const StadiumBorder(),
|
||||
showCheckmark: true,
|
||||
tapEnabled: true,
|
||||
isEnabled: true,
|
||||
);
|
||||
}),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Without avatar, but not selectable.
|
||||
await pushChip();
|
||||
expect(tester.getSize(find.byType(RawChip)), equals(const Size(80.0, 32.0)));
|
||||
|
||||
// Turn on selection.
|
||||
await pushChip(selectable: true);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Simulate a tap on the label to select the chip.
|
||||
await tester.tap(find.byKey(labelKey));
|
||||
expect(selected, equals(true));
|
||||
expect(SchedulerBinding.instance.transientCallbackCount, equals(2));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 50));
|
||||
expect(getSelectProgress(tester), closeTo(0.002, 0.01));
|
||||
expect(getAvatarDrawerProgress(tester), closeTo(0.459, 0.01));
|
||||
expect(getDeleteDrawerProgress(tester), equals(0.0));
|
||||
await tester.pump(const Duration(milliseconds: 50));
|
||||
expect(getSelectProgress(tester), closeTo(0.54, 0.01));
|
||||
expect(getAvatarDrawerProgress(tester), closeTo(0.92, 0.01));
|
||||
expect(getDeleteDrawerProgress(tester), equals(0.0));
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
expect(getSelectProgress(tester), equals(1.0));
|
||||
expect(getAvatarDrawerProgress(tester), equals(1.0));
|
||||
expect(getDeleteDrawerProgress(tester), equals(0.0));
|
||||
await tester.pumpAndSettle();
|
||||
// Simulate another tap on the label to deselect the chip.
|
||||
await tester.tap(find.byKey(labelKey));
|
||||
expect(selected, equals(false));
|
||||
expect(SchedulerBinding.instance.transientCallbackCount, equals(2));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 20));
|
||||
expect(getSelectProgress(tester), closeTo(0.875, 0.01));
|
||||
expect(getAvatarDrawerProgress(tester), closeTo(0.96, 0.01));
|
||||
expect(getDeleteDrawerProgress(tester), equals(0.0));
|
||||
await tester.pump(const Duration(milliseconds: 20));
|
||||
expect(getSelectProgress(tester), closeTo(0.13, 0.01));
|
||||
expect(getAvatarDrawerProgress(tester), closeTo(0.75, 0.01));
|
||||
expect(getDeleteDrawerProgress(tester), equals(0.0));
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
expect(getSelectProgress(tester), equals(0.0));
|
||||
expect(getAvatarDrawerProgress(tester), equals(0.0));
|
||||
expect(getDeleteDrawerProgress(tester), equals(0.0));
|
||||
});
|
||||
|
||||
testWidgets('Activation works as expected on RawChip', (WidgetTester tester) async {
|
||||
bool selected = false;
|
||||
final UniqueKey labelKey = new UniqueKey();
|
||||
Future<Null> pushChip({Widget avatar, bool selectable: false}) async {
|
||||
return tester.pumpWidget(
|
||||
new MaterialApp(
|
||||
home: new Material(
|
||||
child: new Wrap(
|
||||
children: <Widget>[
|
||||
new StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
|
||||
return new RawChip(
|
||||
avatar: avatar,
|
||||
onSelected: selectable != null
|
||||
? (bool value) {
|
||||
setState(() {
|
||||
selected = value;
|
||||
});
|
||||
}
|
||||
: null,
|
||||
selected: selected,
|
||||
label: new Text('Chip', key: labelKey),
|
||||
border: const StadiumBorder(),
|
||||
showCheckmark: false,
|
||||
tapEnabled: true,
|
||||
isEnabled: true,
|
||||
);
|
||||
}),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final UniqueKey avatarKey = new UniqueKey();
|
||||
await pushChip(
|
||||
avatar: new Container(width: 40.0, height: 40.0, key: avatarKey),
|
||||
selectable: true,
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.tap(find.byKey(labelKey));
|
||||
expect(selected, equals(true));
|
||||
expect(SchedulerBinding.instance.transientCallbackCount, equals(2));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 50));
|
||||
expect(getSelectProgress(tester), closeTo(0.002, 0.01));
|
||||
expect(getAvatarDrawerProgress(tester), equals(1.0));
|
||||
expect(getDeleteDrawerProgress(tester), equals(0.0));
|
||||
await tester.pump(const Duration(milliseconds: 50));
|
||||
expect(getSelectProgress(tester), closeTo(0.54, 0.01));
|
||||
expect(getAvatarDrawerProgress(tester), equals(1.0));
|
||||
expect(getDeleteDrawerProgress(tester), equals(0.0));
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
expect(getSelectProgress(tester), equals(1.0));
|
||||
expect(getAvatarDrawerProgress(tester), equals(1.0));
|
||||
expect(getDeleteDrawerProgress(tester), equals(0.0));
|
||||
await tester.pumpAndSettle();
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user