David PHAM-VAN

Implement ListView.builder and ListView.separated

# Changelog
## 1.3.23
- Implement ListView.builder and ListView.separated
## 1.3.22
- Fix Text alignment
... ...
... ... @@ -546,17 +546,99 @@ class Spacer extends StatelessWidget {
}
}
class ListView extends Flex {
ListView(
{Axis direction = Axis.vertical,
// EdgeInsets padding,
// double spacing = 0.0,
List<Widget> children = const <Widget>[]})
: super(
typedef IndexedWidgetBuilder = Widget Function(Context context, int index);
class ListView extends StatelessWidget {
ListView({
this.direction = Axis.vertical,
this.reverse = false,
this.spacing = 0,
this.padding,
this.children = const <Widget>[],
}) : itemBuilder = null,
separatorBuilder = null,
itemCount = children.length,
super();
ListView.builder({
this.direction = Axis.vertical,
this.reverse = false,
this.spacing = 0,
this.padding,
@required this.itemBuilder,
@required this.itemCount,
}) : children = null,
separatorBuilder = null,
super();
ListView.separated({
this.direction = Axis.vertical,
this.reverse = false,
this.padding,
@required this.itemBuilder,
@required this.separatorBuilder,
@required this.itemCount,
}) : children = null,
spacing = null,
super();
final Axis direction;
final EdgeInsets padding;
final double spacing;
final bool reverse;
final IndexedWidgetBuilder itemBuilder;
final IndexedWidgetBuilder separatorBuilder;
final List<Widget> children;
final int itemCount;
Widget _getItem(Context context, int index) {
return children == null ? itemBuilder(context, index) : children[index];
}
Widget _getSeparator(Context context, int index) {
return spacing == null
? separatorBuilder(context, index)
: direction == Axis.vertical
? SizedBox(height: spacing)
: SizedBox(width: spacing);
}
@override
Widget build(Context context) {
final List<Widget> _children = <Widget>[];
if (reverse) {
for (int index = itemCount - 1; index >= 0; index--) {
_children.add(_getItem(context, index));
if (spacing != 0 && index > 0) {
_children.add(_getSeparator(context, index));
}
}
} else {
for (int index = 0; index < itemCount; index++) {
_children.add(_getItem(context, index));
if (spacing != 0 && index < itemCount - 1) {
_children.add(_getSeparator(context, index));
}
}
}
final Widget widget = Flex(
direction: direction,
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
verticalDirection: VerticalDirection.down,
children: children);
children: _children,
);
if (padding != null) {
return Padding(
padding: padding,
child: widget,
);
}
return widget;
}
}
... ...
... ... @@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl
homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf
repository: https://github.com/DavBfr/dart_pdf
issue_tracker: https://github.com/DavBfr/dart_pdf/issues
version: 1.3.22
version: 1.3.23
environment:
sdk: ">=2.1.0 <3.0.0"
... ...
... ... @@ -29,6 +29,7 @@ import 'type1_test.dart' as type1;
import 'widget_basic_test.dart' as widget_basic;
import 'widget_clip_test.dart' as widget_clip;
import 'widget_container_test.dart' as widget_container;
import 'widget_flex_test.dart' as widget_flex;
import 'widget_multipage_test.dart' as widget_multipage;
import 'widget_table_test.dart' as widget_table;
import 'widget_test.dart' as widget;
... ... @@ -51,6 +52,7 @@ void main() {
widget_basic.main();
widget_clip.main();
widget_container.main();
widget_flex.main();
widget_multipage.main();
widget_table.main();
widget_text.main();
... ...
/*
* Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import 'dart:io';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart';
import 'package:test/test.dart';
Document pdf;
void main() {
setUpAll(() {
Document.debug = true;
pdf = Document();
});
test('Flex Widgets ListView', () {
pdf.addPage(
Page(
build: (Context context) => ListView(
spacing: 20,
padding: const EdgeInsets.all(10),
children: <Widget>[
Text('Line 1'),
Text('Line 2'),
Text('Line 3'),
],
),
),
);
});
test('Flex Widgets ListView.builder', () {
pdf.addPage(
Page(
build: (Context context) => ListView.builder(
itemBuilder: (Context context, int index) => Text('Line $index'),
itemCount: 30,
spacing: 2,
reverse: true,
),
),
);
});
test('Flex Widgets ListView.separated', () {
pdf.addPage(
Page(
build: (Context context) => ListView.separated(
separatorBuilder: (Context context, int index) => Container(
color: PdfColors.grey,
height: 0.5,
margin: const EdgeInsets.symmetric(vertical: 10),
),
itemBuilder: (Context context, int index) => Text('Line $index'),
itemCount: 10,
),
),
);
});
tearDownAll(() {
final File file = File('widgets-flex.pdf');
file.writeAsBytesSync(pdf.save());
});
}
... ...