Skip to content

Commit

Permalink
support null weidget/
Browse files Browse the repository at this point in the history
  • Loading branch information
wutsi committed Dec 25, 2022
1 parent c657b10 commit 5b21a43
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 23 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## [0.1.192] 2022-12-25

- CHANGE: Add alignment to MoneyText
- CHANGE: Use `SDUINull` for unsupported widgets

## [0.1.190] 2022-12-24

- ADD: Add chart component
Expand Down
8 changes: 7 additions & 1 deletion lib/src/money.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class SDUIMoneyText extends SDUIWidget {
double? valueFontSize;
double? currencyFontSize;
bool? bold;
String? alignment;

@override
SDUIWidget fromJson(Map<String, dynamic>? json) {
Expand All @@ -31,6 +32,7 @@ class SDUIMoneyText extends SDUIWidget {
valueFontSize = json?['valueFontSize'];
currencyFontSize = json?['currencyFontSize'];
bold = json?['bold'];
alignment = json?['alignment'];
return super.fromJson(json);
}

Expand All @@ -44,6 +46,7 @@ class SDUIMoneyText extends SDUIWidget {
valueFontSize: valueFontSize ?? 50,
currencyFontSize: currencyFontSize ?? 12,
bold: bold,
textAlign: toTextAlign(alignment),
);
}

Expand All @@ -56,6 +59,7 @@ class MoneyText extends StatelessWidget {
final double currencyFontSize;
final Color? color;
final bool? bold;
final TextAlign? textAlign;

const MoneyText(
{Key? key,
Expand All @@ -65,7 +69,8 @@ class MoneyText extends StatelessWidget {
this.valueFontSize = 50,
this.currencyFontSize = 12,
this.color,
this.bold = false})
this.bold = false,
this.textAlign})
: super(key: key);

@override
Expand Down Expand Up @@ -94,6 +99,7 @@ class MoneyText extends StatelessWidget {
),
]),
key: key,
textAlign: textAlign,
);
}

Expand Down
8 changes: 6 additions & 2 deletions lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:logger/logger.dart';

import 'action.dart';
import 'appbar.dart';
Expand Down Expand Up @@ -32,8 +33,10 @@ import 'icon_button.dart';
import 'image.dart';
import 'input.dart';
import 'list_view.dart';
import 'logger.dart';
import 'money.dart';
import 'noop.dart';
import 'null.dart';
import 'page_view.dart';
import 'photo_view.dart';
import 'pin_with_keyboard.dart';
Expand All @@ -56,7 +59,7 @@ import 'wrap.dart';
//-- Core ------------------------------------
/// Parser that convert JSON to flutter [Widget]
class SDUIParser {
// static final Logger _logger = LoggerFactory.create('SDUIParser');
static final Logger _logger = LoggerFactory.create('SDUIParser');
static final SDUIParser _singleton = SDUIParser._internal();

SDUIParser._internal();
Expand Down Expand Up @@ -259,7 +262,8 @@ class SDUIParser {
default:
widget = SDUIWidgetRegistry.getInstance().create(type);
if (widget == null) {
throw Exception("Unsupported node: ${json["type"]}");
_logger.w(">>>> Unsupported node: ${json["type"]}");
widget = SDUINull();
}
}

Expand Down
20 changes: 1 addition & 19 deletions lib/src/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SDUIText extends SDUIWidget {
caption ?? '',
overflow: _toTextOverflow(),
style: _toTextStyle(),
textAlign: _toTextAlign(),
textAlign: toTextAlign(alignment),
maxLines: maxLines,
key: id == null ? null : Key(id!),
);
Expand Down Expand Up @@ -69,24 +69,6 @@ class SDUIText extends SDUIWidget {
fontStyle: italic == true ? FontStyle.italic : FontStyle.normal,
decoration: _toTextDecoration());

TextAlign? _toTextAlign() {
switch (alignment?.toLowerCase()) {
case 'left':
return TextAlign.left;
case 'right':
return TextAlign.right;
case 'center':
return TextAlign.center;
case 'justify':
return TextAlign.justify;
case 'end':
return TextAlign.end;
case 'start':
return TextAlign.start;
}
return null;
}

TextDecoration? _toTextDecoration() {
switch (decoration?.toLowerCase()) {
case 'strikethrough':
Expand Down
18 changes: 18 additions & 0 deletions lib/src/widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ abstract class SDUIWidget {
return Color(int.parse('FF$hexCode', radix: 16));
}

TextAlign? toTextAlign(String? alignment) {
switch (alignment?.toLowerCase()) {
case 'left':
return TextAlign.left;
case 'right':
return TextAlign.right;
case 'center':
return TextAlign.center;
case 'justify':
return TextAlign.justify;
case 'end':
return TextAlign.end;
case 'start':
return TextAlign.start;
}
return null;
}

IconData? toIconData(String? code) {
if (code == null) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ description: SDUI make it easy to implement Server Driven UI pattern on flutter.
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 0.1.190
version: 0.1.192

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down

0 comments on commit 5b21a43

Please sign in to comment.