dynamic_widget 0.0.2

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • new43

Flutter Dynamic Widget #

You can dynamic update your flutter page with Flutter Dynamic Widget. It's a light json UI protocol, which is very similar with flutter widget code. So you don't need to learn another UI protocol, if you are familiar with flutter widget, you might already know how to write Flutter Dynamic Widget json.

Table of contents #

General info #

I work for an e-commerce company. We need to build flexible pages. So we define a light UI protocol, and implement on Android and iOS. We can dynamic update App UIs by pushing a json file. With this ability, we can do some UI A/B testing without publishing App to app store. Flutter allows you to build beautiful native apps on iOS and Android from a single codebase, it can allow you to build web app later. Flutter's hot reload helps you quickly and easily experiment, build UIs, add features, and fix bugs faster. But it still build native app, the UIs can't be dynamic updated. If you want to modify the UIs, you need to publish the updated app to app store. With this project, you can build your UIs from a json string, which is the UI protocal. The json string is very similar with the Flutter widget dart code. All widget type and widget properties are the same.

Widget type will be a type property, and widget's properties will be the json properties too. All properties and their values will be almost the same. You can checkout the following document.

Currently support flutter widgets and properties

Screenshots #

Install #

1. Depend on it #

Add this to your package's pubspec.yaml file:

dependencies:
  dynamic_widget: ^0.0.1

2. Install it #

You can install packages from the command line:

with Flutter:

$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it #

Now in your Dart code, you can use:

import 'package:dynamic_widget/dynamic_widget.dart';

Get started #

You should use DynamicWidgetBuilder().build method to covert a json string into flutter widget. It will be time-consuming. so you'd better using FutureBuilder to build the UI.

import 'package:dynamic_widget/dynamic_widget.dart';
class PreviewPage extends StatelessWidget{

  final String jsonString;

  PreviewPage(this.jsonString);


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          // Here we take the value from the MyHomePage object that was created by
          // the App.build method, and use it to set our appbar title.
          title: Text("Preview"),
        ),
        body: FutureBuilder<Widget>(
          future: _buildWidget(),
          builder: (BuildContext context, AsyncSnapshot<Widget> snapshot){
            return snapshot.hasData?snapshot.data:Text("Loading...");
          },
        ),
    );
  }

  Future<Widget> _buildWidget() async{

    return DynamicWidgetBuilder().build(jsonString);
  }
}

How to implement a WidgetParser #

  1. You need to implement the WidgetParser abstract class.
  2. Add new created WidgetParser to DynamicWidgetBuilder.parsers list.

This is a RaisedButton widget parser.

import 'package:dynamic_widget/dynamic_widget/utils.dart';
import 'package:dynamic_widget/dynamic_widget.dart';
import 'package:flutter/material.dart';

class RaisedButtonParser extends WidgetParser{
  @override
  bool forWidget(String widgetName) {
    return "RaisedButton" == widgetName;
  }

  @override
  Widget parse(Map<String, dynamic> map) {
    return RaisedButton(
      color: map.containsKey('color') ? parseHexColor(map['color']) : null,
      disabledColor: map.containsKey('disabledColor') ? parseHexColor(map['disabledColor']) : null,
      disabledElevation: map.containsKey('disabledElevation') ? map['disabledElevation'] : 0.0,
      disabledTextColor: map.containsKey('disabledTextColor') ? parseHexColor(map['disabledTextColor']) : null,
      elevation: map.containsKey('elevation') ? map['elevation'] : 0.0,
      padding: map.containsKey('padding') ? parseEdgeInsetsGeometry(map['padding']) : null,
      splashColor : map.containsKey('splashColor') ? parseHexColor(map['splashColor']) : null,
      textColor: map.containsKey('textColor') ? parseHexColor(map['textColor']) : null,
      child: DynamicWidgetBuilder().buildFromMap(map['child']),
      onPressed: (){},
    );
  }

}

Add it to parsers list.

class DynamicWidgetBuilder{

  final Logger log = Logger('DynamicWidget');

  static final parsers = [
    ContainerWidgetParser(),
    TextWidgetParser(),
    RaisedButtonParser(),
    RowWidgetParser()
  ];


  Widget build(String json){

    var map = jsonDecode(json);
    return buildFromMap(map);
  }

