Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide documentation on how to work with JSON data #19

Closed
rodydavis opened this issue Aug 9, 2018 · 5 comments
Closed

Provide documentation on how to work with JSON data #19

rodydavis opened this issue Aug 9, 2018 · 5 comments

Comments

@rodydavis
Copy link

rodydavis commented Aug 9, 2018

Using the example didn't help much for loading and saving data from JSON. Here is the code I used to get it working.

This will auto-insert text into Notus document for backwards compatibility of data stored as plain text and will handle errors too.

import 'package:flutter/material.dart';
import 'package:zefyr/zefyr.dart';
import 'dart:convert';

class MyWidget extends StatefulWidget {
  final String json;
  MyWidget({this.json});
  @override
  MyWidgetState createState() => MyWidgetState();
}

class MyWidgetState extends State<MyWidget> {
  ZefyrController _controller;
  FocusNode _focusNode;

  @override
  void initState() {
    super.initState();
    // Create an empty document or load existing if you have one.
    // Here we create an empty document:
   var document = NotusDocument();
    if (widget.json != null) {
      try {
        var _json = json.decode(widget.json);
        document = NotusDocument.fromJson(_json);
      } catch (e) {
        print('Error Parsing JSON: $e');
        try {
          document.insert(0, widget.json);
        } catch (exp) {
          print('Error Inserting into Document: $exp');
        }
      }
    }
    _controller = new ZefyrController(document);
    _focusNode = new FocusNode();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'Notes',
          style: TextStyle(
            color: globals.isDark ? Colors.white : Colors.black,
          ),
        ),
        backgroundColor: globals.isDark ? null : Colors.white,
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.save),
            onPressed: () {
              NotusDocument _data = _controller.document;
              Navigator.pop(context, json.encode(_data.toJson()));
            },
          ),
        ],
      ),
      body: SafeArea(
        child: ZefyrEditor(
          autofocus: true,
          controller: _controller,
          focusNode: _focusNode,
        ),
      ),
    );
  }
}
@rodydavis
Copy link
Author

rodydavis commented Aug 9, 2018

I also added the Scaffold so you can push to the widget with a full-screen dialog to edit the text.

String notes = "";

Navigator.push(
 context,
  MaterialPageRoute(
     builder: (context) => MyWidget(
     json: notes,
  ),
  fullscreenDialog: true,
  ),
 ).then((result) {
  if (result != null) notes = result;
});

@pulyaevskiy
Copy link
Contributor

Thanks for submitting this issue. I agree, docs and examples are pretty basic at this point. I'll make sure to add more information on how to work with JSON for the next release.

@pulyaevskiy pulyaevskiy changed the title update example Provide documentation on how to work with JSON data Aug 9, 2018
@rodydavis
Copy link
Author

@pulyaevskiy awesome thanks! It works great by the way. Using it in an app already. There needs to be a better way in flutter just to view a Notus document and not edit it.

@pulyaevskiy
Copy link
Contributor

There needs to be a better way in flutter just to view a Notus document and not edit it.

A simple way to achieve similar effect is to construct ZefyrEditor with enabled: false. This would hide all controls.

@rodydavis
Copy link
Author

@pulyaevskiy awesome will do!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants