layers_panel.dart (8212B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | import 'dart:io'; import 'package:flutter/material.dart'; import 'package:creekui/data/models/canvas_models.dart'; class LayersPanel extends StatefulWidget { final List<CanvasLayer> layers; final String? activeLayerId; final bool isOpen; final VoidCallback onToggle; final Function(int oldIndex, int newIndex) onReorder; final Function(String id) onDelete; final Function(String id) onToggleVisibility; final Function(String id) onLayerTap; final VoidCallback? onAddLayer; const LayersPanel({ Key? key, required this.layers, required this.activeLayerId, required this.isOpen, required this.onToggle, required this.onReorder, required this.onDelete, required this.onToggleVisibility, required this.onLayerTap, this.onAddLayer, }) : super(key: key); @override State<LayersPanel> createState() => _LayersPanelState(); } class _LayersPanelState extends State<LayersPanel> { @override Widget build(BuildContext context) { // Reverse list for UI so Top Layer is index 0 final displayLayers = widget.layers.reversed.toList(); return AnimatedContainer( duration: const Duration(milliseconds: 300), curve: Curves.easeOutCubic, width: widget.isOpen ? 260 : 56, // Use constrained height calculation based on state height: widget.isOpen ? 400 : 56, decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(16), boxShadow: [ BoxShadow( color: Colors.black.withOpacity(0.15), blurRadius: 10, offset: const Offset(0, 4), ), ], ), clipBehavior: Clip.antiAlias, child: Material( color: Colors.transparent, child: widget.isOpen ? _buildPanelContent(displayLayers) : _buildCollapsedButton(), ), ); } Widget _buildCollapsedButton() { return InkWell( onTap: widget.onToggle, borderRadius: BorderRadius.circular(16), child: const Center( child: Icon(Icons.layers_outlined, color: Colors.black), ), ); } Widget _buildPanelContent(List<CanvasLayer> displayLayers) { return AnimatedSize( duration: const Duration(milliseconds: 200), curve: Curves.easeOutCubic, alignment: Alignment.topRight, child: Container( constraints: const BoxConstraints(maxHeight: 400), child: Column( mainAxisSize: MainAxisSize.min, // Shrink to fit content crossAxisAlignment: CrossAxisAlignment.start, children: [ // Header Padding( padding: const EdgeInsets.all(12), child: Row( children: [ // Add Layer Button if (widget.onAddLayer != null) ...[ InkWell( onTap: widget.onAddLayer, borderRadius: BorderRadius.circular(8), child: Container( padding: const EdgeInsets.all(6), decoration: BoxDecoration( color: Colors.blue.shade50, borderRadius: BorderRadius.circular(8), ), child: const Icon( Icons.add, size: 20, color: Colors.blue, ), ), ), ], const Spacer(), // Layers Icon InkWell( onTap: widget.onToggle, borderRadius: BorderRadius.circular(8), child: Container( padding: const EdgeInsets.all(6), decoration: BoxDecoration( color: Colors.grey.shade100, borderRadius: BorderRadius.circular(8), ), child: const Icon( Icons.layers, size: 20, color: Colors.black87, ), ), ), ], ), ), const Divider(height: 1, thickness: 0.5), // List Flexible( child: ReorderableListView.builder( shrinkWrap: true, padding: EdgeInsets.zero, buildDefaultDragHandles: false, itemCount: displayLayers.length, onReorder: widget.onReorder, itemBuilder: (context, index) { final layer = displayLayers[index]; return _buildLayerItem(context, layer, index); }, ), ), ], ), ), ); } Widget _buildLayerItem(BuildContext context, CanvasLayer layer, int index) { final bool isActive = layer.id == widget.activeLayerId; Widget preview; String label = ""; if (layer is ImageLayer) { final type = layer.data['type']; final content = layer.data['content']; if (type == 'text') { preview = const Icon( Icons.text_fields, size: 18, color: Colors.black54, ); label = content.toString(); } else { preview = Container( width: 28, height: 28, decoration: BoxDecoration( borderRadius: BorderRadius.circular(4), border: Border.all(color: Colors.grey[300]!), image: DecorationImage( image: FileImage(File(content)), fit: BoxFit.cover, ), ), ); label = "Image"; } } else if (layer is SketchLayer) { preview = const Icon(Icons.brush, size: 18, color: Colors.purpleAccent); label = layer.isMagicDraw ? "Magic Draw" : "Sketch"; } else { preview = const SizedBox(); } return Dismissible( key: ValueKey(layer.id), direction: DismissDirection.endToStart, onDismissed: (_) => widget.onDelete(layer.id), background: Container( color: Colors.redAccent, alignment: Alignment.centerRight, padding: const EdgeInsets.only(right: 12), child: const Icon(Icons.delete, color: Colors.white, size: 20), ), child: Material( color: isActive ? Colors.grey.withOpacity(0.1) : Colors.transparent, child: InkWell( onTap: () => widget.onLayerTap(layer.id), child: Padding( padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 4), child: Row( children: [ const SizedBox(width: 12), preview, const SizedBox(width: 12), Expanded( child: Text( label, style: const TextStyle(fontSize: 14), maxLines: 1, overflow: TextOverflow.ellipsis, ), ), IconButton( padding: const EdgeInsets.all(4), constraints: const BoxConstraints(), icon: Icon( layer.isVisible ? Icons.visibility : Icons.visibility_off, size: 18, color: layer.isVisible ? Colors.black54 : Colors.grey, ), onPressed: () => widget.onToggleVisibility(layer.id), ), const SizedBox(width: 4), ReorderableDragStartListener( index: index, child: Container( padding: const EdgeInsets.fromLTRB(12, 12, 16, 12), color: Colors.transparent, child: const Icon( Icons.drag_handle, size: 20, color: Colors.grey, ), ), ), ], ), ), ), ), ); } } |