David PHAM-VAN

Replace Theme with ThemeData

... ... @@ -8,6 +8,7 @@
- Add TableRow decoration
- Add Chart Widget [Marco Papula]
- Add Divider Widget
- Replace Theme with ThemeData
## 1.6.2
... ...
... ... @@ -55,7 +55,7 @@ class Document {
final PdfDocument document;
final Theme theme;
final ThemeData theme;
final List<Page> _pages = <Page>[];
... ...
... ... @@ -87,7 +87,7 @@ class MultiPage extends Page {
this.crossAxisAlignment = CrossAxisAlignment.start,
this.header,
this.footer,
Theme theme,
ThemeData theme,
this.maxPages = 20,
PageOrientation orientation,
EdgeInsets margin})
... ... @@ -159,7 +159,8 @@ class MultiPage extends Page {
: BoxConstraints(
maxWidth: pageFormat.width - _margin.horizontal,
maxHeight: pageFormat.height - _margin.vertical);
final Theme calculatedTheme = theme ?? document.theme ?? Theme.base();
final ThemeData calculatedTheme =
theme ?? document.theme ?? ThemeData.base();
Context context;
double offsetEnd;
double offsetStart;
... ...
... ... @@ -28,7 +28,7 @@ class Page {
{PageTheme pageTheme,
PdfPageFormat pageFormat,
BuildCallback build,
Theme theme,
ThemeData theme,
PageOrientation orientation,
EdgeInsets margin,
bool clip = false})
... ... @@ -58,7 +58,7 @@ class Page {
final BuildCallback _build;
Theme get theme => pageTheme.theme;
ThemeData get theme => pageTheme.theme;
bool get mustRotate => pageTheme.mustRotate;
... ... @@ -100,7 +100,8 @@ class Page {
maxWidth: pageFormat.width - _margin.horizontal,
maxHeight: pageFormat.height - _margin.vertical);
final Theme calculatedTheme = theme ?? document.theme ?? Theme.base();
final ThemeData calculatedTheme =
theme ?? document.theme ?? ThemeData.base();
final Context context = Context(
document: document.document,
page: _pdfPage,
... ...
... ... @@ -40,7 +40,7 @@ class PageTheme {
final BuildCallback buildForeground;
final Theme theme;
final ThemeData theme;
final bool clip;
... ...
... ... @@ -441,26 +441,32 @@ class TextSpan extends InlineSpan {
class RichText extends Widget {
RichText(
{@required this.text,
this.textAlign = TextAlign.left,
this.softWrap = true,
TextAlign textAlign,
bool softWrap,
this.tightBounds = false,
this.textScaleFactor = 1.0,
this.maxLines})
: assert(text != null);
int maxLines})
: assert(text != null),
_textAlign = textAlign,
_softWrap = softWrap,
_maxLines = maxLines;
static bool debug = false;
final InlineSpan text;
final TextAlign textAlign;
TextAlign get textAlign => _textAlign;
TextAlign _textAlign;
final double textScaleFactor;
final bool softWrap;
bool get softWrap => _softWrap;
bool _softWrap;
final bool tightBounds;
final int maxLines;
int get maxLines => _maxLines;
int _maxLines;
final List<_Span> _spans = <_Span>[];
... ... @@ -524,7 +530,11 @@ class RichText extends Widget {
_spans.clear();
_decorations.clear();
final TextStyle defaultstyle = Theme.of(context).defaultTextStyle;
final ThemeData theme = Theme.of(context);
final TextStyle defaultstyle = theme.defaultTextStyle;
_softWrap ??= theme.softWrap;
_maxLines ??= theme.maxLines;
_textAlign ??= theme.textAlign;
final double constraintWidth = constraints.hasBoundedWidth
? constraints.maxWidth
... ... @@ -821,8 +831,8 @@ class Text extends RichText {
Text(
String text, {
TextStyle style,
TextAlign textAlign = TextAlign.left,
bool softWrap = true,
TextAlign textAlign,
bool softWrap,
bool tightBounds = false,
double textScaleFactor = 1.0,
int maxLines,
... ...
... ... @@ -19,8 +19,8 @@
part of widget;
@immutable
class Theme extends Inherited {
factory Theme({
class ThemeData extends Inherited {
factory ThemeData({
TextStyle defaultTextStyle,
TextStyle paragraphStyle,
TextStyle header0,
... ... @@ -32,8 +32,11 @@ class Theme extends Inherited {
TextStyle bulletStyle,
TextStyle tableHeader,
TextStyle tableCell,
bool softWrap,
TextAlign textAlign,
int maxLines,
}) {
final Theme base = Theme.base();
final ThemeData base = ThemeData.base();
return base.copyWith(
defaultTextStyle: defaultTextStyle,
paragraphStyle: paragraphStyle,
... ... @@ -46,10 +49,13 @@ class Theme extends Inherited {
header5: header5,
tableHeader: tableHeader,
tableCell: tableCell,
softWrap: softWrap,
textAlign: textAlign,
maxLines: maxLines,
);
}
Theme._({
ThemeData._({
@required this.defaultTextStyle,
@required this.paragraphStyle,
@required this.header0,
... ... @@ -61,6 +67,9 @@ class Theme extends Inherited {
@required this.bulletStyle,
@required this.tableHeader,
@required this.tableCell,
@required this.softWrap,
@required this.textAlign,
this.maxLines,
}) : assert(defaultTextStyle.inherit == false),
assert(paragraphStyle.inherit == false),
assert(header0.inherit == false),
... ... @@ -71,9 +80,12 @@ class Theme extends Inherited {
assert(header5.inherit == false),
assert(bulletStyle.inherit == false),
assert(tableHeader.inherit == false),
assert(tableCell.inherit == false);
assert(tableCell.inherit == false),
assert(softWrap != null),
assert(maxLines == null || maxLines > 0);
factory Theme.withFont({Font base, Font bold, Font italic, Font boldItalic}) {
factory ThemeData.withFont(
{Font base, Font bold, Font italic, Font boldItalic}) {
final TextStyle defaultStyle = TextStyle.defaultStyle().copyWith(
font: base,
fontNormal: base,
... ... @@ -82,7 +94,7 @@ class Theme extends Inherited {
fontBoldItalic: boldItalic);
final double fontSize = defaultStyle.fontSize;
return Theme._(
return ThemeData._(
defaultTextStyle: defaultStyle,
paragraphStyle: defaultStyle.copyWith(lineSpacing: 5),
bulletStyle: defaultStyle.copyWith(lineSpacing: 5),
... ... @@ -95,12 +107,14 @@ class Theme extends Inherited {
tableHeader: defaultStyle.copyWith(
fontSize: fontSize * 0.8, fontWeight: FontWeight.bold),
tableCell: defaultStyle.copyWith(fontSize: fontSize * 0.8),
softWrap: true,
textAlign: TextAlign.left,
);
}
factory Theme.base() => Theme.withFont();
factory ThemeData.base() => ThemeData.withFont();
Theme copyWith({
ThemeData copyWith({
TextStyle defaultTextStyle,
TextStyle paragraphStyle,
TextStyle header0,
... ... @@ -112,8 +126,11 @@ class Theme extends Inherited {
TextStyle bulletStyle,
TextStyle tableHeader,
TextStyle tableCell,
bool softWrap,
TextAlign textAlign,
int maxLines,
}) =>
Theme._(
ThemeData._(
defaultTextStyle: this.defaultTextStyle.merge(defaultTextStyle),
paragraphStyle: this.paragraphStyle.merge(paragraphStyle),
bulletStyle: this.bulletStyle.merge(bulletStyle),
... ... @@ -125,12 +142,11 @@ class Theme extends Inherited {
header5: this.header5.merge(header5),
tableHeader: this.tableHeader.merge(tableHeader),
tableCell: this.tableCell.merge(tableCell),
softWrap: softWrap ?? this.softWrap,
textAlign: textAlign ?? this.textAlign,
maxLines: maxLines ?? this.maxLines,
);
static Theme of(Context context) {
return context.inherited[Theme];
}
final TextStyle defaultTextStyle;
final TextStyle paragraphStyle;
... ... @@ -147,4 +163,102 @@ class Theme extends Inherited {
final TextStyle tableHeader;
final TextStyle tableCell;
final TextAlign textAlign;
final bool softWrap;
final int maxLines;
}
class Theme extends StatelessWidget {
Theme({
@required this.data,
@required this.child,
}) : assert(data != null),
assert(child != null);
final ThemeData data;
final Widget child;
static ThemeData of(Context context) {
return context.inherited[ThemeData];
}
@Deprecated('Use ThemeData.base()')
static ThemeData base() => ThemeData.base();
@Deprecated('Use ThemeData.withFont()')
static ThemeData withFont(
{Font base, Font bold, Font italic, Font boldItalic}) =>
ThemeData.withFont(
base: base, bold: bold, italic: italic, boldItalic: boldItalic);
@override
Widget build(Context context) {
return InheritedWidget(
inherited: data,
build: (Context context) => child,
);
}
}
class DefaultTextStyle extends StatelessWidget implements Inherited {
DefaultTextStyle({
@required this.style,
@required this.child,
this.textAlign,
this.softWrap = true,
this.maxLines,
}) : assert(style != null),
assert(child != null),
assert(softWrap != null),
assert(maxLines == null || maxLines > 0);
static Widget merge({
TextStyle style,
TextAlign textAlign,
bool softWrap,
int maxLines,
@required Widget child,
}) {
assert(child != null);
return Builder(
builder: (Context context) {
final ThemeData parent = Theme.of(context);
return DefaultTextStyle(
style: parent.defaultTextStyle.merge(style),
textAlign: textAlign ?? parent.textAlign,
softWrap: softWrap ?? parent.softWrap,
maxLines: maxLines ?? parent.maxLines,
child: child,
);
},
);
}
final TextStyle style;
final Widget child;
final TextAlign textAlign;
final bool softWrap;
final int maxLines;
@override
Widget build(Context context) {
final ThemeData theme = Theme.of(context).copyWith(
defaultTextStyle: style,
textAlign: textAlign,
softWrap: softWrap,
maxLines: maxLines,
);
return InheritedWidget(
inherited: theme,
build: (Context context) => child,
);
}
}
... ...
... ... @@ -38,7 +38,7 @@ void main() {
pdf = Document(
title: 'Widgets Test',
theme: Theme.withFont(
theme: ThemeData.withFont(
base: Font.ttf(defaultFont.buffer.asByteData()),
bold: Font.ttf(defaultFontBold.buffer.asByteData()),
));
... ...
... ... @@ -73,7 +73,7 @@ void main() {
});
test('Theme Page 1', () {
final Theme theme = Theme.withFont(base: roboto);
final ThemeData theme = ThemeData.withFont(base: roboto);
pdf.addPage(Page(
theme: theme,
... ... @@ -84,7 +84,7 @@ void main() {
});
test('Theme Page 2', () {
final Theme theme = Theme.base().copyWith(
final ThemeData theme = ThemeData.base().copyWith(
tableHeader: TextStyle(font: openSansBold),
tableCell: TextStyle(font: roboto),
);
... ... @@ -107,11 +107,11 @@ void main() {
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text('Hello default'),
InheritedWidget(
inherited: Theme.withFont(
Theme(
data: ThemeData.withFont(
base: roboto,
),
build: (Context context) => Text('Hello themed'),
child: Text('Hello themed'),
),
],
),
... ... @@ -124,7 +124,7 @@ void main() {
pageFormat: PdfPageFormat.a4,
orientation: PageOrientation.portrait,
margin: const EdgeInsets.all(8.0),
theme: Theme(
theme: ThemeData(
defaultTextStyle: TextStyle(font: Font.courier(), fontSize: 10.0),
),
build: (Context context) {
... ...