Carsten Fregin
Committed by David PHAM-VAN

Add Choice Field

... ... @@ -9,6 +9,7 @@
- Update Image dependency
- Fix lints
- Add options to customise border in Dataset widget [838]
- Add Choice Field [Carsten Fregin]
## 3.8.4
... ...
... ... @@ -31,6 +31,73 @@ import 'object.dart';
import 'object_dict.dart';
import 'page.dart';
class PdfChoiceField extends PdfAnnotWidget {
PdfChoiceField({
required PdfRect rect,
required this.textColor,
required this.font,
required this.fontSize,
required this.items,
String? fieldName,
this.value,
this.defaultValue,
}) : super(
rect: rect,
fieldType: '/Ch',
fieldName: fieldName,
);
final List<String> items;
final PdfColor textColor;
final String? value;
final String? defaultValue;
final Set<PdfFieldFlags>? fieldFlags = {
PdfFieldFlags.combo,
};
final PdfFont font;
final double fontSize;
@override
void build(PdfPage page, PdfObject object, PdfDict params) {
super.build(page, object, params);
// What is /F?
//params['/F'] = const PdfNum(4);
params['/Ff'] = PdfNum(fieldFlagsValue);
params['/Opt'] =
PdfArray<PdfString>(items.map((e) => PdfString.fromString(e)).toList());
if (defaultValue != null) {
params['/DV'] = PdfString.fromString(defaultValue!);
}
if (value != null) {
params['/V'] = PdfString.fromString(value!);
} else {
params['/V'] = const PdfNull();
}
final buf = PdfStream();
final g = PdfGraphics(page, buf);
g.setFillColor(textColor);
g.setFont(font, fontSize);
params['/DA'] = PdfSecString.fromStream(object, buf);
// What is /TU? Tooltip?
//params['/TU'] = PdfString.fromString('Select from list');
}
int get fieldFlagsValue {
if (fieldFlags == null || fieldFlags!.isEmpty) {
return 0;
}
return fieldFlags!
.map<int>((PdfFieldFlags e) => 1 << e.index)
.reduce((int a, int b) => a | b);
}
}
class PdfAnnot extends PdfObjectDict {
PdfAnnot(this.pdfPage, this.annot)
: super(pdfPage.pdfDocument, type: '/Annot') {
... ...
... ... @@ -31,6 +31,46 @@ import 'text_style.dart';
import 'theme.dart';
import 'widget.dart';
class ChoiceField extends StatelessWidget {
ChoiceField({
this.width = 120,
this.height = 13,
this.textStyle,
required this.name,
required this.items,
this.value,
});
final String name;
final TextStyle? textStyle;
final double width;
final double height;
final List<String> items;
final String? value;
@override
void paint(Context context) {
super.paint(context);
final _textStyle = Theme.of(context).defaultTextStyle.merge(textStyle);
final pdfRect = context.localToGlobal(box!);
final bf = PdfChoiceField(
textColor: _textStyle.color!,
fieldName: name,
value: value,
font: _textStyle.font!.getFont(context),
fontSize: _textStyle.fontSize!,
items: items,
rect: pdfRect,
);
PdfAnnot(context.page, bf);
}
@override
Widget build(Context context) {
return SizedBox(width: width, height: height);
}
}
class Checkbox extends SingleChildWidget {
Checkbox({
required this.value,
... ...
... ... @@ -98,6 +98,17 @@ void main() {
Decorated(child: TextField(name: 'Address')),
//
SizedBox(width: double.infinity, height: 10),
Label(label: 'ChoiceField:', width: 100),
Decorated(
child: ChoiceField(name: 'Test Choice', items: [
'One',
'Two',
'Blue',
'Yellow',
'Test äöüß',
])),
//
SizedBox(width: double.infinity, height: 10),
//
Label(label: 'Postcode:', width: 100),
Decorated(
... ... @@ -114,6 +125,7 @@ void main() {
name: 'Country',
color: PdfColors.blue,
)),
//
SizedBox(width: double.infinity, height: 10),
//
... ...