Skip to content

Commit

Permalink
Merge pull request #108 from D-Ajay-Kumar/Add-image-picker
Browse files Browse the repository at this point in the history
Added image picker
  • Loading branch information
avinashkranjan committed Mar 25, 2021
2 parents 5102166 + 862aa47 commit 2ab88f4
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 71 deletions.
2 changes: 2 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

<uses-permission android:name="android.permission.INTERNET"/>
<application

android:requestLegacyExternalStorage= "true"
android:name="io.flutter.app.FlutterApplication"
android:label="Friday"
android:icon="@mipmap/launcher_icon">
Expand Down
1 change: 1 addition & 0 deletions android/settings_aar.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ':app'
4 changes: 4 additions & 0 deletions ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- Added for image picker -->
<key>NSPhotoLibraryUsageDescription</key>
<string>Access gallery to pick image</string>

<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
Expand Down
113 changes: 67 additions & 46 deletions lib/screens/profile_screen.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';

import 'package:class_manager/constants.dart';
import 'package:class_manager/models/users.dart';
import 'package:class_manager/screens/onboarding_page.dart';
Expand All @@ -6,25 +8,63 @@ import 'package:class_manager/services/facebookAuthentication.dart';
import 'package:class_manager/services/googleAuthentication.dart';
import 'package:class_manager/services/user_info_services.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path/path.dart' as Path;

class ProfileScreen extends StatefulWidget {
@override
_ProfileScreenState createState() => _ProfileScreenState();
}

