spectre_json 1.1.0
spectre_json: ^1.1.0 copied to clipboard
A beautiful and feature-rich JSON editor widget for Flutter with syntax highlighting, tree view navigation, real-time validation, and customizable themes. Perfect for configuration editors, data visua [...]
Spectre JSON - JSON Editor for Flutter #
A beautiful and feature-rich JSON editor widget for Flutter with syntax highlighting, tree view navigation, real-time validation, and customizable themes.
β¨ Features #
- π Flexible View Modes: Choose between dual view (tabs), tree-only, or raw-only modes
- π¨ Syntax Highlighting: Beautiful JSON syntax highlighting with customizable themes
- π³ Interactive Tree View: Navigate and edit JSON with an intuitive tree interface
- π― Real-time Validation: Live JSON validation with error highlighting
- π Copy & Paste: Built-in clipboard functionality
- π¨ Customizable Themes: Multiple built-in themes and custom theme support
- π± Responsive Design: Works seamlessly across different screen sizes
- β‘ Performance Optimized: Efficient rendering and memory management
- π§ Smart Indentation: Context-aware indentation and auto-closing
- ποΈ Advanced Controls: Format, clear, and validate JSON with action buttons
π Installation #
Add spectre_json to your pubspec.yaml:
dependencies:
spectre_json: ^1.0.0
Then run:
flutter pub get
π Quick Start #
import 'package:flutter/material.dart';
import 'package:spectre_json/spectre_json.dart';
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Map<String, dynamic> jsonData = {
'name': 'John Doe',
'age': 30,
'isActive': true,
'address': {
'street': '123 Main St',
'city': 'Anytown',
'zipCode': '12345'
},
'hobbies': ['reading', 'coding', 'gaming'],
};
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('JSON Editor Example')),
body: Padding(
padding: EdgeInsets.all(16.0),
child: JsonEditor(
initialData: jsonData,
onDataChanged: (newData) {
setState(() {
jsonData = newData;
});
},
title: 'My JSON Data',
allowCopy: true,
theme: RedPandaTheme(),
),
),
);
}
}
π― Usage #
Basic Usage #
JsonEditor(
initialData: yourJsonData,
onDataChanged: (newData) {
// Handle data changes
print('JSON updated: $newData');
},
)
Advanced Usage #
JsonEditor(
initialData: jsonData,
onDataChanged: (newData) {
setState(() {
jsonData = newData;
});
},
title: 'Configuration Editor',
readOnly: false,
allowCopy: true,
theme: JsonEditorTheme.light(primaryColor: Colors.blue),
isExpanded: true,
onExpansionChanged: (expanded) {
print('Editor ${expanded ? 'expanded' : 'collapsed'}');
},
onCollapse: () {
print('Editor collapsed');
},
)
Custom Theme #
// Using the factory constructor (recommended)
JsonEditor(
initialData: data,
onDataChanged: (newData) {},
theme: JsonEditorTheme.fromColors(
editorBackground: Colors.grey[900]!,
foreground: Colors.white,
primaryColor: Colors.blue,
stringColor: Colors.green,
numberColor: Colors.orange,
),
)
// Or extend the base class
class MyCustomTheme extends JsonEditorTheme {
@override
Color get editorBackground => Colors.grey[900]!;
@override
Color get foreground => Colors.white;
@override
Color get primaryColor => Colors.blue;
// ... implement all other color getters
}
View Types #
Choose how the JSON editor is displayed:
// Dual view (default) - shows both tree and raw editor with tabs
JsonEditor(
initialData: data,
onDataChanged: (newData) {},
viewType: ViewType.dual,
)
// Tree view only - shows only the interactive tree interface
JsonEditor(
initialData: data,
onDataChanged: (newData) {},
viewType: ViewType.treeOnly,
)
// Raw editor only - shows only the text editor
JsonEditor(
initialData: data,
onDataChanged: (newData) {},
viewType: ViewType.rawOnly,
)
Expansion Modes #
Control how tree view nodes are expanded by default:
// No expansion (default) - only root node is expanded
JsonEditor(
initialData: data,
onDataChanged: (newData) {},
expansionMode: ExpansionMode.none,
)
// Expand all object nodes (including nested objects)
JsonEditor(
initialData: data,
onDataChanged: (newData) {},
expansionMode: ExpansionMode.objects,
)
// Expand all array nodes (including nested arrays)
JsonEditor(
initialData: data,
onDataChanged: (newData) {},
expansionMode: ExpansionMode.arrays,
)
// Expand both objects and arrays
JsonEditor(
initialData: data,
onDataChanged: (newData) {},
expansionMode: ExpansionMode.objectsAndArrays,
)
// Expand everything
JsonEditor(
initialData: data,
onDataChanged: (newData) {},
expansionMode: ExpansionMode.all,
)
// Expand only the top 3 levels
JsonEditor(
initialData: data,
onDataChanged: (newData) {},
expansionMode: ExpansionMode.levels,
maxExpansionLevel: 3,
)
π¨ Available Themes #
Built-in Themes #
- RedPandaTheme: Beautiful dark theme with red accents (default)
- JsonEditorTheme.light(): Clean light theme
- JsonEditorTheme.dark(): Modern dark theme
- JsonEditorTheme.material(context): Theme that adapts to your app's Material theme
Custom Themes #
Create custom themes using the JsonEditorTheme.fromColors() factory constructor:
JsonEditorTheme.fromColors(
editorBackground: Colors.grey[900]!,
foreground: Colors.white,
primaryColor: Colors.blue,
stringColor: Colors.green,
numberColor: Colors.orange,
booleanColor: Colors.purple,
nullColor: Colors.red,
punctuationColor: Colors.grey,
keyColor: Colors.cyan,
)
π API Reference #
JsonEditor #
The main widget for editing JSON data.
Properties
| Property | Type | Required | Default | Description |
|---|---|---|---|---|
initialData |
Map<String, dynamic> |
β | - | The initial JSON data to display |
onDataChanged |
Function(Map<String, dynamic>) |
β | - | Callback function when data changes |
title |
String |
β | 'JSON Editor' |
The title displayed in the editor header |
readOnly |
bool |
β | false |
Whether the editor is read-only |
allowCopy |
bool |
β | false |
Whether to show copy functionality |
theme |
JsonEditorTheme? |
β | RedPandaTheme() |
Custom theme for the editor |
isExpanded |
bool? |
β | true |
Initial expansion state |
onExpansionChanged |
Function(bool)? |
β | - | Callback when expansion state changes |
onCollapse |
VoidCallback? |
β | - | Callback when editor is collapsed |
viewType |
ViewType |
β | ViewType.dual |
The view type to display (dual, treeOnly, rawOnly) |
expansionMode |
ExpansionMode |
β | ExpansionMode.none |
Controls how tree view nodes are expanded by default |
maxExpansionLevel |
int |
β | 2 |
Maximum levels to expand when using ExpansionMode.levels |
JsonEditorTheme #
Base class for custom themes with the following color properties:
editorBackground: Background color of the editorforeground: Default text colorheaderBackground: Header background colorsurfaceBackground: Surface background colorlineNumbersBackground: Line numbers background colorborderColor: Border colorprimaryColor: Primary accent coloronPrimary: Text color on primary backgrounderrorColor: Error text and border colorcursorColor: Text cursor colorstringColor: JSON string value colornumberColor: JSON number value colorbooleanColor: JSON boolean value colornullColor: JSON null value colorpunctuationColor: JSON punctuation colorkeyColor: JSON key color
π Features in Detail #
Flexible View Modes #
- Dual View: Switch between text editor and tree view with tabs
- Tree Only: Dedicated tree view interface for focused navigation
- Raw Only: Full-featured text editor with syntax highlighting
Syntax Highlighting #
- Color-coded JSON syntax for better readability
- Support for strings, numbers, booleans, null values, and punctuation
- Customizable colors through themes
Tree View Features #
- Expandable/collapsible nodes
- Inline editing of values and keys
- Add new properties and array items
- Intelligent type inference for arrays
- Copy individual values
Smart Editing #
- Context-aware indentation
- Auto-closing brackets, braces, and quotes
- Real-time JSON validation
- Error highlighting and messages
Action Buttons #
- Format: Beautify JSON with proper indentation
- Clear: Reset to empty JSON object
- Validate: Check JSON validity
- Copy: Copy JSON to clipboard
- Paste: Paste JSON from clipboard
π± Platform Support #
- β Android
- β iOS
- β Web
- β Windows
- β macOS
- β Linux
π§ Development #
Running Tests #
flutter test
Running the Example #
cd example
flutter run
π€ Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
π License #
This project is licensed under the MIT License - see the LICENSE file for details.
π Changelog #
1.0.0 #
- π Initial release
- β¨ Dual view mode (text editor and tree view)
- π¨ Syntax highlighting with customizable themes
- π³ Interactive tree view with inline editing
- π§ Real-time JSON validation
- π Copy and paste functionality
- ποΈ Action buttons (format, clear, validate)
- π¨ Multiple built-in themes (RedPanda, Light, Dark, Material)
- π§ Smart indentation and auto-closing
- π± Responsive design for all platforms
- β‘ Performance optimizations
- π§ͺ Comprehensive test coverage (85+ tests)
π Acknowledgments #
- Inspired by modern code editors and JSON viewers
- Built with Flutter's powerful widget system
- Tested across multiple platforms and devices