Skip to content

Instantly share code, notes, and snippets.

@horgag1
Created April 24, 2019 22:47
Show Gist options
  • Save horgag1/8ad580b7a12c7dad181215f34c3460e3 to your computer and use it in GitHub Desktop.
Save horgag1/8ad580b7a12c7dad181215f34c3460e3 to your computer and use it in GitHub Desktop.
import 'dart:async';
import 'dart:io';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:flutter_chat_demo/const.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:image_picker/image_picker.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'auth_provider.dart';
import 'package:flutter_chat_demo/user_data.dart';
class Settings extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(
'SETTINGS',
style: TextStyle(color: primaryColor, fontWeight: FontWeight.bold),
),
centerTitle: true,
),
body: new SettingsScreen(),
);
}
}
class SettingsScreen extends StatefulWidget {
@override
State createState() => new SettingsScreenState();
}
class SettingsScreenState extends State<SettingsScreen> {
TextEditingController controllerName;
TextEditingController controllerEmail;
TextEditingController controllerPhoneNumber;
SharedPreferences prefs;
String id = '';
String name = '';
String email = '';
String avatarUrl = '';
bool isLoading = false;
File avatarImageFile;
final FocusNode focusName = new FocusNode();
final FocusNode focusEmail = new FocusNode();
final FocusNode focusPhoneNumber= new FocusNode();
@override
void initState() {
super.initState();
}
@override
void didChangeDependencies() {
readLocal();
}
void readLocal() async {
controllerName = new TextEditingController(text: AuthProvider.of(context).userData.name);
print(controllerName);
controllerEmail = new TextEditingController(text: AuthProvider.of(context).userData.email);
avatarUrl= AuthProvider.of(context).userData.avatarUrl;
// controllerPhoneNumber= new TextEditingController(text: AuthProvider.of(context).userData.phoneNumber);
// Force refresh input
setState(() {});
}
Future getImage() async {
File image = await ImagePicker.pickImage(source: ImageSource.gallery);
if (image != null) {
setState(() {
avatarImageFile = image;
isLoading = true;
});
}
//uploadFile();
}
Future uploadFile() async {
String fileName = id;
StorageReference reference = FirebaseStorage.instance.ref().child(fileName);
StorageUploadTask uploadTask = reference.putFile(avatarImageFile);
StorageTaskSnapshot storageTaskSnapshot;
uploadTask.onComplete.then((value) {
if (value.error == null) {
storageTaskSnapshot = value;
storageTaskSnapshot.ref.getDownloadURL().then((downloadUrl) {
avatarUrl = downloadUrl;
AuthProvider.of(context).userData.docRef.updateData({'email': email,'name': name, 'avatarUrl': avatarUrl}).then((data) async {
//await prefs.setString('avatarUrl', avatarUrl);
setState(() {
isLoading = false;
});
Fluttertoast.showToast(msg: "Upload success");
}).catchError((err) {
setState(() {
isLoading = false;
});
Fluttertoast.showToast(msg: err.toString());
});
}, onError: (err) {
setState(() {
isLoading = false;
});
Fluttertoast.showToast(msg: 'This file is not an image');
});
} else {
setState(() {
isLoading = false;
});
Fluttertoast.showToast(msg: 'This file is not an image');
}
}, onError: (err) {
setState(() {
isLoading = false;
});
Fluttertoast.showToast(msg: err.toString());
});
}
void handleUpdateData() {
focusName.unfocus();
focusEmail.unfocus();
setState(() {
isLoading = true;
});
AuthProvider.of(context).userData.docRef.updateData({'name': name, 'email': email,'avatarUrl':avatarUrl}).then((data) async{
print('@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@');
setState(() {
isLoading = false;
});
Fluttertoast.showToast(msg: "Update success");
}).catchError((err) {
setState(() {
isLoading = false;
});
Fluttertoast.showToast(msg: err.toString());
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
SingleChildScrollView(
child: Column(
children: <Widget>[
// Avatar
Container(
child: Center(
child: Stack(
children: <Widget>[
(avatarImageFile == null)
? (avatarUrl != ''
? Material(
child: CachedNetworkImage(
placeholder: (context, url) => Container(
child: CircularProgressIndicator(
strokeWidth: 2.0,
valueColor: AlwaysStoppedAnimation<Color>(themeColor),
),
width: 90.0,
height: 90.0,
padding: EdgeInsets.all(20.0),
),
imageUrl: avatarUrl,
width: 90.0,
height: 90.0,
fit: BoxFit.cover,
),
borderRadius: BorderRadius.all(Radius.circular(45.0)),
clipBehavior: Clip.hardEdge,
)
: Icon(
Icons.account_circle,
size: 90.0,
color: greyColor,
))
: Material(
child: Image.file(
avatarImageFile,
width: 90.0,
height: 90.0,
fit: BoxFit.cover,
),
borderRadius: BorderRadius.all(Radius.circular(45.0)),
clipBehavior: Clip.hardEdge,
),
IconButton(
icon: Icon(
Icons.camera_alt,
color: primaryColor.withOpacity(0.5),
),
onPressed: getImage,
padding: EdgeInsets.all(30.0),
splashColor: Colors.transparent,
highlightColor: greyColor,
iconSize: 30.0,
),
],
),
),
width: double.infinity,
margin: EdgeInsets.all(20.0),
),
// Input
Column(
children: <Widget>[
// Username
Container(
child: Text(
'Nickname',
style: TextStyle(fontStyle: FontStyle.italic, fontWeight: FontWeight.bold, color: primaryColor),
),
margin: EdgeInsets.only(left: 10.0, bottom: 5.0, top: 10.0),
),
Container(
child: Theme(
data: Theme.of(context).copyWith(primaryColor: primaryColor),
child: TextField(
decoration: InputDecoration(
contentPadding: new EdgeInsets.all(5.0),
hintStyle: TextStyle(color: greyColor),
),
controller: controllerName,
onChanged: (value) {
name = value;
},
focusNode: focusName,
),
),
margin: EdgeInsets.only(left: 30.0, right: 30.0),
),
// About me
Container(
child: Text(
'Email',
style: TextStyle(fontStyle: FontStyle.italic, fontWeight: FontWeight.bold, color: primaryColor),
),
margin: EdgeInsets.only(left: 10.0, top: 30.0, bottom: 5.0),
),
Container(
child: Theme(
data: Theme.of(context).copyWith(primaryColor: primaryColor),
child: TextField(
decoration: InputDecoration(
hintText: 'Fun, like travel and play PES...',
contentPadding: EdgeInsets.all(5.0),
hintStyle: TextStyle(color: greyColor),
),
controller: controllerEmail,
onChanged: (value) {
email = value;
},
focusNode: focusEmail,
),
),
margin: EdgeInsets.only(left: 30.0, right: 30.0),
),
],
crossAxisAlignment: CrossAxisAlignment.start,
),
// Button
Container(
child: FlatButton(
onPressed: handleUpdateData,
child: Text(
'UPDATE',
style: TextStyle(fontSize: 16.0),
),
color: primaryColor,
highlightColor: new Color(0xff8d93a0),
splashColor: Colors.transparent,
textColor: Colors.white,
padding: EdgeInsets.fromLTRB(30.0, 10.0, 30.0, 10.0),
),
margin: EdgeInsets.only(top: 50.0, bottom: 50.0),
),
],
),
padding: EdgeInsets.only(left: 15.0, right: 15.0),
),
// Loading
Positioned(
child: isLoading
? Container(
child: Center(
child: CircularProgressIndicator(valueColor: AlwaysStoppedAnimation<Color>(themeColor)),
),
color: Colors.white.withOpacity(0.8),
)
: Container(),
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment