Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/widgets/previewer_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ class _JsonPreviewerState extends State<JsonPreviewer> {
),
trailingBuilder: (context, node) => node.isFocused
? Padding(
padding: const EdgeInsets.only(right: 12),
padding: const EdgeInsets.only(right: 6),
child: IconButton(
padding: EdgeInsets.zero,
constraints:
Expand Down
76 changes: 57 additions & 19 deletions packages/json_explorer/lib/src/json_explorer.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
Expand Down Expand Up @@ -262,6 +264,9 @@ class JsonAttribute extends StatelessWidget {
this.maxRootNodeWidth,
}) : super(key: key);

static bool get _isMobile =>
!kIsWeb && (Platform.isIOS || Platform.isAndroid);

@override
Widget build(BuildContext context) {
final searchTerm =
Expand Down Expand Up @@ -290,12 +295,43 @@ class JsonAttribute extends StatelessWidget {
},
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTapDown: _isMobile && !node.isRoot && !hasInteraction
? (_) {
final jsonExplorerStore = Provider.of<JsonExplorerStore>(
context,
listen: false,
);
if (node.isFocused) {
node.focus(isFocused: false);
node.highlight(isHighlighted: false);
} else {
node.focus();
node.highlight();
jsonExplorerStore.unfocusAllExcept(node);
}
}
: null,
onTap: hasInteraction
? () {
if (valueStyle.onTap != null) {
valueStyle.onTap!.call();
} else {
_onTap(context);
if (_isMobile && node.isRoot) {
final jsonExplorerStore = Provider.of<JsonExplorerStore>(
context,
listen: false,
);
if (node.isFocused) {
node.focus(isFocused: false);
node.highlight(isHighlighted: false);
} else {
node.focus();
node.highlight();
jsonExplorerStore.unfocusAllExcept(node);
}
} else {
_onTap(context);
}
}
}
: null,
Expand All @@ -322,10 +358,13 @@ class JsonAttribute extends StatelessWidget {
lineColor: theme.indentationLineColor,
),
if (node.isRoot)
SizedBox(
width: 24,
child: collapsableToggleBuilder?.call(context, node) ??
_defaultCollapsableToggleBuilder(context, node),
GestureDetector(
onTap: () => _onTap(context),
child: SizedBox(
width: 24,
child: collapsableToggleBuilder?.call(context, node) ??
_defaultCollapsableToggleBuilder(context, node),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: spacing),
Expand Down Expand Up @@ -542,21 +581,20 @@ class _PropertyNodeWidget extends StatelessWidget {

final text = valueFormatter?.call(node.value) ?? node.value.toString();

if (!showHighlightedText) {
return SelectableText(text, style: style);
final isMobile = !kIsWeb && (Platform.isIOS || Platform.isAndroid);

if (showHighlightedText) {
return _HighlightedText(
text: text,
highlightedText: searchTerm,
style: style,
primaryMatchStyle: focusedSearchHighlightStyle,
secondaryMatchStyle: searchHighlightStyle,
focusedSearchMatchIndex: context
.select<JsonExplorerStore, int?>(_getFocusedSearchMatchIndex),
);
}

final focusedSearchMatchIndex =
context.select<JsonExplorerStore, int?>(_getFocusedSearchMatchIndex);

return _HighlightedText(
text: text,
highlightedText: searchTerm,
style: style,
primaryMatchStyle: focusedSearchHighlightStyle,
secondaryMatchStyle: searchHighlightStyle,
focusedSearchMatchIndex: focusedSearchMatchIndex,
);
return isMobile ? Text(text, style: style) : SelectableText(text, style: style);
}
}

Expand Down
11 changes: 11 additions & 0 deletions packages/json_explorer/lib/src/json_explorer_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,17 @@ class JsonExplorerStore extends ChangeNotifier {
}
}

/// Unfocuses all nodes except the given [node].
/// [notifyListeners] is called to notify all registered listeners.
void unfocusAllExcept(NodeViewModelState node) {
for (final n in _allNodes) {
if (n != node && n.isFocused) {
n.focus(isFocused: false);
n.highlight(isHighlighted: false);
}
}
}

/// Uses the given [jsonObject] to build the [displayNodes] list.
///
/// If [areAllCollapsed] is true, then all nodes will be collapsed, and
Expand Down