SanjaySodani / json_editor_flutter

A user friendly widget to edit JSON object.
BSD 3-Clause "New" or "Revised" License
8 stars 2 forks source link

feature request: optionally disable add and delete #1

Closed naychrist closed 11 months ago

naychrist commented 11 months ago

hi there. this package is great. only JSON editor I've found that supports all platforms. I'm keen to use it for editing application config files but need to disable the add and delete buttons. I have something working for my purposes but thought it would be too hacky for a pull request. let me know if you'd like one though. my solution was:

define global bool to toggle:

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

/// Optionally remove Add and Delete icons
bool hideAddDelete = false;

set bool using optional constructor argument

  JsonEditor({
    super.key,
    required this.json,
    required this.onSaved,
    this.height,
    this.width,
    this.color,
    bool disableAddDelete = false,
  }) {
    hideAddDelete = disableAddDelete;
  }

then everywhere either add or delete icons are displayed I check the bool:

child: !hideAddDelete ? const Icon(Icons.add, size: 20) : Container(),

...

        children: [
          if (!hideAddDelete)
            InkWell(
            onTap: () {
              if (parent is Map) {
                parent.remove(key);
              } else {
                (parent as List).removeAt(key);
              }
              setState(() {});
            },
            child: deleteIcon,
          ),
SanjaySodani commented 11 months ago

Hi @naychrist, I have been working on this package from few days and building it again from scratch. I will include this feature too as suggested.

Thank you!

naychrist commented 11 months ago

thanks @SanjaySodani for my purposes I also plan to eventually add some icon buttons up top that I can subscribe to with callbacks like the save button but for 'download json file', 'upload json file' and 'close window'. Would love it if this ended up in the new version too or if the new version is as easy to work with as the current one - thanks for the package!

SanjaySodani commented 11 months ago

@naychrist I have published the newer version with your feature as well.

I will add download and upload json file feature too in upcoming versions but could you please elaborate what will be the function of 'close window'?

naychrist commented 11 months ago

thanks @SanjaySodani ! I will check it out. for me I use a shortcut key or right click to open the json editor (currently just toggled inside a visibility widget). Ideally I'd have the option to include a close button that I can listen to so that I can hide it again. But having said that I am currently using the same shortcut to hide it again so not critical.

naychrist commented 11 months ago

thanks again @SanjaySodani just had a play. love the enableMoreOptions toggle and onChanged. Copy and text view is great too as is easy to manually load/save entire json blocks now, which will suffice until any download/upload buttons. onSaved was useful as an option as I use this in combinaton with the shared_preferences package to save my app config although could replace this with a key shortcut. Undo and expand all buttons were handy too although if it could be set to default expanded would not need the expand button.

SanjaySodani commented 11 months ago

@naychrist Yes, in the previous versions also the expand/collapse buttons were not working properly. I will include them in upcoming versions making sure they work as expected. But for other needs like having a close window button and save button, I can provide an additional Widget parameter where you can include all your buttons with your callbacks. That will be appended at the end of the header part.

naychrist commented 11 months ago

thanks @SanjaySodani that sounds like an elegant solution. the new version working great for me already though.

SanjaySodani commented 2 months ago

@naychrist Checkout the new version

naychrist commented 1 month ago

thanks for getting to this, just took the example for a spin and added a custom widget to test - works great.