-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[material_ui] Add ReorderableListView.separated constructor #12779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
krolmic
wants to merge
1
commit into
flutter:main
Choose a base branch
from
krolmic:material-ui-reorderable-list-separated
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
packages/material_ui/example/lib/reorderable_list/reorderable_list_view.separated.0.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| // #region body | ||
| import 'package:material_ui/material_ui.dart'; | ||
|
|
||
| /// Flutter code sample for [ReorderableListView.separated]. | ||
|
|
||
| void main() => runApp(const ReorderableApp()); | ||
|
|
||
| class ReorderableApp extends StatelessWidget { | ||
| const ReorderableApp({super.key}); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return MaterialApp( | ||
| home: Scaffold( | ||
| appBar: AppBar( | ||
| title: const Text('ReorderableListView.separated Sample'), | ||
| ), | ||
| body: const ReorderableExample(), | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| class ReorderableExample extends StatefulWidget { | ||
| const ReorderableExample({super.key}); | ||
|
|
||
| @override | ||
| State<ReorderableExample> createState() => _ReorderableExampleState(); | ||
| } | ||
|
|
||
| class _ReorderableExampleState extends State<ReorderableExample> { | ||
| final List<int> _items = List<int>.generate(20, (int index) => index); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| final ColorScheme colorScheme = Theme.of(context).colorScheme; | ||
| final Color oddItemColor = colorScheme.primary.withValues(alpha: 0.05); | ||
| final Color evenItemColor = colorScheme.primary.withValues(alpha: 0.15); | ||
|
|
||
| return ReorderableListView.separated( | ||
| padding: const .symmetric(horizontal: 40.0), | ||
| itemCount: _items.length, | ||
| itemBuilder: (BuildContext context, int index) { | ||
| // ListTile.tileColor is painted by the nearest Material ancestor, so | ||
| // each tile brings its own Material: without it the color would be | ||
| // painted by the Scaffold's Material and would not move with the tile | ||
| // during a reorder drag (per the ListTile documentation). | ||
| return Material( | ||
| // Key each tile by its item rather than by its position, so a tile's | ||
| // identity follows the item it shows when the order changes. | ||
| key: ValueKey<int>(_items[index]), | ||
| child: ListTile( | ||
| tileColor: _items[index].isOdd ? oddItemColor : evenItemColor, | ||
| title: Text('Item ${_items[index]}'), | ||
| ), | ||
| ); | ||
| }, | ||
| // The separator index is a boundary index: separator `index` sits between | ||
| // the items built for `index` and `index + 1`, so it describes a position | ||
| // in the list rather than a particular item. The thick dividers therefore | ||
| // stay on the even boundaries no matter how the items are reordered, and | ||
| // every divider stays visible while an item is being dragged. | ||
| separatorBuilder: (BuildContext context, int index) { | ||
| return Divider( | ||
| height: 8.0, | ||
| thickness: index.isEven ? 4.0 : 1.0, | ||
| color: index.isEven | ||
| ? colorScheme.primary | ||
| : colorScheme.outlineVariant, | ||
| ); | ||
| }, | ||
| onReorderItem: (int oldIndex, int newIndex) { | ||
| setState(() { | ||
| final int item = _items.removeAt(oldIndex); | ||
| _items.insert(newIndex, item); | ||
| }); | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // #endregion body | ||
86 changes: 86 additions & 0 deletions
86
...ges/material_ui/example/test/reorderable_list/reorderable_list_view.separated.0_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // 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:material_ui/material_ui.dart'; | ||
| import 'package:material_ui_examples/reorderable_list/reorderable_list_view.separated.0.dart' | ||
| as example; | ||
|
|
||
| void main() { | ||
| testWidgets('Example separates each pair of items with a divider', ( | ||
| WidgetTester tester, | ||
| ) async { | ||
| await tester.pumpWidget(const example.ReorderableApp()); | ||
|
|
||
| expect(find.text('Item 0'), findsOneWidget); | ||
| expect(find.text('Item 1'), findsOneWidget); | ||
| expect(find.byType(Divider), findsAtLeast(2)); | ||
|
|
||
| // Separator 0 is the boundary between items 0 and 1, so it occupies the gap | ||
| // between the two tiles rather than any space inside either of them. | ||
| final Finder firstItem = find.ancestor( | ||
| of: find.text('Item 0'), | ||
| matching: find.byType(ListTile), | ||
| ); | ||
| final Finder secondItem = find.ancestor( | ||
| of: find.text('Item 1'), | ||
| matching: find.byType(ListTile), | ||
| ); | ||
| final double firstSeparatorCenter = tester | ||
| .getCenter(find.byType(Divider).first) | ||
| .dy; | ||
| expect( | ||
| firstSeparatorCenter, | ||
| greaterThan(tester.getBottomLeft(firstItem).dy), | ||
| ); | ||
| expect(firstSeparatorCenter, lessThan(tester.getTopLeft(secondItem).dy)); | ||
| }); | ||
|
|
||
| testWidgets( | ||
| 'Example gives each tile its own Material, so its background moves with it during a drag', | ||
| (WidgetTester tester) async { | ||
| await tester.pumpWidget(const example.ReorderableApp()); | ||
|
|
||
| // ListTile.tileColor is painted by the tile's nearest ancestor Material. | ||
| // Each tile must bring its own (the wrapper carrying the item's key): | ||
| // with a bare ListTile the nearest Material is the Scaffold's, and the | ||
| // background would stay put while the tile moves during a reorder drag. | ||
| final List<Element> tiles = find.byType(ListTile).evaluate().toList(); | ||
| expect(tiles, isNotEmpty); | ||
| for (final Element tile in tiles) { | ||
| final Text title = (tile.widget as ListTile).title! as Text; | ||
| final int item = int.parse(title.data!.split(' ').last); | ||
| final Material? material = tile | ||
| .findAncestorWidgetOfExactType<Material>(); | ||
| expect( | ||
| material?.key, | ||
| ValueKey<int>(item), | ||
| reason: | ||
| 'the Material painting "${title.data}" must be its own wrapper', | ||
| ); | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| testWidgets('Example thickens the separators on even boundaries', ( | ||
| WidgetTester tester, | ||
| ) async { | ||
| await tester.pumpWidget(const example.ReorderableApp()); | ||
|
|
||
| final List<Divider> separators = tester | ||
| .widgetList<Divider>(find.byType(Divider)) | ||
| .toList(); | ||
| expect(separators.length, greaterThan(1)); | ||
|
|
||
| // The list starts unscrolled, so the separators onstage are boundaries 0, 1, | ||
| // 2, ... in order, and each one is built from its own boundary index. | ||
| for (int i = 0; i < separators.length; i += 1) { | ||
| expect( | ||
| separators[i].thickness, | ||
| i.isEven ? 4.0 : 1.0, | ||
| reason: 'separator at boundary $i', | ||
| ); | ||
| } | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a syntax error here:
.symmetricis missing theEdgeInsetsclass name. It should beEdgeInsets.symmetric.