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

Implement basic TextFieldWidgetParser #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
.DS_Store
.dart_tool/
# IntelliJ related
.idea/

# Visual Studio Code related
.vscode/

# Flutter/Dart/Pub related
**/doc/api/
.dart_tool/
.flutter-plugins
.packages
.pub-cache/
.pub/
/build/
pubspec.lock
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure that pubspec.lock should be in git :)



build/
ios/.generated/
Expand Down
24 changes: 22 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'dart:async';
import 'dart:collection';
import 'dart:convert';

import 'package:dynamic_widget/dynamic_widget.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -263,6 +265,15 @@ class _MyHomePageState extends State<MyHomePage> {
builder: (context) => CodeEditorPage(cliprrectJson)));
},
),
RaisedButton(
child: Text("TextField"),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CodeEditorPage(textFieldJson)));
},
),
],
));
}
Expand Down Expand Up @@ -332,6 +343,7 @@ class _CodeEditorPageState extends State<CodeEditorPage> {

class PreviewPage extends StatelessWidget {
final String jsonString;
final Map<String, TextEditingController> textEditingControllers = HashMap();

PreviewPage(this.jsonString);

Expand Down Expand Up @@ -360,8 +372,16 @@ class PreviewPage extends StatelessWidget {
}

Future<Widget> _buildWidget(BuildContext context) async {
return DynamicWidgetBuilder()
.build(jsonString, context, new DefaultClickListener());
WidgetParserCompanion companion = WidgetParserCompanion();
companion.clickListener = new DefaultClickListener();
companion.onTextChange = (String id, String text) {
print("TextField id: $id, text $text");
};
companion.textEditingController = textEditingControllers;
var finalLayout = DynamicWidgetBuilder()
.build(jsonString, context, companion);
print("TextEditingController size ${textEditingControllers.length}");
return finalLayout;
}
}

Expand Down
8 changes: 8 additions & 0 deletions example/lib/widget_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -886,3 +886,11 @@ var cliprrectJson = '''
}
}
''';

var textFieldJson = '''
{
"id": "text_field_test",
"type": "TextField",
"textColor": "#00FF00"
}
''';
38 changes: 26 additions & 12 deletions lib/dynamic_widget.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
library dynamic_widget;

import 'dart:convert';

import 'package:dynamic_widget/dynamic_widget/basic/align_widget_parser.dart';
import 'package:dynamic_widget/dynamic_widget/basic/aspectratio_widget_parser.dart';
import 'package:dynamic_widget/dynamic_widget/basic/baseline_widget_parser.dart';
Expand All @@ -22,10 +24,10 @@ import 'package:dynamic_widget/dynamic_widget/scrolling/gridview_widget_parser.d
import 'package:dynamic_widget/dynamic_widget/scrolling/listview_widget_parser.dart';
import 'package:dynamic_widget/dynamic_widget/scrolling/pageview_widget_parser.dart';
import 'package:flutter/widgets.dart';
import 'dart:convert';
import 'package:logging/logging.dart';

import 'dynamic_widget/basic/cliprrect_widget_parser.dart';
import 'dynamic_widget/basic/text_field_widget_parser.dart';

class DynamicWidgetBuilder {
static final Logger log = Logger('DynamicWidget');
Expand Down Expand Up @@ -56,7 +58,8 @@ class DynamicWidgetBuilder {
SizedBoxWidgetParser(),
OpacityWidgetParser(),
WrapWidgetParser(),
ClipRRectWidgetParser()
ClipRRectWidgetParser(),
TextFieldWidgetParser()
];

// use this method for adding your custom widget parser
Expand All @@ -66,33 +69,34 @@ class DynamicWidgetBuilder {
_parsers.add(parser);
}

Widget build(String json, BuildContext buildContext, ClickListener listener) {
Widget build(String json, BuildContext buildContext, WidgetParserCompanion widgetParserCompanion) {
print("print widget 2");
var map = jsonDecode(json);
ClickListener _listener =
listener == null ? new NonResponseWidgetClickListener() : listener;
var widget = buildFromMap(map, buildContext, _listener);
if(widgetParserCompanion == null) widgetParserCompanion = WidgetParserCompanion();
widgetParserCompanion.clickListener =
widgetParserCompanion.clickListener == null ? new NonResponseWidgetClickListener() : widgetParserCompanion.clickListener;
var widget = buildFromMap(map, buildContext, widgetParserCompanion);
return widget;
}

static Widget buildFromMap(Map<String, dynamic> map,
BuildContext buildContext, ClickListener listener) {
BuildContext buildContext, WidgetParserCompanion widgetParserCompanion) {
String widgetName = map['type'];

for (var parser in _parsers) {
if (parser.forWidget(widgetName)) {
return parser.parse(map, buildContext, listener);
return parser.parse(map, buildContext, widgetParserCompanion);
}
}

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

static List<Widget> buildWidgets(
List<dynamic> values, BuildContext buildContext, ClickListener listener) {
List<dynamic> values, BuildContext buildContext, WidgetParserCompanion widgetParserCompanion) {
List<Widget> rt = [];
for (var value in values) {
rt.add(buildFromMap(value, buildContext, listener));
rt.add(buildFromMap(value, buildContext, widgetParserCompanion));
}
return rt;
}
Expand All @@ -102,7 +106,7 @@ class DynamicWidgetBuilder {
abstract class WidgetParser {
/// parse the json map into a flutter widget.
Widget parse(Map<String, dynamic> map, BuildContext buildContext,
ClickListener listener);
WidgetParserCompanion widgetParserCompanion);

/// check the matched widget type. for example:
/// {"type" : "Text", "data" : "Denny"}
Expand All @@ -116,6 +120,10 @@ abstract class ClickListener {
void onClicked(String event);
}

abstract class OnTextChangeListener {
void onTextChange(String textFieldId, String text);
}

class NonResponseWidgetClickListener implements ClickListener {
static final Logger log = Logger('NonResponseWidgetClickListener');

Expand All @@ -125,3 +133,9 @@ class NonResponseWidgetClickListener implements ClickListener {
print("receiver click event: " + event);
}
}

class WidgetParserCompanion {
ClickListener clickListener;
Function(String,String) onTextChange;
Map<String, TextEditingController> textEditingController;
}
Binary file added lib/dynamic_widget/.DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions lib/dynamic_widget/basic/align_widget_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AlignWidgetParser extends WidgetParser {

@override
Widget parse(Map<String, dynamic> map, BuildContext buildContext,
ClickListener listener) {
WidgetParserCompanion widgetParserCompanion) {
return Align(
alignment: map.containsKey("alignment")
? parseAlignment(map["alignment"])
Expand All @@ -19,7 +19,7 @@ class AlignWidgetParser extends WidgetParser {
heightFactor:
map.containsKey("heightFactor") ? map["heightFactor"] : null,
child: DynamicWidgetBuilder.buildFromMap(
map["child"], buildContext, listener),
map["child"], buildContext, widgetParserCompanion),
);
}
}
4 changes: 2 additions & 2 deletions lib/dynamic_widget/basic/aspectratio_widget_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ class AspectRatioWidgetParser extends WidgetParser {

@override
Widget parse(Map<String, dynamic> map, BuildContext buildContext,
ClickListener listener) {
WidgetParserCompanion widgetParserCompanion) {
return AspectRatio(
aspectRatio: map["aspectRatio"],
child: DynamicWidgetBuilder.buildFromMap(
map["child"], buildContext, listener),
map["child"], buildContext, widgetParserCompanion),
);
}
}
4 changes: 2 additions & 2 deletions lib/dynamic_widget/basic/baseline_widget_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ class BaselineWidgetParser extends WidgetParser {

@override
Widget parse(Map<String, dynamic> map, BuildContext buildContext,
ClickListener listener) {
WidgetParserCompanion widgetParserCompanion) {
return Baseline(
baseline: map["baseline"],
baselineType: map["baselineType"] == "alphabetic"
? TextBaseline.alphabetic
: TextBaseline.ideographic,
child: DynamicWidgetBuilder.buildFromMap(
map["child"], buildContext, listener),
map["child"], buildContext, widgetParserCompanion),
);
}
}
6 changes: 3 additions & 3 deletions lib/dynamic_widget/basic/button_widget_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class RaisedButtonParser extends WidgetParser {

@override
Widget parse(Map<String, dynamic> map, BuildContext buildContext,
ClickListener listener) {
WidgetParserCompanion widgetParserCompanion) {
String clickEvent =
map.containsKey("click_event") ? map['click_event'] : "";

Expand All @@ -34,9 +34,9 @@ class RaisedButtonParser extends WidgetParser {
textColor:
map.containsKey('textColor') ? parseHexColor(map['textColor']) : null,
child: DynamicWidgetBuilder.buildFromMap(
map['child'], buildContext, listener),
map['child'], buildContext, widgetParserCompanion),
onPressed: () {
listener.onClicked(clickEvent);
widgetParserCompanion.clickListener.onClicked(clickEvent);
},
);

Expand Down
4 changes: 2 additions & 2 deletions lib/dynamic_widget/basic/center_widget_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class CenterWidgetParser extends WidgetParser {

@override
Widget parse(Map<String, dynamic> map, BuildContext buildContext,
ClickListener listener) {
WidgetParserCompanion widgetParserCompanion) {
return Center(
widthFactor: map.containsKey("widthFactor") ? map["widthFactor"] : null,
heightFactor:
map.containsKey("heightFactor") ? map["heightFactor"] : null,
child: DynamicWidgetBuilder.buildFromMap(
map["child"], buildContext, listener),
map["child"], buildContext, widgetParserCompanion),
);
}
}
4 changes: 2 additions & 2 deletions lib/dynamic_widget/basic/cliprrect_widget_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ClipRRectWidgetParser extends WidgetParser {

@override
Widget parse(Map<String, dynamic> map, BuildContext buildContext,
ClickListener listener) {
WidgetParserCompanion widgetParserCompanion) {
var radius = map['borderRadius'].toString().split(",");
double topLeft = double.parse(radius[0]);
double topRight = double.parse(radius[1]);
Expand All @@ -25,7 +25,7 @@ class ClipRRectWidgetParser extends WidgetParser {
bottomRight: Radius.circular(bottomRight)),
clipBehavior: parseClipBehavior(clipBehaviorString),
child: DynamicWidgetBuilder.buildFromMap(
map["child"], buildContext, listener),
map["child"], buildContext, widgetParserCompanion),
);
}
}
8 changes: 4 additions & 4 deletions lib/dynamic_widget/basic/container_widget_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ContainerWidgetParser extends WidgetParser {

@override
Widget parse(Map<String, dynamic> map, BuildContext buildContext,
ClickListener listener) {
WidgetParserCompanion widgetParserCompanion) {
Alignment alignment = parseAlignment(map['alignment']);
Color color = parseHexColor(map['color']);
BoxConstraints constraints = parseBoxConstraints(map['constraints']);
Expand All @@ -20,7 +20,7 @@ class ContainerWidgetParser extends WidgetParser {
Map<String, dynamic> childMap = map['child'];
Widget child = childMap == null
? null
: DynamicWidgetBuilder.buildFromMap(childMap, buildContext, listener);
: DynamicWidgetBuilder.buildFromMap(childMap, buildContext, widgetParserCompanion);

String clickEvent =
map.containsKey("click_event") ? map['click_event'] : "";
Expand All @@ -36,10 +36,10 @@ class ContainerWidgetParser extends WidgetParser {
child: child,
);

if (listener != null && clickEvent != null) {
if (widgetParserCompanion.clickListener != null && clickEvent != null) {
return GestureDetector(
onTap: () {
listener.onClicked(clickEvent);
widgetParserCompanion.clickListener.onClicked(clickEvent);
},
child: containerWidget,
);
Expand Down
4 changes: 2 additions & 2 deletions lib/dynamic_widget/basic/expanded_widget_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class ExpandedWidgetParser extends WidgetParser {

@override
Widget parse(Map<String, dynamic> map, BuildContext buildContext,
ClickListener listener) {
WidgetParserCompanion widgetParserCompanion) {
return Expanded(
child: DynamicWidgetBuilder.buildFromMap(
map["child"], buildContext, listener),
map["child"], buildContext, widgetParserCompanion),
flex: map.containsKey("flex") ? map["flex"] : 1,
);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/dynamic_widget/basic/fittedbox_widget_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class FittedBoxWidgetParser extends WidgetParser {

@override
Widget parse(Map<String, dynamic> map, BuildContext buildContext,
ClickListener listener) {
WidgetParserCompanion widgetParserCompanion) {
return FittedBox(
alignment: map.containsKey("alignment")
? parseAlignment(map["alignment"])
: Alignment.center,
fit: map.containsKey("fit") ? parseBoxFit(map["fit"]) : BoxFit.contain,
child: DynamicWidgetBuilder.buildFromMap(
map["child"], buildContext, listener),
map["child"], buildContext, widgetParserCompanion),
);
}
}
12 changes: 6 additions & 6 deletions lib/dynamic_widget/basic/image_widget_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AssetImageWidgetParser extends WidgetParser {

@override
Widget parse(Map<String, dynamic> map, BuildContext buildContext,
ClickListener listener) {
WidgetParserCompanion widgetParserCompanion) {
String name = map['name'];
String semanticLabel =
map.containsKey('semanticLabel') ? map['semanticLabel'] : null;
Expand Down Expand Up @@ -65,10 +65,10 @@ class AssetImageWidgetParser extends WidgetParser {
filterQuality: filterQuality,
);

if (listener != null && (clickEvent != null && clickEvent.isNotEmpty)) {
if (widgetParserCompanion.clickListener != null && (clickEvent != null && clickEvent.isNotEmpty)) {
return GestureDetector(
onTap: () {
listener.onClicked(clickEvent);
widgetParserCompanion.clickListener.onClicked(clickEvent);
},
child: widget,
);
Expand All @@ -85,7 +85,7 @@ class NetworkImageWidgetParser extends WidgetParser {

@override
Widget parse(Map<String, dynamic> map, BuildContext buildContext,
ClickListener listener) {
WidgetParserCompanion widgetParserCompanion) {
String src = map['src'];
String semanticLabel =
map.containsKey('semanticLabel') ? map['semanticLabel'] : null;
Expand Down Expand Up @@ -138,10 +138,10 @@ class NetworkImageWidgetParser extends WidgetParser {
filterQuality: filterQuality,
);

if (listener != null && (clickEvent != null && clickEvent.isNotEmpty)) {
if (widgetParserCompanion.clickListener != null && (clickEvent != null && clickEvent.isNotEmpty)) {
return GestureDetector(
onTap: () {
listener.onClicked(clickEvent);
widgetParserCompanion.clickListener.onClicked(clickEvent);
},
child: widget,
);
Expand Down
Loading