David PHAM-VAN

Implement ListView.builder and ListView.separated

1 # Changelog 1 # Changelog
2 2
  3 +## 1.3.23
  4 +
  5 +- Implement ListView.builder and ListView.separated
  6 +
3 ## 1.3.22 7 ## 1.3.22
4 8
5 - Fix Text alignment 9 - Fix Text alignment
@@ -546,17 +546,99 @@ class Spacer extends StatelessWidget { @@ -546,17 +546,99 @@ class Spacer extends StatelessWidget {
546 } 546 }
547 } 547 }
548 548
549 -class ListView extends Flex {  
550 - ListView(  
551 - {Axis direction = Axis.vertical,  
552 - // EdgeInsets padding,  
553 - // double spacing = 0.0,  
554 - List<Widget> children = const <Widget>[]})  
555 - : super( 549 +typedef IndexedWidgetBuilder = Widget Function(Context context, int index);
  550 +
  551 +class ListView extends StatelessWidget {
  552 + ListView({
  553 + this.direction = Axis.vertical,
  554 + this.reverse = false,
  555 + this.spacing = 0,
  556 + this.padding,
  557 + this.children = const <Widget>[],
  558 + }) : itemBuilder = null,
  559 + separatorBuilder = null,
  560 + itemCount = children.length,
  561 + super();
  562 +
  563 + ListView.builder({
  564 + this.direction = Axis.vertical,
  565 + this.reverse = false,
  566 + this.spacing = 0,
  567 + this.padding,
  568 + @required this.itemBuilder,
  569 + @required this.itemCount,
  570 + }) : children = null,
  571 + separatorBuilder = null,
  572 + super();
  573 +
  574 + ListView.separated({
  575 + this.direction = Axis.vertical,
  576 + this.reverse = false,
  577 + this.padding,
  578 + @required this.itemBuilder,
  579 + @required this.separatorBuilder,
  580 + @required this.itemCount,
  581 + }) : children = null,
  582 + spacing = null,
  583 + super();
  584 +
  585 + final Axis direction;
  586 + final EdgeInsets padding;
  587 + final double spacing;
  588 + final bool reverse;
  589 + final IndexedWidgetBuilder itemBuilder;
  590 + final IndexedWidgetBuilder separatorBuilder;
  591 + final List<Widget> children;
  592 + final int itemCount;
  593 +
  594 + Widget _getItem(Context context, int index) {
  595 + return children == null ? itemBuilder(context, index) : children[index];
  596 + }
  597 +
  598 + Widget _getSeparator(Context context, int index) {
  599 + return spacing == null
  600 + ? separatorBuilder(context, index)
  601 + : direction == Axis.vertical
  602 + ? SizedBox(height: spacing)
  603 + : SizedBox(width: spacing);
  604 + }
  605 +
  606 + @override
  607 + Widget build(Context context) {
  608 + final List<Widget> _children = <Widget>[];
  609 +
  610 + if (reverse) {
  611 + for (int index = itemCount - 1; index >= 0; index--) {
  612 + _children.add(_getItem(context, index));
  613 + if (spacing != 0 && index > 0) {
  614 + _children.add(_getSeparator(context, index));
  615 + }
  616 + }
  617 + } else {
  618 + for (int index = 0; index < itemCount; index++) {
  619 + _children.add(_getItem(context, index));
  620 + if (spacing != 0 && index < itemCount - 1) {
  621 + _children.add(_getSeparator(context, index));
  622 + }
  623 + }
  624 + }
  625 +
  626 + final Widget widget = Flex(
556 direction: direction, 627 direction: direction,
557 mainAxisAlignment: MainAxisAlignment.start, 628 mainAxisAlignment: MainAxisAlignment.start,
558 mainAxisSize: MainAxisSize.max, 629 mainAxisSize: MainAxisSize.max,
559 crossAxisAlignment: CrossAxisAlignment.center, 630 crossAxisAlignment: CrossAxisAlignment.center,
560 verticalDirection: VerticalDirection.down, 631 verticalDirection: VerticalDirection.down,
561 - children: children); 632 + children: _children,
  633 + );
  634 +
  635 + if (padding != null) {
  636 + return Padding(
  637 + padding: padding,
  638 + child: widget,
  639 + );
  640 + }
  641 +
  642 + return widget;
  643 + }
562 } 644 }
@@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl @@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl
4 homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf 4 homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf
5 repository: https://github.com/DavBfr/dart_pdf 5 repository: https://github.com/DavBfr/dart_pdf
6 issue_tracker: https://github.com/DavBfr/dart_pdf/issues 6 issue_tracker: https://github.com/DavBfr/dart_pdf/issues
7 -version: 1.3.22 7 +version: 1.3.23
8 8
9 environment: 9 environment:
10 sdk: ">=2.1.0 <3.0.0" 10 sdk: ">=2.1.0 <3.0.0"
@@ -29,6 +29,7 @@ import 'type1_test.dart' as type1; @@ -29,6 +29,7 @@ import 'type1_test.dart' as type1;
29 import 'widget_basic_test.dart' as widget_basic; 29 import 'widget_basic_test.dart' as widget_basic;
30 import 'widget_clip_test.dart' as widget_clip; 30 import 'widget_clip_test.dart' as widget_clip;
31 import 'widget_container_test.dart' as widget_container; 31 import 'widget_container_test.dart' as widget_container;
  32 +import 'widget_flex_test.dart' as widget_flex;
