mirror of
https://github.com/supabase/supabase.git
synced 2026-10-09 19:35:06 +03:00
* Refactor login_page with a _showSnackBar function * Add an onError block to onAuthStateChange Errors get swallowed without this error handler, I noticed it when I had requested too many magic link emails, there was no feedback in the ui, nor in the logs in the console. * Fix SnackBars not being displayed and add a _showSnackBar function * Fix code samples for login_page.dart and account_page.dart to reflect fixes in #27104 * Fix isError not being passed for an Unexpected Error in login_page * Update flutter user management example to a more modern code * Update the guide to match the new content * run formatter * got rid of excess code and minor format fix --------- Co-authored-by: dshukertjr <dshukertjr@gmail.com>
90 lines
2.3 KiB
Dart
90 lines
2.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
import 'package:supabase_quickstart/main.dart';
|
|
|
|
class Avatar extends StatefulWidget {
|
|
const Avatar({
|
|
super.key,
|
|
required this.imageUrl,
|
|
required this.onUpload,
|
|
});
|
|
|
|
final String? imageUrl;
|
|
final void Function(String) onUpload;
|
|
|
|
@override
|
|
State<Avatar> createState() => _AvatarState();
|
|
}
|
|
|
|
class _AvatarState extends State<Avatar> {
|
|
bool _isLoading = false;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Column(
|
|
children: [
|
|
if (widget.imageUrl == null || widget.imageUrl!.isEmpty)
|
|
Container(
|
|
width: 150,
|
|
height: 150,
|
|
color: Colors.grey,
|
|
child: const Center(
|
|
child: Text('No Image'),
|
|
),
|
|
)
|
|
else
|
|
Image.network(
|
|
widget.imageUrl!,
|
|
width: 150,
|
|
height: 150,
|
|
fit: BoxFit.cover,
|
|
),
|
|
ElevatedButton(
|
|
onPressed: _isLoading ? null : _upload,
|
|
child: const Text('Upload'),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Future<void> _upload() async {
|
|
final picker = ImagePicker();
|
|
final imageFile = await picker.pickImage(
|
|
source: ImageSource.gallery,
|
|
maxWidth: 300,
|
|
maxHeight: 300,
|
|
);
|
|
if (imageFile == null) {
|
|
return;
|
|
}
|
|
setState(() => _isLoading = true);
|
|
|
|
try {
|
|
final bytes = await imageFile.readAsBytes();
|
|
final fileExt = imageFile.path.split('.').last;
|
|
final fileName = '${DateTime.now().toIso8601String()}.$fileExt';
|
|
final filePath = fileName;
|
|
await supabase.storage.from('avatars').uploadBinary(
|
|
filePath,
|
|
bytes,
|
|
fileOptions: FileOptions(contentType: imageFile.mimeType),
|
|
);
|
|
final imageUrlResponse = await supabase.storage
|
|
.from('avatars')
|
|
.createSignedUrl(filePath, 60 * 60 * 24 * 365 * 10);
|
|
widget.onUpload(imageUrlResponse);
|
|
} on StorageException catch (error) {
|
|
if (mounted) {
|
|
context.showSnackBar(error.message, isError: true);
|
|
}
|
|
} catch (error) {
|
|
if (mounted) {
|
|
context.showSnackBar('Unexpected error occurred', isError: true);
|
|
}
|
|
}
|
|
|
|
setState(() => _isLoading = false);
|
|
}
|
|
}
|