Skip to content

Commit

Permalink
Fixed flash on remove/add image
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham16g committed Dec 19, 2022
1 parent cd186a2 commit 8d0d39a
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 33 deletions.
3 changes: 0 additions & 3 deletions .flutter-plugins

This file was deleted.

1 change: 0 additions & 1 deletion .flutter-plugins-dependencies

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ migrate_working_dir/
.dart_tool/
.packages
build/
.flutter-plugins
.flutter-plugins-dependencies
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 0.0.7
- Added support for default images (pre selected images)
- Added support for default images 🖼️ (pre selected images)
- Solved white flash 🔦 problem occurs on add/remove image

## 0.0.6
- 🪲 Fixed some bugs
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.6"
version: "0.0.7"
path:
dependency: transitive
description:
Expand Down
3 changes: 2 additions & 1 deletion lib/src/image_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:typed_data';

/// Store the image data and other information.
class ImageFile {
final String key;
final String name;
final String extension;
final Uint8List? bytes;
Expand All @@ -13,6 +14,6 @@ class ImageFile {
/// returns size of bytes if image has bytes, else 0.
int get size => bytes?.length ?? 0;

ImageFile(
ImageFile(this.key,
{required this.name, required this.extension, this.bytes, this.path});
}
2 changes: 1 addition & 1 deletion lib/src/multi_image_picker_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class MultiImagePickerController with ChangeNotifier {
.where((e) =>
e.extension != null &&
allowedImageTypes.contains(e.extension?.toLowerCase()))
.map((e) => ImageFile(
.map((e) => ImageFile(UniqueKey().toString(),
name: e.name,
extension: e.extension!,
bytes: e.bytes,
Expand Down
51 changes: 26 additions & 25 deletions lib/src/multi_image_picker_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,39 @@ class MultiImagePickerView extends StatefulWidget {

final SliverGridDelegate? gridDelegate;

// final images = <String>[];

@override
State<MultiImagePickerView> createState() => _MultiImagePickerViewState();
}

class _MultiImagePickerViewState extends State<MultiImagePickerView> {
late ScrollController scrollController;
final gridViewKey = GlobalKey();
final selectorKey = GlobalKey();

@override
Widget build(BuildContext context) {
_pickImages() async {
final result = await widget.controller.pickImages();
if (!result) return;
if (widget.onChange != null) {
widget.onChange!(widget.controller.images);
}
void initState() {
super.initState();
scrollController = ScrollController();
widget.controller.addListener(updateUi);
}

void _pickImages() async {
final result = await widget.controller.pickImages();
if (!result) return;
if (widget.onChange != null) {
widget.onChange!(widget.controller.images);
}
}

void _deleteImage(ImageFile imageFile) {
widget.controller.removeImage(imageFile);
if (widget.onChange != null) {
widget.onChange!(widget.controller.images);
}
void _deleteImage(ImageFile imageFile) {
widget.controller.removeImage(imageFile);
if (widget.onChange != null) {
widget.onChange!(widget.controller.images);
}
}

@override
Widget build(BuildContext context) {
if (widget.controller.hasNoImages) {
return widget.initialContainerBuilder != null
? widget.initialContainerBuilder!(context, _pickImages)
Expand Down Expand Up @@ -89,7 +98,7 @@ class _MultiImagePickerViewState extends State<MultiImagePickerView> {
);
}
final selector = SizedBox(
key: UniqueKey(),
key: selectorKey,
child: widget.addMoreBuilder != null
? widget.addMoreBuilder!(context, _pickImages)
: Container(
Expand All @@ -116,9 +125,6 @@ class _MultiImagePickerViewState extends State<MultiImagePickerView> {
),
);

final scrollController = ScrollController();
final gridViewKey = GlobalKey();

return Padding(
padding: widget.padding ?? EdgeInsets.zero,
child: ReorderableBuilder(
Expand Down Expand Up @@ -173,7 +179,7 @@ class _MultiImagePickerViewState extends State<MultiImagePickerView> {
},
children: widget.controller.images
.map<Widget>((e) => SizedBox(
key: UniqueKey(),
key: Key(e.key),
child: widget.itemBuilder != null
? widget.itemBuilder!(context, e, _deleteImage)
: _ItemView(file: e, onDelete: _deleteImage),
Expand All @@ -186,12 +192,6 @@ class _MultiImagePickerViewState extends State<MultiImagePickerView> {
);
}

@override
void initState() {
super.initState();
widget.controller.addListener(updateUi);
}

@override
void didUpdateWidget(MultiImagePickerView? oldWidget) {
if (oldWidget == null) return;
Expand All @@ -213,6 +213,7 @@ class _MultiImagePickerViewState extends State<MultiImagePickerView> {
@override
void dispose() {
widget.controller.removeListener(updateUi);
scrollController.dispose();
super.dispose();
}
}
Expand Down

0 comments on commit 8d0d39a

Please sign in to comment.