New Reorderable list widgets (#74299)
Introduced new widget/ReorderableList and widget/SliverReorderableList widgets.
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter/semantics.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@@ -90,6 +91,7 @@ void main() {
|
||||
await drag.moveTo(tester.getCenter(find.text('Item 4')));
|
||||
expect(listItems, orderedEquals(originalListItems));
|
||||
await drag.up();
|
||||
await tester.pumpAndSettle();
|
||||
expect(listItems, orderedEquals(<String>['Item 2', 'Item 3', 'Item 1', 'Item 4']));
|
||||
});
|
||||
|
||||
@@ -101,6 +103,7 @@ void main() {
|
||||
tester.getCenter(find.text('Item 1')),
|
||||
tester.getCenter(find.text('Item 4')) + const Offset(0.0, itemHeight * 2),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(listItems, orderedEquals(<String>['Item 2', 'Item 3', 'Item 4', 'Item 1']));
|
||||
});
|
||||
|
||||
@@ -112,6 +115,7 @@ void main() {
|
||||
tester.getCenter(find.text('Item 4')),
|
||||
tester.getCenter(find.text('Item 1')),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(listItems, orderedEquals(<String>['Item 4', 'Item 1', 'Item 2', 'Item 3']));
|
||||
});
|
||||
|
||||
@@ -123,6 +127,7 @@ void main() {
|
||||
tester.getCenter(find.text('Item 3')),
|
||||
tester.getCenter(find.text('Item 2')),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(listItems, orderedEquals(<String>['Item 1', 'Item 3', 'Item 2', 'Item 4']));
|
||||
});
|
||||
|
||||
@@ -135,6 +140,7 @@ void main() {
|
||||
tester.getCenter(find.text('Item 1')),
|
||||
tester.getCenter(find.text('Item 4')) + const Offset(0.0, itemHeight * 2),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Header Text'), findsOneWidget);
|
||||
expect(listItems, orderedEquals(<String>['Item 2', 'Item 3', 'Item 4', 'Item 1']));
|
||||
});
|
||||
@@ -168,45 +174,43 @@ void main() {
|
||||
),
|
||||
));
|
||||
|
||||
Element getContentElement() {
|
||||
final SingleChildScrollView listScrollView = tester.widget(find.byType(SingleChildScrollView));
|
||||
final Widget scrollContents = listScrollView.child!;
|
||||
final Element contentElement = tester.element(find.byElementPredicate((Element element) => element.widget == scrollContents));
|
||||
return contentElement;
|
||||
double getListHeight() {
|
||||
final RenderSliverList listScrollView = tester.renderObject(find.byType(SliverList));
|
||||
return listScrollView.geometry!.maxPaintExtent;
|
||||
}
|
||||
|
||||
const double kDraggingListHeight = 292.0;
|
||||
const double kDraggingListHeight = 4 * itemHeight;
|
||||
// Drag a normal text item
|
||||
expect(getContentElement().size!.height, kDraggingListHeight);
|
||||
expect(getListHeight(), kDraggingListHeight);
|
||||
TestGesture drag = await tester.startGesture(tester.getCenter(find.text('Normal item')));
|
||||
await tester.pump(kLongPressTimeout + kPressTimeout);
|
||||
await tester.pumpAndSettle();
|
||||
expect(getContentElement().size!.height, kDraggingListHeight);
|
||||
expect(getListHeight(), kDraggingListHeight);
|
||||
|
||||
// Move it
|
||||
await drag.moveTo(tester.getCenter(find.text('Last item')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(getContentElement().size!.height, kDraggingListHeight);
|
||||
expect(getListHeight(), kDraggingListHeight);
|
||||
|
||||
// Drop it
|
||||
await drag.up();
|
||||
await tester.pumpAndSettle();
|
||||
expect(getContentElement().size!.height, kDraggingListHeight);
|
||||
expect(getListHeight(), kDraggingListHeight);
|
||||
|
||||
// Drag a tall item
|
||||
drag = await tester.startGesture(tester.getCenter(find.text('Tall item')));
|
||||
await tester.pump(kLongPressTimeout + kPressTimeout);
|
||||
await tester.pumpAndSettle();
|
||||
expect(getContentElement().size!.height, kDraggingListHeight);
|
||||
expect(getListHeight(), kDraggingListHeight);
|
||||
// Move it
|
||||
await drag.moveTo(tester.getCenter(find.text('Last item')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(getContentElement().size!.height, kDraggingListHeight);
|
||||
expect(getListHeight(), kDraggingListHeight);
|
||||
|
||||
// Drop it
|
||||
await drag.up();
|
||||
await tester.pumpAndSettle();
|
||||
expect(getContentElement().size!.height, kDraggingListHeight);
|
||||
expect(getListHeight(), kDraggingListHeight);
|
||||
});
|
||||
|
||||
testWidgets('Vertical drop area golden', (WidgetTester tester) async {
|
||||
@@ -344,8 +348,8 @@ void main() {
|
||||
}
|
||||
|
||||
await tester.pumpWidget(buildWithScrollController(primary));
|
||||
SingleChildScrollView scrollView = tester.widget(
|
||||
find.byType(SingleChildScrollView),
|
||||
Scrollable scrollView = tester.widget(
|
||||
find.byType(Scrollable),
|
||||
);
|
||||
expect(scrollView.controller, primary);
|
||||
|
||||
@@ -353,7 +357,7 @@ void main() {
|
||||
final ScrollController primary2 = ScrollController();
|
||||
await tester.pumpWidget(buildWithScrollController(primary2));
|
||||
scrollView = tester.widget(
|
||||
find.byType(SingleChildScrollView),
|
||||
find.byType(Scrollable),
|
||||
);
|
||||
expect(scrollView.controller, primary2);
|
||||
});
|
||||
@@ -367,7 +371,7 @@ void main() {
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: SizedBox(
|
||||
height: 200,
|
||||
height: 150,
|
||||
child: ReorderableListView(
|
||||
scrollController: customController,
|
||||
onReorder: (int oldIndex, int newIndex) { },
|
||||
@@ -414,6 +418,9 @@ void main() {
|
||||
// First 20.0 px always ignored, so scroll offset is only
|
||||
// shifted by 80.0.
|
||||
// Final offset: 40.0 + 80.0 = 120.0
|
||||
// The total distance available to scroll is 300.0 - 150.0 = 150.0, or
|
||||
// height of the ReorderableListView minus height of the SizedBox. Since
|
||||
// The final offset is less than this, it's not limited.
|
||||
expect(customController.offset, 120.0);
|
||||
});
|
||||
|
||||
@@ -446,11 +453,6 @@ void main() {
|
||||
} catch (e) {
|
||||
fail('Expected no error, but got $e');
|
||||
}
|
||||
// Expect that we have build *a* ScrollController for use in the view.
|
||||
final SingleChildScrollView scrollView = tester.widget(
|
||||
find.byType(SingleChildScrollView),
|
||||
);
|
||||
expect(scrollView.controller, isNotNull);
|
||||
});
|
||||
|
||||
group('Accessibility (a11y/Semantics)', () {
|
||||
@@ -675,6 +677,7 @@ void main() {
|
||||
tester.getCenter(find.text('Item 1')),
|
||||
tester.getCenter(find.text('Item 4')) + const Offset(itemHeight * 2, 0.0),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(listItems, orderedEquals(<String>['Item 2', 'Item 3', 'Item 4', 'Item 1']));
|
||||
});
|
||||
|
||||
@@ -686,6 +689,7 @@ void main() {
|
||||
tester.getCenter(find.text('Item 4')),
|
||||
tester.getCenter(find.text('Item 1')),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(listItems, orderedEquals(<String>['Item 4', 'Item 1', 'Item 2', 'Item 3']));
|
||||
});
|
||||
|
||||
@@ -697,6 +701,7 @@ void main() {
|
||||
tester.getCenter(find.text('Item 3')),
|
||||
tester.getCenter(find.text('Item 2')),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(listItems, orderedEquals(<String>['Item 1', 'Item 3', 'Item 2', 'Item 4']));
|
||||
});
|
||||
|
||||
@@ -718,6 +723,7 @@ void main() {
|
||||
tester.getCenter(find.text('Item 4')),
|
||||
tester.getCenter(find.text('Item 3')),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Header Text'), findsOneWidget);
|
||||
expect(listItems, orderedEquals(<String>['Item 2', 'Item 4', 'Item 3', 'Item 1']));
|
||||
});
|
||||
@@ -751,45 +757,43 @@ void main() {
|
||||
),
|
||||
));
|
||||
|
||||
Element getContentElement() {
|
||||
final SingleChildScrollView listScrollView = tester.widget(find.byType(SingleChildScrollView));
|
||||
final Widget scrollContents = listScrollView.child!;
|
||||
final Element contentElement = tester.element(find.byElementPredicate((Element element) => element.widget == scrollContents));
|
||||
return contentElement;
|
||||
double getListWidth() {
|
||||
final RenderSliverList listScrollView = tester.renderObject(find.byType(SliverList));
|
||||
return listScrollView.geometry!.maxPaintExtent;
|
||||
}
|
||||
|
||||
const double kDraggingListWidth = 292.0;
|
||||
const double kDraggingListWidth = 4 * itemHeight;
|
||||
// Drag a normal text item
|
||||
expect(getContentElement().size!.width, kDraggingListWidth);
|
||||
expect(getListWidth(), kDraggingListWidth);
|
||||
TestGesture drag = await tester.startGesture(tester.getCenter(find.text('Normal item')));
|
||||
await tester.pump(kLongPressTimeout + kPressTimeout);
|
||||
await tester.pumpAndSettle();
|
||||
expect(getContentElement().size!.width, kDraggingListWidth);
|
||||
expect(getListWidth(), kDraggingListWidth);
|
||||
|
||||
// Move it
|
||||
await drag.moveTo(tester.getCenter(find.text('Last item')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(getContentElement().size!.width, kDraggingListWidth);
|
||||
expect(getListWidth(), kDraggingListWidth);
|
||||
|
||||
// Drop it
|
||||
await drag.up();
|
||||
await tester.pumpAndSettle();
|
||||
expect(getContentElement().size!.width, kDraggingListWidth);
|
||||
expect(getListWidth(), kDraggingListWidth);
|
||||
|
||||
// Drag a tall item
|
||||
drag = await tester.startGesture(tester.getCenter(find.text('Tall item')));
|
||||
await tester.pump(kLongPressTimeout + kPressTimeout);
|
||||
await tester.pumpAndSettle();
|
||||
expect(getContentElement().size!.width, kDraggingListWidth);
|
||||
expect(getListWidth(), kDraggingListWidth);
|
||||
// Move it
|
||||
await drag.moveTo(tester.getCenter(find.text('Last item')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(getContentElement().size!.width, kDraggingListWidth);
|
||||
expect(getListWidth(), kDraggingListWidth);
|
||||
|
||||
// Drop it
|
||||
await drag.up();
|
||||
await tester.pumpAndSettle();
|
||||
expect(getContentElement().size!.width, kDraggingListWidth);
|
||||
expect(getListWidth(), kDraggingListWidth);
|
||||
});
|
||||
|
||||
testWidgets('Horizontal drop area golden', (WidgetTester tester) async {
|
||||
@@ -1171,7 +1175,7 @@ void main() {
|
||||
await tester.pumpWidget(MaterialApp(
|
||||
home: reorderableListView,
|
||||
));
|
||||
expect(tester.getCenter(find.text('A')).dy, lessThan(tester.getCenter(find.text('B')).dy));
|
||||
expect(tester.getCenter(find.text('A')).dy, greaterThan(tester.getCenter(find.text('B')).dy));
|
||||
});
|
||||
|
||||
testWidgets('Animation test when placing an item in place', (WidgetTester tester) async {
|
||||
|
||||
198
packages/flutter/test/widgets/reorderable_list_test.dart
Normal file
198
packages/flutter/test/widgets/reorderable_list_test.dart
Normal file
@@ -0,0 +1,198 @@
|
||||
// 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_test/flutter_test.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('SliverReorderableList, drag and drop, fixed height items', (WidgetTester tester) async {
|
||||
final List<int> items = List<int>.generate(8, (int index) => index);
|
||||
|
||||
Future<void> pressDragRelease(Offset start, Offset delta) async {
|
||||
final TestGesture drag = await tester.startGesture(start);
|
||||
await tester.pump(kPressTimeout);
|
||||
await drag.moveBy(delta);
|
||||
await tester.pump(kPressTimeout);
|
||||
await drag.up();
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
void check({ List<int> visible = const <int>[], List<int> hidden = const <int>[] }) {
|
||||
for (final int i in visible) {
|
||||
expect(find.text('item $i'), findsOneWidget);
|
||||
}
|
||||
for (final int i in hidden) {
|
||||
expect(find.text('item $i'), findsNothing);
|
||||
}
|
||||
}
|
||||
|
||||
// The SliverReorderableList is 800x600, 8 items, each item is 800x100 with
|
||||
// an "item $index" text widget at the item's origin. Drags are initiated by
|
||||
// a simple press on the text widget.
|
||||
await tester.pumpWidget(TestList(items: items));
|
||||
check(visible: <int>[0, 1, 2, 3, 4, 5], hidden: <int>[6, 7]);
|
||||
|
||||
// Drag item 0 downwards less than halfway and let it snap back. List
|
||||
// should remain as it is.
|
||||
await pressDragRelease(const Offset(12, 50), const Offset(12, 60));
|
||||
check(visible: <int>[0, 1, 2, 3, 4, 5], hidden: <int>[6, 7]);
|
||||
expect(tester.getTopLeft(find.text('item 0')), Offset.zero);
|
||||
expect(tester.getTopLeft(find.text('item 1')), const Offset(0, 100));
|
||||
expect(items, orderedEquals(<int>[0, 1, 2, 3, 4, 5, 6, 7]));
|
||||
|
||||
// Drag item 0 downwards more than halfway to displace item 1.
|
||||
await pressDragRelease(tester.getCenter(find.text('item 0')), const Offset(0, 151));
|
||||
check(visible: <int>[0, 1, 2, 3, 4, 5], hidden: <int>[6, 7]);
|
||||
expect(tester.getTopLeft(find.text('item 1')), Offset.zero);
|
||||
expect(tester.getTopLeft(find.text('item 0')), const Offset(0, 100));
|
||||
expect(items, orderedEquals(<int>[1, 0, 2, 3, 4, 5, 6, 7]));
|
||||
|
||||
// Drag item 0 back to where it was.
|
||||
await pressDragRelease(tester.getCenter(find.text('item 0')), const Offset(0, -51));
|
||||
check(visible: <int>[0, 1, 2, 3, 4, 5], hidden: <int>[6, 7]);
|
||||
expect(tester.getTopLeft(find.text('item 0')), Offset.zero);
|
||||
expect(tester.getTopLeft(find.text('item 1')), const Offset(0, 100));
|
||||
expect(items, orderedEquals(<int>[0, 1, 2, 3, 4, 5, 6, 7]));
|
||||
|
||||
// Drag item 1 to item 3
|
||||
await pressDragRelease(tester.getCenter(find.text('item 1')), const Offset(0, 251));
|
||||
check(visible: <int>[0, 1, 2, 3, 4, 5], hidden: <int>[6, 7]);
|
||||
expect(tester.getTopLeft(find.text('item 0')), Offset.zero);
|
||||
expect(tester.getTopLeft(find.text('item 1')), const Offset(0, 300));
|
||||
expect(tester.getTopLeft(find.text('item 3')), const Offset(0, 200));
|
||||
expect(items, orderedEquals(<int>[0, 2, 3, 1, 4, 5, 6, 7]));
|
||||
|
||||
// Drag item 1 back to where it was
|
||||
await pressDragRelease(tester.getCenter(find.text('item 1')), const Offset(0, -200));
|
||||
check(visible: <int>[0, 1, 2, 3, 4, 5], hidden: <int>[6, 7]);
|
||||
expect(tester.getTopLeft(find.text('item 0')), Offset.zero);
|
||||
expect(tester.getTopLeft(find.text('item 1')), const Offset(0, 100));
|
||||
expect(tester.getTopLeft(find.text('item 3')), const Offset(0, 300));
|
||||
expect(items, orderedEquals(<int>[0, 1, 2, 3, 4, 5, 6, 7]));
|
||||
});
|
||||
|
||||
testWidgets('SliverReorderableList, items inherit DefaultTextStyle, IconTheme', (WidgetTester tester) async {
|
||||
const Color textColor = Color(0xffffffff);
|
||||
const Color iconColor = Color(0xff0000ff);
|
||||
|
||||
TextStyle getIconStyle() {
|
||||
return tester.widget<RichText>(
|
||||
find.descendant(
|
||||
of: find.byType(Icon),
|
||||
matching: find.byType(RichText),
|
||||
),
|
||||
).text.style!;
|
||||
}
|
||||
|
||||
TextStyle getTextStyle() {
|
||||
return tester.widget<RichText>(
|
||||
find.descendant(
|
||||
of: find.text('item 0'),
|
||||
matching: find.byType(RichText),
|
||||
),
|
||||
).text.style!;
|
||||
}
|
||||
|
||||
// This SliverReorderableList has just one item: "item 0".
|
||||
await tester.pumpWidget(
|
||||
TestList(
|
||||
items: List<int>.from(<int>[0]),
|
||||
textColor: textColor,
|
||||
iconColor: iconColor,
|
||||
),
|
||||
);
|
||||
expect(tester.getTopLeft(find.text('item 0')), Offset.zero);
|
||||
expect(getIconStyle().color, iconColor);
|
||||
expect(getTextStyle().color, textColor);
|
||||
|
||||
// Dragging item 0 causes it to be reparented in the overlay. The item
|
||||
// should still inherit the IconTheme and DefaultTextStyle because they are
|
||||
// InheritedThemes.
|
||||
final TestGesture drag = await tester.startGesture(tester.getCenter(find.text('item 0')));
|
||||
await tester.pump(kPressTimeout);
|
||||
await drag.moveBy(const Offset(0, 50));
|
||||
await tester.pump(kPressTimeout);
|
||||
expect(tester.getTopLeft(find.text('item 0')), const Offset(0, 50));
|
||||
expect(getIconStyle().color, iconColor);
|
||||
expect(getTextStyle().color, textColor);
|
||||
|
||||
// Drag is complete, item 0 returns to where it was.
|
||||
await drag.up();
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.getTopLeft(find.text('item 0')), Offset.zero);
|
||||
expect(getIconStyle().color, iconColor);
|
||||
expect(getTextStyle().color, textColor);
|
||||
});
|
||||
}
|
||||
|
||||
class TestList extends StatefulWidget {
|
||||
const TestList({
|
||||
Key? key,
|
||||
this.textColor,
|
||||
this.iconColor,
|
||||
required this.items,
|
||||
}) : super(key: key);
|
||||
|
||||
final List<int> items;
|
||||
final Color? textColor;
|
||||
final Color? iconColor;
|
||||
|
||||
@override
|
||||
_TestListState createState() => _TestListState();
|
||||
}
|
||||
|
||||
class _TestListState extends State<TestList> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
home: Scaffold(
|
||||
body: DefaultTextStyle(
|
||||
style: TextStyle(color: widget.textColor),
|
||||
child: IconTheme(
|
||||
data: IconThemeData(color: widget.iconColor),
|
||||
child: StatefulBuilder(
|
||||
builder: (BuildContext outerContext, StateSetter setState) {
|
||||
final List<int> items = widget.items;
|
||||
return CustomScrollView(
|
||||
slivers: <Widget>[
|
||||
SliverReorderableList(
|
||||
itemBuilder: (BuildContext context, int index) {
|
||||
return Container(
|
||||
key: ValueKey<int>(items[index]),
|
||||
height: 100,
|
||||
color: items[index].isOdd ? Colors.red : Colors.green,
|
||||
child: ReorderableDragStartListener(
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
Text('item ${items[index]}'),
|
||||
const Icon(Icons.drag_handle),
|
||||
],
|
||||
),
|
||||
index: index,
|
||||
),
|
||||
);
|
||||
},
|
||||
itemCount: items.length,
|
||||
onReorder: (int fromIndex, int toIndex) {
|
||||
setState(() {
|
||||
if (toIndex > fromIndex) {
|
||||
toIndex -= 1;
|
||||
}
|
||||
items.insert(toIndex, items.removeAt(fromIndex));
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user