A comprehensive Flutter application demonstrating image upload functionality with multiple methods and UI options.
- Multiple Image Selection - Camera, Gallery, and Multiple images
- Various Upload Methods - Single, Multiple, and Base64 uploads
- Progress Tracking - Real-time upload progress with visual indicators
- Different UI Screens - Multiple demo screens showcasing different approaches
- Error Handling - Comprehensive error handling and user feedback
- Free Testing - Uses httpbin.org for free API testing
The app includes several demo screens:
- Main navigation screen
- Simple image upload
- Enhanced upload with progress
- Multiple upload options
- Flutter SDK (3.0 or higher)
- Dart SDK
- Android Studio / VS Code
- Android device or emulator
-
Clone the repository
git clone <your-repo-url> cd tessssts
-
Install dependencies
flutter pub get
-
Run the app
flutter run
The app uses the following key packages:
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.8
dio: ^5.4.0 # HTTP client for uploads
image_picker: ^1.0.7 # Image selection from camera/gallery
path: ^1.8.3 # Path manipulation utilities
path_provider: ^2.1.2 # Platform-specific directories
-
Services
ImageUploadService
- Handles all upload operationsApiClient
- Simplified API interface
-
Screens
main.dart
- Navigation and main screenImageUploadScreen
- Full-featured upload demoEnhancedImageUploadScreen
- Advanced upload optionsSimpleUploadDemo
- Basic upload example
final response = await ImageUploadService.uploadImage(
url: 'https://httpbin.org/post',
imageFile: selectedImage,
fieldName: 'file',
onSendProgress: (sent, total) {
// Update progress
},
);
final response = await ImageUploadService.uploadMultipleImages(
url: 'https://httpbin.org/post',
imageFiles: selectedImages,
fieldName: 'file',
onSendProgress: (sent, total) {
// Update progress
},
);
final response = await ImageUploadService.uploadImageAsBase64(
url: 'https://httpbin.org/post',
imageFile: selectedImage,
fieldName: 'file',
headers: {'Content-Type': 'application/json'},
);
The app currently uses httpbin.org for testing:
- Base URL:
https://httpbin.org
- Endpoint:
/post
- Method:
POST
- Content-Type:
multipart/form-data
orapplication/json
- Field Name:
file
- Authentication: Not required
To use your own API, update the configuration in your upload methods:
// In your upload calls, change:
url: 'https://httpbin.org/post'
// To:
url: 'https://your-api.com/upload'
// And add authentication if needed:
headers: {
'Authorization': 'Bearer your-token',
'Content-Type': 'multipart/form-data',
}
// Pick an image
final XFile? image = await ImageUploadService.pickImage(
source: ImageSource.gallery,
);
// Upload the image
if (image != null) {
final response = await ImageUploadService.uploadImage(
url: 'https://httpbin.org/post',
imageFile: image,
fieldName: 'file',
);
if (response?.statusCode == 200) {
print('Upload successful!');
}
}
await ImageUploadService.uploadImage(
url: 'https://httpbin.org/post',
imageFile: selectedImage,
fieldName: 'file',
onSendProgress: (int sent, int total) {
double progress = sent / total;
print('Upload progress: ${(progress * 100).toStringAsFixed(1)}%');
},
);
- Camera: Take a photo directly
- Gallery: Select from device gallery
- Multiple: Select multiple images at once
- Linear progress bar during upload
- Percentage display
- Status messages
- Network error messages
- File size validation
- Format validation
- Retry mechanisms
Edit lib/services/image_upload_service.dart
:
// Configure Dio settings
ImageUploadService.configureDio(
baseUrl: 'https://your-api.com',
connectTimeout: const Duration(seconds: 30),
receiveTimeout: const Duration(seconds: 30),
sendTimeout: const Duration(minutes: 5),
);
static Future<Response?> customUpload({
required String url,
required XFile imageFile,
Map<String, String>? customHeaders,
}) async {
// Your custom upload logic
}
- β Android - Fully supported
- β iOS - Fully supported
β οΈ Web - Limited (file picker restrictions)β οΈ Desktop - Limited (file picker restrictions)
-
"Unexpected field" error
- Check field name matches API expectations
- Verify Content-Type header
-
Upload timeout
- Increase timeout settings in Dio configuration
- Check network connection
-
Permission denied
- Ensure camera/storage permissions in
android/app/src/main/AndroidManifest.xml
- Ensure camera/storage permissions in
Enable detailed logging by setting:
// In image_upload_service.dart
print('Upload progress: ${(sent / total * 100).toStringAsFixed(1)}%');
print('Response: ${response.data}');
This project is open source and available under the MIT License.
- Fork the project
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
If you have any questions or issues:
- Create an issue in the repository
- Check the troubleshooting section
- Review the Flutter documentation
- v1.0.0 - Initial release with basic upload functionality
- v1.1.0 - Added multiple upload methods and progress tracking
- v1.2.0 - Enhanced UI and error handling
- v1.3.0 - Switched to httpbin.org for free testing
Made with β€οΈ using Flutter