json_editor_2 0.1.2 copy "json_editor_2: ^0.1.2" to clipboard
json_editor_2: ^0.1.2 copied to clipboard

A json editor on flutter,The project is forked from https://github.com/youngchan1988/json_editor_2, and modified to support passing and modifying custom TextFiledController..

example/lib/main.dart

import 'package:flutter/material.dart';

import 'package:json_editor_2/json_editor.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {


    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool _darkMode = false;
  JsonElement? _elementResult;
  final GlobalKey<JsonEditorState> _jsonEditorKey = GlobalKey<JsonEditorState>();

  @override
  Widget build(BuildContext context) {
  RichTextEditingController controller = RichTextEditingController();
    return Scaffold(
        backgroundColor: _darkMode
            ? ThemeData.dark().scaffoldBackgroundColor
            : ThemeData.light().scaffoldBackgroundColor,
        appBar: AppBar(
          title: const Text('JsonEditor'),
        ),
        body: Padding(
            padding: const EdgeInsets.all(16),
            child: Column(
              children: [
                Row(
                  children: [
                    Switch(
                        value: _darkMode,
                        onChanged: (b) {
                          setState(() {
                            _darkMode = b;
                          });
                        }),
                    const SizedBox(width: 8),
                    Text(
                      'Dark Mode',
                      style: TextStyle(
                          color: _darkMode ? Colors.white : Colors.black),
                    ),
                    const SizedBox(
                      width: 16,
                    ),
                    ElevatedButton(
                        onPressed: () {
                          Navigator.of(context).push(MaterialPageRoute(
                              builder: (context) => ObjectDemoPage(
                                    obj: _elementResult?.toObject(),
                                  )));
                        },
                        child: Text('Object Demo')),
                    const SizedBox(
                      width: 16,
                    ),
                    ElevatedButton(
                        onPressed: () {
                          // Navigator.of(context).push(MaterialPageRoute(
                          //     builder: (context) => ElementDemoPage(
                          //           element: _elementResult,
                          //         )));
                          debugPrint("controller:${controller.text}");
                        },
                        child: Text('Get AllText')),
                  ],
                ),
                Expanded(
                  child: JsonEditorTheme(
                    themeData: JsonEditorThemeData.defaultTheme(),
                    child: JsonEditor.string(
                      controller: controller,
                      openDebug: true,
                      jsonString: '''
                      {
                        // This is a comment
                        "name": "young chan",
                        "number": 100,
                        "boo": true,
                        "user": {"age": 20, "tall": 1.8},
                        "cities": ["beijing", "shanghai", "shenzhen"]
                      }''',
                      onValueChanged: (value) {
                        debugPrint("onValueChanged:${value}");
                        _elementResult = value;

                        print(value);
                      },
                    ),
                  ),
                )
              ],
            )));
  }
}

class ObjectDemoPage extends StatelessWidget {
  const ObjectDemoPage({Key? key, this.obj}) : super(key: key);

  final Object? obj;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Object Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: JsonEditor.object(
          object: obj,
          onValueChanged: (value) {
            var json = value.toJson();
            print(json);
            var fromJson = JsonElement.fromJson(json);
            print(fromJson);
          },
        ),
      ),
    );
  }
}

class ElementDemoPage extends StatelessWidget {
  const ElementDemoPage({Key? key, this.element}) : super(key: key);

  final JsonElement? element;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Element Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: JsonEditor.element(
          element: element,
          onValueChanged: (value) {
            var json = value.toJson();
            print(json);
            var fromJson = JsonElement.fromJson(json);
            print(fromJson);
          },
        ),
      ),
    );
  }
}
0
likes
120
points
38
downloads

Publisher

unverified uploader

Weekly Downloads

A json editor on flutter,The project is forked from https://github.com/youngchan1988/json_editor_2, and modified to support passing and modifying custom TextFiledController..

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flutter, logger

More

Packages that depend on json_editor_2