  Widget buildFromMap(Map<String, dynamic> map){
    String widgetName = map['type'];

    for (var parser in parsers) {
      if (parser.forWidget(widgetName)) {
        return parser.parse(map);
      }
    }

    log.warning("Not support type: $widgetName");
    return null;
  }

  List<Widget> buildWidgets(List<dynamic> values){
    List<Widget> rt = [];
    for (var value in values) {
      rt.add(buildFromMap(value));
    }
    return rt;
  }
}

Documents #

Currently support widgets and properties

Setup #

Checkout this project and run deom.

Code Examples #

Checkout this project and run deom.

Features #

Already completed widgets:

  • Container
  • Text & TextSpan
  • Row & Column
  • AssetImage
  • NetworkImage
  • FileImage
  • Placeholder
  • GridView
  • ListView
  • PageView
  • Expanded
  • Padding
  • Center
  • Align
  • AspectRatio
  • FittedBox
  • Baseline

You can view Currently support widgets and properties here.

Contact #

Created by @deng.yin@gmail.com - feel free to contact me!

[0.0.1] - TODO: Add release date.

  • TODO: Describe initial release.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  dynamic_widget: ^0.0.2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:dynamic_widget/dynamic_widget.dart';
  
Version Uploaded Documentation Archive
0.0.2 Feb 15, 2019 Go to the documentation of dynamic_widget 0.0.2 Download dynamic_widget 0.0.2 archive
0.0.1 Jan 28, 2019 Go to the documentation of dynamic_widget 0.0.1 Download dynamic_widget 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
95
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
71
Overall:
Weighted score of the above. [more]
43
Learn more about scoring.

We analyzed this package on Feb 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.13+1
  • Flutter: 1.2.1

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Fix lib/dynamic_widget/basic/aspectratio_widget_parser.dart. (-0.50 points)

Analysis of lib/dynamic_widget/basic/aspectratio_widget_parser.dart reported 1 hint:

line 4 col 8: Don't import implementation files from another package.

Fix lib/dynamic_widget/basic/baseline_widget_parser.dart. (-0.50 points)

Analysis of lib/dynamic_widget/basic/baseline_widget_parser.dart reported 1 hint:

line 4 col 8: Don't import implementation files from another package.

Fix lib/dynamic_widget/basic/center_widget_parser.dart. (-0.50 points)

Analysis of lib/dynamic_widget/basic/center_widget_parser.dart reported 1 hint:

line 4 col 8: Don't import implementation files from another package.

Fix additional 15 files with analysis or formatting issues. (-4 points)

Additional issues in the following files:

  • lib/dynamic_widget/basic/expanded_widget_parser.dart (1 hint)
  • lib/dynamic_widget/basic/fittedbox_widget_parser.dart (1 hint)
  • lib/dynamic_widget/basic/padding_widget_parser.dart (1 hint)
  • lib/dynamic_widget/basic/placeholder_widget_parser.dart (1 hint)
  • lib/dynamic_widget/basic/text_widget_parser.dart (1 hint)
  • lib/dynamic_widget/scrolling/gridview_widget_parser.dart (1 hint)
  • lib/dynamic_widget/scrolling/listview_widget_parser.dart (1 hint)
  • lib/dynamic_widget/scrolling/pageview_widget_parser.dart (1 hint)
  • lib/dynamic_widget.dart (Run flutter format to format lib/dynamic_widget.dart.)
  • lib/dynamic_widget/basic/align_widget_parser.dart (Run flutter format to format lib/dynamic_widget/basic/align_widget_parser.dart.)
  • lib/dynamic_widget/basic/button_widget_parser.dart (Run flutter format to format lib/dynamic_widget/basic/button_widget_parser.dart.)
  • lib/dynamic_widget/basic/container_widget_parser.dart (Run flutter format to format lib/dynamic_widget/basic/container_widget_parser.dart.)
  • lib/dynamic_widget/basic/image_widget_parser.dart (Run flutter format to format lib/dynamic_widget/basic/image_widget_parser.dart.)
  • lib/dynamic_widget/basic/row_column_widget_parser.dart (Run flutter format to format lib/dynamic_widget/basic/row_column_widget_parser.dart.)
  • lib/dynamic_widget/utils.dart (Run flutter format to format lib/dynamic_widget/utils.dart.)

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and dynamic_widget.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

The package description is too short. (-9 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
logging ^0.11.3+2 0.11.3+2
Transitive dependencies
collection 1.14.11
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test