|
| 1 | +import 'dart:math' as math; |
| 2 | + |
| 3 | +import 'package:flutter/foundation.dart'; |
| 4 | +import 'package:flutter/widgets.dart'; |
| 5 | +import 'package:flutter_omarchy/src/theme/theme.dart'; |
| 6 | +import 'package:flutter_omarchy/src/widgets/icon_data.g.dart'; |
| 7 | +import 'package:flutter_omarchy/src/widgets/utils/default_foreground.dart'; |
| 8 | +import 'package:flutter_omarchy/src/widgets/utils/fade_in.dart'; |
| 9 | +import 'package:flutter_omarchy/src/widgets/utils/grouping.dart'; |
| 10 | +import 'package:flutter_omarchy/src/widgets/utils/pointer_area.dart'; |
| 11 | + |
| 12 | +class OmarchyTreeController<T> extends ChangeNotifier { |
| 13 | + OmarchyTreeController({Set<T>? expandedNodes}) |
| 14 | + : _expandedNodes = expandedNodes ?? <T>{}; |
| 15 | + |
| 16 | + final Set<T> _expandedNodes; |
| 17 | + |
| 18 | + void expand(OmarchyTreeNode<T> node) { |
| 19 | + if (_expandedNodes.add(node.id)) { |
| 20 | + notifyListeners(); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + void collapse(OmarchyTreeNode<T> node) { |
| 25 | + if (_expandedNodes.remove(node.id)) { |
| 26 | + notifyListeners(); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + bool isExpanded(OmarchyTreeNode<T> node) { |
| 31 | + return _expandedNodes.contains(node.id); |
| 32 | + } |
| 33 | + |
| 34 | + void toggle(OmarchyTreeNode<T> node) { |
| 35 | + if (isExpanded(node)) { |
| 36 | + collapse(node); |
| 37 | + } else { |
| 38 | + expand(node); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + void collapseAll() { |
| 43 | + if (_expandedNodes.isNotEmpty) { |
| 44 | + _expandedNodes.clear(); |
| 45 | + notifyListeners(); |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +class OmarchyTreeScope<T> extends InheritedModel<T> { |
| 51 | + OmarchyTreeScope({ |
| 52 | + super.key, |
| 53 | + required this.controller, |
| 54 | + required Set<T> selectedItems, |
| 55 | + required super.child, |
| 56 | + }) : selectedItems = Set.unmodifiable(selectedItems); |
| 57 | + |
| 58 | + final OmarchyTreeController<T>? controller; |
| 59 | + final Set<T> selectedItems; |
| 60 | + |
| 61 | + static OmarchyTreeController<T>? maybeOf<T>(BuildContext context) { |
| 62 | + final widget = context |
| 63 | + .dependOnInheritedWidgetOfExactType<OmarchyTreeScope<T>>(); |
| 64 | + return widget?.controller; |
| 65 | + } |
| 66 | + |
| 67 | + static bool? isExpanded<T>(BuildContext context, T id) { |
| 68 | + final controller = context |
| 69 | + .dependOnInheritedWidgetOfExactType<OmarchyTreeScope<T>>(aspect: id); |
| 70 | + if (controller == null) { |
| 71 | + return null; |
| 72 | + } |
| 73 | + return controller.selectedItems.contains(id); |
| 74 | + } |
| 75 | + |
| 76 | + @override |
| 77 | + bool updateShouldNotify(covariant OmarchyTreeScope<T> oldWidget) { |
| 78 | + if (oldWidget.controller != controller) return true; |
| 79 | + return !setEquals(oldWidget.selectedItems, selectedItems); |
| 80 | + } |
| 81 | + |
| 82 | + @override |
| 83 | + bool updateShouldNotifyDependent( |
| 84 | + covariant OmarchyTreeScope<T> oldWidget, |
| 85 | + Set<T> dependencies, |
| 86 | + ) { |
| 87 | + if (oldWidget.controller != controller) return true; |
| 88 | + |
| 89 | + for (final dependency in dependencies) { |
| 90 | + final wasSelected = oldWidget.selectedItems.contains(dependency); |
| 91 | + final isSelected = selectedItems.contains(dependency); |
| 92 | + if (wasSelected != isSelected) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + } |
| 96 | + return false; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +class OmarchyTree<T> extends StatelessWidget { |
| 101 | + const OmarchyTree({super.key, required this.children, this.controller}); |
| 102 | + |
| 103 | + final OmarchyTreeController<T>? controller; |
| 104 | + final List<OmarchyTreeNode<T>> children; |
| 105 | + |
| 106 | + @override |
| 107 | + Widget build(BuildContext context) { |
| 108 | + final result = CustomScrollView( |
| 109 | + slivers: [ |
| 110 | + for (var i = 0; i < children.length; i++) |
| 111 | + Grouping( |
| 112 | + position: (i, children.length), |
| 113 | + child: OmarchyTreeNodeSliver(node: children[i]), |
| 114 | + ), |
| 115 | + ], |
| 116 | + ); |
| 117 | + if (controller != null) { |
| 118 | + return AnimatedBuilder( |
| 119 | + animation: controller!, |
| 120 | + builder: (context, _) { |
| 121 | + return OmarchyTreeScope<T>( |
| 122 | + controller: controller, |
| 123 | + selectedItems: controller!._expandedNodes, |
| 124 | + child: result, |
| 125 | + ); |
| 126 | + }, |
| 127 | + ); |
| 128 | + } |
| 129 | + return result; |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +class OmarchyTreeNodeSliver<T> extends StatelessWidget { |
| 134 | + const OmarchyTreeNodeSliver({ |
| 135 | + super.key, |
| 136 | + required this.node, |
| 137 | + this.fadeIn = false, |
| 138 | + }); |
| 139 | + |
| 140 | + final OmarchyTreeNode<T> node; |
| 141 | + final bool fadeIn; |
| 142 | + |
| 143 | + @override |
| 144 | + Widget build(BuildContext context) { |
| 145 | + final theme = OmarchyTheme.of(context); |
| 146 | + final isExpanded = |
| 147 | + node.isExpanded ?? |
| 148 | + OmarchyTreeScope.isExpanded<T>(context, node.id) == true; |
| 149 | + return SliverMainAxisGroup( |
| 150 | + slivers: [ |
| 151 | + SliverToBoxAdapter(child: fadeIn ? FadeIn(child: node) : node), |
| 152 | + if (isExpanded) |
| 153 | + SliverPadding( |
| 154 | + padding: const EdgeInsets.only(left: 6.0), |
| 155 | + sliver: DecoratedSliver( |
| 156 | + decoration: BoxDecoration( |
| 157 | + border: Border( |
| 158 | + left: BorderSide(color: theme.colors.normal.black, width: 1), |
| 159 | + ), |
| 160 | + ), |
| 161 | + sliver: SliverPadding( |
| 162 | + padding: const EdgeInsets.only(left: 12.0), |
| 163 | + sliver: SliverMainAxisGroup( |
| 164 | + slivers: [ |
| 165 | + for (var i = 0; i < node.children.length; i++) |
| 166 | + Grouping( |
| 167 | + position: (i, node.children.length), |
| 168 | + child: OmarchyTreeNodeSliver( |
| 169 | + fadeIn: true, |
| 170 | + node: node.children[i], |
| 171 | + ), |
| 172 | + ), |
| 173 | + ], |
| 174 | + ), |
| 175 | + ), |
| 176 | + ), |
| 177 | + ), |
| 178 | + ], |
| 179 | + ); |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +class OmarchyTreeNode<T> extends StatelessWidget { |
| 184 | + const OmarchyTreeNode({ |
| 185 | + super.key, |
| 186 | + required this.id, |
| 187 | + this.children = const [], |
| 188 | + this.isExpanded, |
| 189 | + this.icon, |
| 190 | + this.title, |
| 191 | + this.trailing, |
| 192 | + this.onTap, |
| 193 | + }); |
| 194 | + |
| 195 | + final T id; |
| 196 | + final bool? isExpanded; |
| 197 | + final Widget? title; |
| 198 | + final Widget? trailing; |
| 199 | + final Widget? icon; |
| 200 | + final List<OmarchyTreeNode<T>> children; |
| 201 | + final VoidCallback? onTap; |
| 202 | + |
| 203 | + @override |
| 204 | + Widget build(BuildContext context) { |
| 205 | + final theme = OmarchyTheme.of(context); |
| 206 | + final icon = this.icon; |
| 207 | + final title = this.title; |
| 208 | + final trailing = this.trailing; |
| 209 | + final isExpanded = |
| 210 | + this.isExpanded ?? OmarchyTreeScope.isExpanded(context, id) == true; |
| 211 | + return PointerArea( |
| 212 | + onTap: () { |
| 213 | + final controller = OmarchyTreeScope.maybeOf<T>(context); |
| 214 | + if (controller != null) { |
| 215 | + controller.toggle(this); |
| 216 | + } |
| 217 | + onTap?.call(); |
| 218 | + }, |
| 219 | + builder: (context, state, _) => Padding( |
| 220 | + padding: const EdgeInsets.symmetric(vertical: 4.0), |
| 221 | + child: Row( |
| 222 | + spacing: 4, |
| 223 | + children: [ |
| 224 | + if (children.isNotEmpty) |
| 225 | + AnimatedContainer( |
| 226 | + duration: const Duration(milliseconds: 200), |
| 227 | + transform: Matrix4.rotationZ(!isExpanded ? 0 : math.pi / 2), |
| 228 | + transformAlignment: Alignment.center, |
| 229 | + child: Icon( |
| 230 | + OmarchyIcons.codChevronRight, |
| 231 | + size: 12, |
| 232 | + color: switch (state) { |
| 233 | + _ when isExpanded => theme.colors.normal.blue, |
| 234 | + PointerState(isPressed: true) => theme.colors.normal.blue, |
| 235 | + PointerState(isHovering: true) => theme.colors.normal.white, |
| 236 | + _ => theme.colors.normal.black, |
| 237 | + }, |
| 238 | + ), |
| 239 | + ) |
| 240 | + else |
| 241 | + SizedBox(width: 12, height: 12), |
| 242 | + if (icon != null) |
| 243 | + IconTheme( |
| 244 | + data: IconThemeData(color: theme.colors.normal.blue, size: 18), |
| 245 | + child: icon, |
| 246 | + ), |
| 247 | + if (title != null) |
| 248 | + DefaultForeground( |
| 249 | + foreground: switch (state) { |
| 250 | + PointerState(isPressed: true) => theme.colors.bright.white, |
| 251 | + PointerState(isHovering: true) => theme.colors.selectedText, |
| 252 | + _ => theme.colors.bright.white, |
| 253 | + }, |
| 254 | + textStyle: theme.text.normal, |
| 255 | + child: title, |
| 256 | + ), |
| 257 | + const Spacer(), |
| 258 | + if (trailing != null) |
| 259 | + DefaultForeground( |
| 260 | + foreground: switch (state) { |
| 261 | + PointerState( |
| 262 | + isEnabled: true, |
| 263 | + isPressed: false, |
| 264 | + isHovering: true, |
| 265 | + ) => |
| 266 | + theme.colors.normal.white, |
| 267 | + _ => theme.colors.normal.black, |
| 268 | + }, |
| 269 | + textStyle: theme.text.italic, |
| 270 | + child: trailing, |
| 271 | + ), |
| 272 | + ], |
| 273 | + ), |
| 274 | + ), |
| 275 | + ); |
| 276 | + } |
| 277 | +} |
0 commit comments