32 import 'widget_multipage_test.dart' as widget_multipage; 33 import 'widget_multipage_test.dart' as widget_multipage;
33 import 'widget_table_test.dart' as widget_table; 34 import 'widget_table_test.dart' as widget_table;
34 import 'widget_test.dart' as widget; 35 import 'widget_test.dart' as widget;
@@ -51,6 +52,7 @@ void main() { @@ -51,6 +52,7 @@ void main() {
51 widget_basic.main(); 52 widget_basic.main();
52 widget_clip.main(); 53 widget_clip.main();
53 widget_container.main(); 54 widget_container.main();
  55 + widget_flex.main();
54 widget_multipage.main(); 56 widget_multipage.main();
55 widget_table.main(); 57 widget_table.main();
56 widget_text.main(); 58 widget_text.main();
  1 +/*
  2 + * Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
  17 +import 'dart:io';
  18 +
  19 +import 'package:pdf/pdf.dart';
  20 +import 'package:pdf/widgets.dart';
  21 +import 'package:test/test.dart';
  22 +
  23 +Document pdf;
  24 +
  25 +void main() {
  26 + setUpAll(() {
  27 + Document.debug = true;
  28 + pdf = Document();
  29 + });
  30 +
  31 + test('Flex Widgets ListView', () {
  32 + pdf.addPage(
  33 + Page(
  34 + build: (Context context) => ListView(
  35 + spacing: 20,
  36 + padding: const EdgeInsets.all(10),
  37 + children: <Widget>[
  38 + Text('Line 1'),
  39 + Text('Line 2'),
  40 + Text('Line 3'),
  41 + ],
  42 + ),
  43 + ),
  44 + );
  45 + });
  46 +
  47 + test('Flex Widgets ListView.builder', () {
  48 + pdf.addPage(
  49 + Page(
  50 + build: (Context context) => ListView.builder(
  51 + itemBuilder: (Context context, int index) => Text('Line $index'),
  52 + itemCount: 30,
  53 + spacing: 2,
  54 + reverse: true,
  55 + ),
  56 + ),
  57 + );
  58 + });
  59 +
  60 + test('Flex Widgets ListView.separated', () {
  61 + pdf.addPage(
  62 + Page(
  63 + build: (Context context) => ListView.separated(
  64 + separatorBuilder: (Context context, int index) => Container(
  65 + color: PdfColors.grey,
  66 + height: 0.5,
  67 + margin: const EdgeInsets.symmetric(vertical: 10),
  68 + ),
  69 + itemBuilder: (Context context, int index) => Text('Line $index'),
  70 + itemCount: 10,
  71 + ),
  72 + ),
  73 + );
  74 + });
  75 +
  76 + tearDownAll(() {
  77 + final File file = File('widgets-flex.pdf');
  78 + file.writeAsBytesSync(pdf.save());
  79 + });
  80 +}