TKK_E32210585_aplikasi/lib/ui/bottom_nav_pages/profile.dart

149 lines
4.3 KiB
Dart

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:e_commerce/const/AppColors.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
class Profile extends StatefulWidget {
@override
_ProfileState createState() => _ProfileState();
}
class _ProfileState extends State<Profile> {
TextEditingController? _nameController;
TextEditingController? _phoneController;
TextEditingController? _ageController;
setDataToTextField(data) {
return Column(
children: [
TextField(
style: TextStyle(
fontSize: 14,
),
controller: _nameController =
TextEditingController(text: data['name']),
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20.0),
borderSide: BorderSide(color: Colors.black),
),
hintText: 'Enter your email',
suffixIcon: const Icon(
Icons.person,
size: 24.0,
),
),
),
const SizedBox(
height: 10.0,
),
TextField(
style: TextStyle(
fontSize: 14,
),
controller: _phoneController =
TextEditingController(text: data['phone']),
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20.0),
borderSide: BorderSide(color: Colors.black),
),
hintText: 'Enter your phone number',
suffixIcon: const Icon(
Icons.phone,
size: 24.0,
),
),
),
const SizedBox(
height: 10.0,
),
TextField(
style: TextStyle(
fontSize: 14,
),
controller: _ageController = TextEditingController(text: data['age']),
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20.0),
borderSide: BorderSide(color: Colors.black),
),
hintText: 'Enter your age',
suffixIcon: const Icon(
Icons.person,
size: 24.0,
),
),
),
const SizedBox(
height: 10.0,
),
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: AppColors.deep_orange,
shape: ContinuousRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
),
onPressed: () => updateData(),
child: Text(
"Update",
style: TextStyle(
color: Colors.white,
),
),
),
],
);
}
updateData() {
CollectionReference _collectionRef =
FirebaseFirestore.instance.collection("users-form-data");
return _collectionRef.doc(FirebaseAuth.instance.currentUser!.email).update({
"name": _nameController!.text,
"phone": _phoneController!.text,
"age": _ageController!.text,
}).then((value) => Fluttertoast.showToast(
msg: "Updated Successfully",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.TOP,
backgroundColor: AppColors.deep_orange,
textColor: Colors.white,
fontSize: 16.0,
));
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: StreamBuilder(
stream: FirebaseFirestore.instance
.collection("users-form-data")
.doc(FirebaseAuth.instance.currentUser!.email)
.snapshots(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
var data = snapshot.data;
if (data == null) {
return Center(
child: CircularProgressIndicator(),
);
}
return setDataToTextField(data);
},
),
)),
);
}
}