-
-
Notifications
You must be signed in to change notification settings - Fork 325
feat: completed image section in Add Excercise Screen #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rolandgeider
merged 2 commits into
wger-project:feature/exercise-crowdsourcing
from
agwanyaseen:feature/exercise-crowdsourcing
Jan 18, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import 'package:flutter/foundation.dart'; | ||
|
||
import 'dart:io'; | ||
|
||
class AddExcerciseProvider with ChangeNotifier { | ||
List<File> get excerciseImages => [..._excerciseImages]; | ||
final List<File> _excerciseImages = []; | ||
|
||
void addExcerciseImages(List<File> excercizes) { | ||
_excerciseImages.addAll(excercizes); | ||
notifyListeners(); | ||
} | ||
|
||
void removeExcercise(String path) { | ||
final file = _excerciseImages.where((element) => element.path == path).first; | ||
_excerciseImages.remove(file); | ||
notifyListeners(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:image_picker/image_picker.dart'; | ||
import 'package:provider/provider.dart'; | ||
import 'package:wger/providers/add_excercise_provider.dart'; | ||
|
||
const validFileExtensions = ['jpg', 'jpeg', 'png', 'webp']; | ||
const maxFileSize = 20; | ||
|
||
mixin ExcerciseImagePickerMixin { | ||
bool _validateFileType(int fileLength) { | ||
final kb = fileLength / 1024; | ||
final mb = kb / 1024; | ||
return mb > maxFileSize; | ||
} | ||
|
||
bool _validateFileSize(File file) { | ||
final extension = file.path.split('.').last; | ||
return validFileExtensions.any((element) => extension == element.toLowerCase()); | ||
} | ||
|
||
void pickImages(BuildContext context) async { | ||
final imagePicker = ImagePicker(); | ||
final images = await imagePicker.pickMultiImage(); | ||
final selectedImages = <File>[]; | ||
if (images != null) { | ||
selectedImages.addAll(images.map((e) => File(e.path)).toList()); | ||
|
||
for (final image in selectedImages) { | ||
bool isFileValid = true; | ||
String errorMessage = ''; | ||
|
||
if (!_validateFileSize(image)) { | ||
isFileValid = false; | ||
errorMessage = "Select only 'jpg', 'jpeg', 'png', 'webp' files"; | ||
} | ||
if (_validateFileType(image.lengthSync())) { | ||
isFileValid = true; | ||
errorMessage = 'File Size should not be greater than 20 mb'; | ||
} | ||
|
||
if (!isFileValid) { | ||
showDialog(context: context, builder: (context) => Text(errorMessage)); | ||
return; | ||
} | ||
} | ||
context.read<AddExcerciseProvider>().addExcerciseImages(selectedImages); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:provider/provider.dart'; | ||
import 'package:wger/widgets/add_exercise/mixins/image_picker_mixin.dart'; | ||
import '../../providers/add_excercise_provider.dart'; | ||
|
||
class PreviewExcercizeImages extends StatelessWidget with ExcerciseImagePickerMixin { | ||
PreviewExcercizeImages({ | ||
Key? key, | ||
required this.selectedimages, | ||
}) : super(key: key); | ||
|
||
final List<File> selectedimages; | ||
@override | ||
Widget build(BuildContext context) { | ||
return SizedBox( | ||
height: 300, | ||
child: ListView(scrollDirection: Axis.horizontal, children: [ | ||
...selectedimages | ||
.map( | ||
(file) => SizedBox( | ||
height: 200, | ||
child: Padding( | ||
padding: const EdgeInsets.all(8.0), | ||
child: Stack( | ||
children: [ | ||
Image.file(file), | ||
Positioned( | ||
bottom: 0, | ||
right: 0, | ||
child: Padding( | ||
padding: const EdgeInsets.all(3.0), | ||
child: Container( | ||
decoration: BoxDecoration( | ||
color: Colors.grey.withOpacity(0.5), | ||
borderRadius: const BorderRadius.all(Radius.circular(20))), | ||
child: IconButton( | ||
iconSize: 20, | ||
onPressed: () => | ||
context.read<AddExcerciseProvider>().removeExcercise(file.path), | ||
color: Colors.white, | ||
icon: const Icon(Icons.delete), | ||
), | ||
), | ||
), | ||
), | ||
], | ||
), | ||
), | ||
), | ||
) | ||
.toList(), | ||
const SizedBox( | ||
width: 10, | ||
), | ||
Padding( | ||
padding: const EdgeInsets.all(8.0), | ||
child: Container( | ||
color: Colors.grey, | ||
height: 200, | ||
width: 100, | ||
child: Center( | ||
child: IconButton( | ||
icon: const Icon(Icons.add), | ||
onPressed: () => pickImages(context), | ||
), | ||
), | ||
), | ||
) | ||
]), | ||
); | ||
} | ||
} | ||
rolandgeider marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.