|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart' hide Image; |
| 4 | +import 'package:nativeapi/nativeapi.dart'; |
| 5 | + |
| 6 | +/// Drag and drop with the two widgets of `nativeapi`: |
| 7 | +/// |
| 8 | +/// - the left panel is a [DropRegion]: drop files or text on it; |
| 9 | +/// - the cards on the right are [DragOutArea]s: drag them into a file manager, |
| 10 | +/// an editor, or onto the left panel. |
| 11 | +/// |
| 12 | +/// tools/gui/flutter_drag_drop_test.* in the workspace repository drives this |
| 13 | +/// example by its texts; keep them in sync. |
| 14 | +void main() { |
| 15 | + runApp(const DragDropApp()); |
| 16 | +} |
| 17 | + |
| 18 | +class DragDropApp extends StatelessWidget { |
| 19 | + const DragDropApp({super.key}); |
| 20 | + |
| 21 | + @override |
| 22 | + Widget build(BuildContext context) { |
| 23 | + return MaterialApp( |
| 24 | + debugShowCheckedModeBanner: false, |
| 25 | + theme: ThemeData(colorSchemeSeed: Colors.teal, useMaterial3: true), |
| 26 | + home: const DragDropPage(), |
| 27 | + ); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +class DragDropPage extends StatefulWidget { |
| 32 | + const DragDropPage({super.key}); |
| 33 | + |
| 34 | + @override |
| 35 | + State<DragDropPage> createState() => _DragDropPageState(); |
| 36 | +} |
| 37 | + |
| 38 | +class _DragDropPageState extends State<DragDropPage> { |
| 39 | + late final File _file = _createFile(); |
| 40 | + bool _hovering = false; |
| 41 | + Offset? _hoverPosition; |
| 42 | + int _drops = 0; |
| 43 | + List<String> _droppedFiles = const []; |
| 44 | + String? _droppedText; |
| 45 | + String _lastDragResult = '-'; |
| 46 | + |
| 47 | + static File _createFile() { |
| 48 | + final file = File( |
| 49 | + '${Directory.systemTemp.path}${Platform.pathSeparator}' |
| 50 | + 'nativeapi-drag-drop-note.txt', |
| 51 | + ); |
| 52 | + file.writeAsStringSync('A note dragged out of drag_drop_example.\n'); |
| 53 | + return file; |
| 54 | + } |
| 55 | + |
| 56 | + @override |
| 57 | + void initState() { |
| 58 | + super.initState(); |
| 59 | + final window = WindowManager.instance.getCurrent(); |
| 60 | + if (window == null) return; |
| 61 | + window.title = 'nativeapi · Drag and drop'; |
| 62 | + window.contentSize = const Size(760, 460); |
| 63 | + window.center(); |
| 64 | + } |
| 65 | + |
| 66 | + @override |
| 67 | + Widget build(BuildContext context) { |
| 68 | + final theme = Theme.of(context); |
| 69 | + return Scaffold( |
| 70 | + body: Padding( |
| 71 | + padding: const EdgeInsets.all(16), |
| 72 | + child: Row( |
| 73 | + crossAxisAlignment: CrossAxisAlignment.stretch, |
| 74 | + children: [ |
| 75 | + Expanded(child: _buildDropPanel(theme)), |
| 76 | + const SizedBox(width: 16), |
| 77 | + SizedBox(width: 260, child: _buildDragPanel(theme)), |
| 78 | + ], |
| 79 | + ), |
| 80 | + ), |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + Widget _buildDropPanel(ThemeData theme) { |
| 85 | + final colors = theme.colorScheme; |
| 86 | + return DropRegion( |
| 87 | + onDragEntered: (position) => setState(() { |
| 88 | + _hovering = true; |
| 89 | + _hoverPosition = position; |
| 90 | + }), |
| 91 | + onDragUpdated: (position) => setState(() => _hoverPosition = position), |
| 92 | + onDragExited: () => setState(() { |
| 93 | + _hovering = false; |
| 94 | + _hoverPosition = null; |
| 95 | + }), |
| 96 | + onDropped: (details) => setState(() { |
| 97 | + _hovering = false; |
| 98 | + _hoverPosition = null; |
| 99 | + _drops++; |
| 100 | + _droppedFiles = details.filePaths; |
| 101 | + _droppedText = details.text; |
| 102 | + }), |
| 103 | + child: AnimatedContainer( |
| 104 | + duration: const Duration(milliseconds: 120), |
| 105 | + decoration: BoxDecoration( |
| 106 | + color: _hovering |
| 107 | + ? colors.primaryContainer |
| 108 | + : colors.surfaceContainerHighest, |
| 109 | + borderRadius: BorderRadius.circular(12), |
| 110 | + border: Border.all( |
| 111 | + color: _hovering ? colors.primary : colors.outlineVariant, |
| 112 | + width: 2, |
| 113 | + ), |
| 114 | + ), |
| 115 | + padding: const EdgeInsets.all(16), |
| 116 | + child: Column( |
| 117 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 118 | + children: [ |
| 119 | + Text( |
| 120 | + _hovering ? 'Release to drop' : 'Drop files or text here', |
| 121 | + style: theme.textTheme.titleLarge, |
| 122 | + ), |
| 123 | + const SizedBox(height: 4), |
| 124 | + Text( |
| 125 | + _hoverPosition == null |
| 126 | + ? 'Supported: ${DropRegion.isSupported}' |
| 127 | + : 'At ${_hoverPosition!.dx.round()}, ${_hoverPosition!.dy.round()}', |
| 128 | + style: theme.textTheme.bodySmall, |
| 129 | + ), |
| 130 | + const Divider(height: 24), |
| 131 | + Text('Drops: $_drops', style: theme.textTheme.titleMedium), |
| 132 | + const SizedBox(height: 8), |
| 133 | + Expanded( |
| 134 | + child: ListView( |
| 135 | + children: [ |
| 136 | + for (final path in _droppedFiles) |
| 137 | + ListTile( |
| 138 | + dense: true, |
| 139 | + leading: const Icon(Icons.insert_drive_file_outlined), |
| 140 | + title: Text(path.split(Platform.pathSeparator).last), |
| 141 | + subtitle: Text(path), |
| 142 | + ), |
| 143 | + if (_droppedText != null) |
| 144 | + ListTile( |
| 145 | + dense: true, |
| 146 | + leading: const Icon(Icons.notes), |
| 147 | + title: Text('Text: $_droppedText'), |
| 148 | + ), |
| 149 | + ], |
| 150 | + ), |
| 151 | + ), |
| 152 | + ], |
| 153 | + ), |
| 154 | + ), |
| 155 | + ); |
| 156 | + } |
| 157 | + |
| 158 | + Widget _buildDragPanel(ThemeData theme) { |
| 159 | + return Column( |
| 160 | + crossAxisAlignment: CrossAxisAlignment.stretch, |
| 161 | + children: [ |
| 162 | + Text('Drag out', style: theme.textTheme.titleLarge), |
| 163 | + const SizedBox(height: 12), |
| 164 | + _DragCard( |
| 165 | + icon: Icons.description_outlined, |
| 166 | + title: 'Drag this note', |
| 167 | + subtitle: _file.path.split(Platform.pathSeparator).last, |
| 168 | + child: (card) => DragOutArea( |
| 169 | + filePaths: [_file.path], |
| 170 | + onDragEnded: _onDragEnded, |
| 171 | + child: card, |
| 172 | + ), |
| 173 | + ), |
| 174 | + const SizedBox(height: 12), |
| 175 | + _DragCard( |
| 176 | + icon: Icons.short_text, |
| 177 | + title: 'Drag this text', |
| 178 | + subtitle: 'Hello from nativeapi', |
| 179 | + child: (card) => DragOutArea( |
| 180 | + text: 'Hello from nativeapi', |
| 181 | + onDragEnded: _onDragEnded, |
| 182 | + child: card, |
| 183 | + ), |
| 184 | + ), |
| 185 | + const SizedBox(height: 16), |
| 186 | + Text('Last drag: $_lastDragResult', style: theme.textTheme.bodyMedium), |
| 187 | + const Spacer(), |
| 188 | + Text( |
| 189 | + 'Supported: ${DragOutArea.isSupported}', |
| 190 | + style: theme.textTheme.bodySmall, |
| 191 | + ), |
| 192 | + ], |
| 193 | + ); |
| 194 | + } |
| 195 | + |
| 196 | + void _onDragEnded(DragOperation operation) { |
| 197 | + setState(() => _lastDragResult = operation.name); |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +class _DragCard extends StatelessWidget { |
| 202 | + const _DragCard({ |
| 203 | + required this.icon, |
| 204 | + required this.title, |
| 205 | + required this.subtitle, |
| 206 | + required this.child, |
| 207 | + }); |
| 208 | + |
| 209 | + final IconData icon; |
| 210 | + final String title; |
| 211 | + final String subtitle; |
| 212 | + final Widget Function(Widget card) child; |
| 213 | + |
| 214 | + @override |
| 215 | + Widget build(BuildContext context) { |
| 216 | + return MouseRegion( |
| 217 | + cursor: SystemMouseCursors.grab, |
| 218 | + child: child( |
| 219 | + Card( |
| 220 | + child: ListTile( |
| 221 | + leading: Icon(icon), |
| 222 | + title: Text(title), |
| 223 | + subtitle: Text(subtitle), |
| 224 | + ), |
| 225 | + ), |
| 226 | + ), |
| 227 | + ); |
| 228 | + } |
| 229 | +} |
0 commit comments