David PHAM-VAN

Fix Theme creation

@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 ## 1.3.22 3 ## 1.3.22
4 4
5 - Fix Text alignment 5 - Fix Text alignment
  6 +- Fix Theme creation
6 7
7 ## 1.3.21 8 ## 1.3.21
8 9
@@ -111,7 +111,10 @@ class TextStyle { @@ -111,7 +111,10 @@ class TextStyle {
111 this.decorationStyle, 111 this.decorationStyle,
112 this.decorationThickness, 112 this.decorationThickness,
113 }) : assert(inherit || color != null), 113 }) : assert(inherit || color != null),
114 - assert(inherit || font != null), 114 + assert(inherit || fontNormal != null),
  115 + assert(inherit || fontBold != null),
  116 + assert(inherit || fontItalic != null),
  117 + assert(inherit || fontBoldItalic != null),
115 assert(inherit || fontSize != null), 118 assert(inherit || fontSize != null),
116 assert(inherit || fontWeight != null), 119 assert(inherit || fontWeight != null),
117 assert(inherit || fontStyle != null), 120 assert(inherit || fontStyle != null),
@@ -120,7 +123,6 @@ class TextStyle { @@ -120,7 +123,6 @@ class TextStyle {
120 assert(inherit || lineSpacing != null), 123 assert(inherit || lineSpacing != null),
121 assert(inherit || height != null), 124 assert(inherit || height != null),
122 assert(inherit || decoration != null), 125 assert(inherit || decoration != null),
123 - assert(inherit || decorationColor != null),  
124 assert(inherit || decorationStyle != null), 126 assert(inherit || decorationStyle != null),
125 assert(inherit || decorationThickness != null), 127 assert(inherit || decorationThickness != null),
126 fontNormal = fontNormal ?? 128 fontNormal = fontNormal ??
@@ -142,6 +144,7 @@ class TextStyle { @@ -142,6 +144,7 @@ class TextStyle {
142 144
143 factory TextStyle.defaultStyle() { 145 factory TextStyle.defaultStyle() {
144 return TextStyle( 146 return TextStyle(
  147 + inherit: false,
145 color: PdfColors.black, 148 color: PdfColors.black,
146 fontNormal: Font.helvetica(), 149 fontNormal: Font.helvetica(),
147 fontBold: Font.helveticaBold(), 150 fontBold: Font.helveticaBold(),
@@ -18,7 +18,36 @@ part of widget; @@ -18,7 +18,36 @@ part of widget;
18 18
19 @immutable 19 @immutable
20 class Theme extends Inherited { 20 class Theme extends Inherited {
21 - const Theme({ 21 + factory Theme({
  22 + TextStyle defaultTextStyle,
  23 + TextStyle paragraphStyle,
  24 + TextStyle header0,
  25 + TextStyle header1,
  26 + TextStyle header2,
  27 + TextStyle header3,
  28 + TextStyle header4,
  29 + TextStyle header5,
  30 + TextStyle bulletStyle,
  31 + TextStyle tableHeader,
  32 + TextStyle tableCell,
  33 + }) {
  34 + final Theme base = Theme.base();
  35 + return base.copyWith(
  36 + defaultTextStyle: defaultTextStyle,
  37 + paragraphStyle: paragraphStyle,
  38 + bulletStyle: bulletStyle,
  39 + header0: header0,
  40 + header1: header1,
  41 + header2: header2,
  42 + header3: header3,
  43 + header4: header4,
  44 + header5: header5,
  45 + tableHeader: tableHeader,
  46 + tableCell: tableCell,
  47 + );
  48 + }
  49 +
  50 + Theme._({
22 @required this.defaultTextStyle, 51 @required this.defaultTextStyle,
23 @required this.paragraphStyle, 52 @required this.paragraphStyle,
24 @required this.header0, 53 @required this.header0,
@@ -30,7 +59,17 @@ class Theme extends Inherited { @@ -30,7 +59,17 @@ class Theme extends Inherited {
30 @required this.bulletStyle, 59 @required this.bulletStyle,
31 @required this.tableHeader, 60 @required this.tableHeader,
32 @required this.tableCell, 61 @required this.tableCell,
33 - }); 62 + }) : assert(defaultTextStyle.inherit == false),
  63 + assert(paragraphStyle.inherit == false),
  64 + assert(header0.inherit == false),
  65 + assert(header1.inherit == false),
  66 + assert(header2.inherit == false),
  67 + assert(header3.inherit == false),
  68 + assert(header4.inherit == false),
  69 + assert(header5.inherit == false),
  70 + assert(bulletStyle.inherit == false),
  71 + assert(tableHeader.inherit == false),
  72 + assert(tableCell.inherit == false);
34 73
35 factory Theme.withFont({Font base, Font bold, Font italic, Font boldItalic}) { 74 factory Theme.withFont({Font base, Font bold, Font italic, Font boldItalic}) {
36 final TextStyle defaultStyle = TextStyle.defaultStyle().copyWith( 75 final TextStyle defaultStyle = TextStyle.defaultStyle().copyWith(
@@ -41,7 +80,7 @@ class Theme extends Inherited { @@ -41,7 +80,7 @@ class Theme extends Inherited {
41 fontBoldItalic: boldItalic); 80 fontBoldItalic: boldItalic);
42 final double fontSize = defaultStyle.fontSize; 81 final double fontSize = defaultStyle.fontSize;
43 82
44 - return Theme( 83 + return Theme._(
45 defaultTextStyle: defaultStyle, 84 defaultTextStyle: defaultStyle,
46 paragraphStyle: defaultStyle.copyWith(lineSpacing: 5), 85 paragraphStyle: defaultStyle.copyWith(lineSpacing: 5),
47 bulletStyle: defaultStyle.copyWith(lineSpacing: 5), 86 bulletStyle: defaultStyle.copyWith(lineSpacing: 5),
@@ -53,7 +92,8 @@ class Theme extends Inherited { @@ -53,7 +92,8 @@ class Theme extends Inherited {
53 header5: defaultStyle.copyWith(fontSize: fontSize * 1.1), 92 header5: defaultStyle.copyWith(fontSize: fontSize * 1.1),
54 tableHeader: defaultStyle.copyWith( 93 tableHeader: defaultStyle.copyWith(
55 fontSize: fontSize * 0.8, fontWeight: FontWeight.bold), 94 fontSize: fontSize * 0.8, fontWeight: FontWeight.bold),
56 - tableCell: defaultStyle.copyWith(fontSize: fontSize * 0.8)); 95 + tableCell: defaultStyle.copyWith(fontSize: fontSize * 0.8),
  96 + );
57 } 97 }
58 98
59 factory Theme.base() => Theme.withFont(); 99 factory Theme.base() => Theme.withFont();
@@ -71,18 +111,19 @@ class Theme extends Inherited { @@ -71,18 +111,19 @@ class Theme extends Inherited {
71 TextStyle tableHeader, 111 TextStyle tableHeader,
72 TextStyle tableCell, 112 TextStyle tableCell,
73 }) => 113 }) =>
74 - Theme(  
75 - defaultTextStyle: defaultTextStyle ?? this.defaultTextStyle,  
76 - paragraphStyle: paragraphStyle ?? this.paragraphStyle,  
77 - bulletStyle: bulletStyle ?? this.bulletStyle,  
78 - header0: header0 ?? this.header0,  
79 - header1: header1 ?? this.header1,  
80 - header2: header2 ?? this.header2,  
81 - header3: header3 ?? this.header3,  
82 - header4: header4 ?? this.header4,  
83 - header5: header5 ?? this.header5,  
84 - tableHeader: tableHeader ?? this.tableHeader,  
85 - tableCell: tableCell ?? this.tableCell); 114 + Theme._(
  115 + defaultTextStyle: this.defaultTextStyle.merge(defaultTextStyle),
  116 + paragraphStyle: this.paragraphStyle.merge(paragraphStyle),
  117 + bulletStyle: this.bulletStyle.merge(bulletStyle),
  118 + header0: this.header0.merge(header0),
  119 + header1: this.header1.merge(header1),
  120 + header2: this.header2.merge(header2),
  121 + header3: this.header3.merge(header3),
  122 + header4: this.header4.merge(header4),
  123 + header5: this.header5.merge(header5),
  124 + tableHeader: this.tableHeader.merge(tableHeader),
  125 + tableCell: this.tableCell.merge(tableCell),
  126 + );
86 127
87 static Theme of(Context context) { 128 static Theme of(Context context) {
88 return context.inherited[Theme]; 129 return context.inherited[Theme];
@@ -117,6 +117,19 @@ void main() { @@ -117,6 +117,19 @@ void main() {
117 )); 117 ));
118 }); 118 });
119 119
  120 + test('Theme Page 4', () {
  121 + pdf.addPage(Page(
  122 + pageFormat: PdfPageFormat.a4,
  123 + orientation: PageOrientation.portrait,
  124 + margin: EdgeInsets.all(8.0),
  125 + theme: Theme(
  126 + defaultTextStyle: TextStyle(font: Font.courier(), fontSize: 10.0),
  127 + ),
  128 + build: (Context context) {
  129 + return Center(child: Text('Text'));
  130 + }));
  131 + });
  132 +
120 tearDownAll(() { 133 tearDownAll(() {
121 final File file = File('widgets-theme.pdf'); 134 final File file = File('widgets-theme.pdf');
122 file.writeAsBytesSync(pdf.save()); 135 file.writeAsBytesSync(pdf.save());