|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:flutter/foundation.dart'; |
| 6 | +import 'package:flutter/material.dart'; |
| 7 | + |
| 8 | +class DragScreen extends StatefulWidget { |
| 9 | + const DragScreen({super.key}); |
| 10 | + |
| 11 | + @override |
| 12 | + State<DragScreen> createState() => _DragScreenState(); |
| 13 | +} |
| 14 | + |
| 15 | +class _DragScreenState extends State<DragScreen> { |
| 16 | + List<int> items = <int>[1, 2, 3, 4, 5]; |
| 17 | + |
| 18 | + // Target order for Maestro drag-and-drop test validation. |
| 19 | + static const List<int> targetOrder = <int>[3, 1, 4, 5, 2]; |
| 20 | + |
| 21 | + bool get isCorrectOrder => listEquals(items, targetOrder); |
| 22 | + |
| 23 | + void _onReorder(int oldIndex, int newIndex) { |
| 24 | + setState(() { |
| 25 | + if (newIndex > oldIndex) { |
| 26 | + newIndex -= 1; |
| 27 | + } |
| 28 | + final int item = items.removeAt(oldIndex); |
| 29 | + items.insert(newIndex, item); |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + @override |
| 34 | + Widget build(BuildContext context) { |
| 35 | + return Scaffold( |
| 36 | + appBar: AppBar( |
| 37 | + title: const Text('Drag Test'), |
| 38 | + leading: IconButton( |
| 39 | + icon: const Icon(Icons.arrow_back), |
| 40 | + onPressed: () => Navigator.of(context).pop(), |
| 41 | + ), |
| 42 | + ), |
| 43 | + body: Column( |
| 44 | + children: <Widget>[ |
| 45 | + Padding( |
| 46 | + padding: const EdgeInsets.all(16.0), |
| 47 | + child: Text( |
| 48 | + 'Reorder items to: ${targetOrder.join(", ")}', |
| 49 | + style: Theme.of(context).textTheme.titleMedium, |
| 50 | + ), |
| 51 | + ), |
| 52 | + Padding( |
| 53 | + padding: const EdgeInsets.symmetric(horizontal: 16.0), |
| 54 | + child: Text( |
| 55 | + 'Current order: ${items.join(", ")}', |
| 56 | + style: Theme.of(context).textTheme.bodyMedium, |
| 57 | + ), |
| 58 | + ), |
| 59 | + const SizedBox(height: 16), |
| 60 | + Expanded( |
| 61 | + child: ReorderableListView.builder( |
| 62 | + itemCount: items.length, |
| 63 | + onReorderItem: _onReorder, |
| 64 | + buildDefaultDragHandles: false, |
| 65 | + proxyDecorator: (Widget child, int index, Animation<double> animation) { |
| 66 | + return Material(elevation: 4, color: Colors.transparent, child: child); |
| 67 | + }, |
| 68 | + itemBuilder: (BuildContext context, int index) { |
| 69 | + final int item = items[index]; |
| 70 | + return ReorderableDragStartListener( |
| 71 | + key: ValueKey<int>(item), |
| 72 | + index: index, |
| 73 | + child: Container( |
| 74 | + margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 4), |
| 75 | + child: ListTile( |
| 76 | + leading: const Icon(Icons.drag_handle), |
| 77 | + title: Text( |
| 78 | + 'Item $item', |
| 79 | + style: const TextStyle(fontSize: 18), |
| 80 | + semanticsLabel: 'Item $item', |
| 81 | + ), |
| 82 | + ), |
| 83 | + ), |
| 84 | + ); |
| 85 | + }, |
| 86 | + ), |
| 87 | + ), |
| 88 | + if (isCorrectOrder) |
| 89 | + Container( |
| 90 | + padding: const EdgeInsets.all(24), |
| 91 | + color: Colors.green, |
| 92 | + width: double.infinity, |
| 93 | + child: const Text( |
| 94 | + 'Drag Success', |
| 95 | + textAlign: TextAlign.center, |
| 96 | + style: TextStyle(color: Colors.white, fontSize: 24, fontWeight: FontWeight.bold), |
| 97 | + ), |
| 98 | + ), |
| 99 | + ], |
| 100 | + ), |
| 101 | + ); |
| 102 | + } |
| 103 | +} |
0 commit comments