class _ProfileScreenState extends State<ProfileScreen> {
ImagePicker _imagePicker = ImagePicker();
StorageReference _storageReference = FirebaseStorage.instance.ref();

void getImage() async {
PickedFile image = await _imagePicker.getImage(source: ImageSource.gallery);

if (image == null) {
return;
}

String imagePath = image.path;

File file = File(imagePath);

uploadPictures(file);
}

uploadPictures(File image) async {
// uploads picture(s) to storage and return it's URL
final StorageReference ref =
_storageReference.child('${Path.basename(image.path)}}');

final StorageUploadTask uploadTask = ref.putFile(image);

UploadTaskSnapshot uploadTaskSnapshot = await uploadTask.future;
String pictureUrl = uploadTaskSnapshot.downloadUrl.toString();

final userInfoProvider =
Provider.of<UserInfoServices>(context, listen: false);

Users currentUser = userInfoProvider.user;
currentUser.profilePictureUrl = pictureUrl;

userInfoProvider.setUser(currentUser);

userInfoProvider.upateProfilePictureUrl();
}

@override
Widget build(BuildContext context) {
final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
final User userData = firebaseAuth.currentUser;

return Scaffold(
backgroundColor: Theme
.of(context)
.backgroundColor
.withOpacity(0.8),
backgroundColor: Theme.of(context).backgroundColor.withOpacity(0.8),
body: SingleChildScrollView(
physics: AlwaysScrollableScrollPhysics(parent: BouncingScrollPhysics()),
child: Consumer<UserInfoServices>(
Expand All @@ -36,17 +76,12 @@ class _ProfileScreenState extends State<ProfileScreen> {
children: [
Column(
children: [
SizedBox(height: 0.15 * MediaQuery
.of(context)
.size
.height),
SizedBox(height: 0.15 * MediaQuery.of(context).size.height),
Container(
margin: EdgeInsets.fromLTRB(15, 15, 15, 60),
padding: EdgeInsets.all(30),
decoration: BoxDecoration(
color: Theme
.of(context)
.primaryColor,
color: Theme.of(context).primaryColor,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(40),
topRight: Radius.circular(40),
Expand Down Expand Up @@ -139,8 +174,9 @@ class _ProfileScreenState extends State<ProfileScreen> {
// For Sign Out from Google Auth or Facebook Auth, Back to the On Boarding Page
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (_) => OnboardingPage()),
(Route<dynamic> route) => false,
MaterialPageRoute(
builder: (_) => OnboardingPage()),
(Route<dynamic> route) => false,
);
}
}
Expand All @@ -151,18 +187,10 @@ class _ProfileScreenState extends State<ProfileScreen> {
horizontal: 20, vertical: 10),
shape: StadiumBorder(),
color: Colors.transparent,
hoverColor: Theme
.of(context)
.primaryColor,
splashColor: Theme
.of(context)
.primaryColor,
focusColor: Theme
.of(context)
.primaryColor,
highlightColor: Theme
.of(context)
.primaryColor,
hoverColor: Theme.of(context).primaryColor,
splashColor: Theme.of(context).primaryColor,
focusColor: Theme.of(context).primaryColor,
highlightColor: Theme.of(context).primaryColor,
child: Text(
"Log out",
style: TextStyle(
Expand All @@ -178,35 +206,28 @@ class _ProfileScreenState extends State<ProfileScreen> {
],
),
Positioned(
top: 0.1 * MediaQuery
.of(context)
.size
.height,
top: 0.1 * MediaQuery.of(context).size.height,
child: CircleAvatar(
radius: profilePictureDiameter / 2,
backgroundImage:
AssetImage("assets/images/profile_pic.jpg"),
backgroundImage: _user.profilePictureUrl.isEmpty
? AssetImage("assets/images/profile_pic.jpg")
: NetworkImage(_user.profilePictureUrl),
backgroundColor: Colors.transparent,
foregroundColor: Theme
.of(context)
.backgroundColor,
foregroundColor: Theme.of(context).backgroundColor,
),
),
Positioned(
top: 0.1 * MediaQuery
.of(context)
.size
.height +
top: 0.1 * MediaQuery.of(context).size.height +
profilePictureDiameter -
35,
left: (MediaQuery
.of(context)
.size
.width / 2) + 25,
child: Icon(
Icons.camera_alt,
size: profilePictureDiameter * 0.25,
color: Colors.white,
left: (MediaQuery.of(context).size.width / 2) + 25,
child: IconButton(
icon: Icon(
Icons.camera_alt,
size: profilePictureDiameter * 0.25,
color: Colors.white,
),
onPressed: getImage,
),
),
],
Expand Down
11 changes: 11 additions & 0 deletions lib/services/user_db_services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ class UserDBServices {
.set(_user.toJson(), SetOptions(merge: true));
}

static Future<void> updateProfilePictureUrl(
String uid, String profilePictureUrl) async {
FirebaseFirestore firestoreDB = FirebaseFirestore.instance;

await firestoreDB.collection(usersCollection).doc(uid).update(
{
'profilePictureUrl': profilePictureUrl,
},
);
}

// Fetches User Data from Collection
static Future<bool> fetchUserData(BuildContext context) async {
FirebaseFirestore firestoreDB = FirebaseFirestore.instance;
Expand Down
6 changes: 6 additions & 0 deletions lib/services/user_info_services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class UserInfoServices extends ChangeNotifier {
notifyListeners();
}

Future<void> upateProfilePictureUrl() async {
await UserDBServices.updateProfilePictureUrl(
_user.uid, _user.profilePictureUrl);
// notifyListeners();
}

void setUser(Users _usr) {
this._user = _usr;
this.hasData = true;
Expand Down
51 changes: 27 additions & 24 deletions lib/widgets/header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,40 @@ class Header extends StatelessWidget {
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.fromLTRB(30.0, 50.0, 30.0, 30.0),

child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Flexible(
child:SvgPicture.asset(
"assets/icons/grad_cap.svg",
height: 70.0,
),
),
Flexible(
child:Consumer<UserInfoServices>(
builder: (ctx, _userInfo, _) => FittedBox(
fit: BoxFit.fitWidth,
child: Text(
"Hello, " +
(_userInfo.hasData
? _userInfo.user.name.split(" ")[0]
: "Sir"),
style: TextStyle(
color: Colors.white,
fontSize: 25.0,
fontWeight: FontWeight.bold,
),
),
child: SvgPicture.asset(
"assets/icons/grad_cap.svg",
height: 70.0,
),
),
Consumer<UserInfoServices>(
builder: (ctx, _userInfo, _) => Row(
children: [
FittedBox(
fit: BoxFit.fitWidth,
child: Text(
"Hello, " +
(_userInfo.hasData
? _userInfo.user.name.split(" ")[0]
: "Sir"),
style: TextStyle(
color: Colors.white,
fontSize: 25.0,
fontWeight: FontWeight.bold,
),
),
),
CircleAvatar(
radius: 25.0,
backgroundImage: _userInfo.user.profilePictureUrl.isEmpty
? AssetImage("assets/images/profile_pic.jpg")
: NetworkImage(_userInfo.user.profilePictureUrl),
),
],
),
CircleAvatar(
radius: 25.0,
backgroundImage: AssetImage("assets/images/profile_pic.jpg"),
),
],
),
Expand Down
Loading

0 comments on commit 2ab88f4

Please sign in to comment.