import 'package:flutter/material.dart'; import 'package:flutter_datetime_picker/flutter_datetime_picker.dart'; import 'package:intl/intl.dart'; import 'package:teso/Classes/customTesoButton.dart'; import 'package:teso/util/consts.dart'; import 'package:teso/Classes/TesoUser.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:provider/provider.dart'; import 'package:teso/providers/user_provider.dart'; import 'dart:convert'; class CompleteProfile extends StatefulWidget { @override _CompleteProfileState createState() => _CompleteProfileState(); } class _CompleteProfileState extends State { DateFormat dateFormat = DateFormat("EEEE dd-MM-yyyy"); DateTime selectedDate = DateTime.now(); String? selectedGender; List gender = ["Male", "Female", "Other"]; bool error = false; String message = ""; String? olduser; void changeDate(v) { setState(() { selectedDate = v; }); } @override void initState() { super.initState(); SharedPreferences.getInstance().then((prefs) async { olduser = prefs.getString("currentUser"); }); } completeProfile() async { if (selectedDate.year != DateTime.now().year || selectedGender == null || selectedGender!.isEmpty) { SharedPreferences.getInstance().then((prefs) async { Map user = jsonDecode(prefs.getString("currentUser")!) as Map; TesoUser currentuser = TesoUser.fromJSON(user); currentuser.dateOfBirth = selectedDate; currentuser.gender = selectedGender; Provider.of(context, listen: false) .updateUser(currentuser); }).whenComplete(() { SharedPreferences.getInstance().then((prefs) async { if (prefs.getString("currentUser") == null) { prefs.setString("currentUser", olduser!); setState(() { error = true; message = "Sorry, an error occurred while updating your profile please try again later !!!"; }); Future.delayed(const Duration(seconds: 5), () { if (error) { if (mounted) { setState(() { error = false; }); } } }); Navigator.pop(context); } else { Map user = jsonDecode(olduser!) as Map; TesoUser currentuser = TesoUser.fromJSON(user); currentuser.dateOfBirth = selectedDate; currentuser.gender = selectedGender; Provider.of(context, listen: false) .setUser(currentuser); Navigator.pop(context); } }); }); } else { setState(() { error = true; message = "Sorry, an error occurred please make sure to select the right date and select a gender !!!"; }); Future.delayed(const Duration(seconds: 5), () { if (error) { if (mounted) { setState(() { error = false; }); } } }); } } @override Widget build(BuildContext context) { return Scaffold( body: SingleChildScrollView( scrollDirection: Axis.vertical, child: Container( width: MediaQuery.of(context).size.width, padding: EdgeInsets.all(20), child: Column( children: [ Container( width: MediaQuery.of(context).size.width, height: 120, padding: EdgeInsets.symmetric(vertical: 20), child: Image( image: AssetImage("assets/images/tesoCouponInsignia.png"), ), ), SizedBox( height: 5, ), Container( width: MediaQuery.of(context).size.width, child: Center( child: Text( "Complete your profile", style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18), ), ), ), SizedBox( height: 40, ), Container( width: MediaQuery.of(context).size.width, // height: 10, child: Text( "When is your birthday ?", style: TextStyle( fontWeight: FontWeight.bold, ), ), ), SizedBox( height: 5, ), Container( alignment: Alignment.centerLeft, child: TextButton( onPressed: () { DatePicker.showDatePicker( context, showTitleActions: true, maxTime: DateTime.now(), onConfirm: (date) { changeDate(date); }, currentTime: selectedDate, locale: LocaleType.en, ); }, child: Text( dateFormat.format(selectedDate).toString(), style: TextStyle( fontWeight: FontWeight.normal, ), ), ), ), Divider(), SizedBox( height: 10, ), Container( width: MediaQuery.of(context).size.width, child: Text( "What is your gender ?", style: TextStyle( fontWeight: FontWeight.bold, ), ), ), Container( alignment: Alignment.centerLeft, child: DropdownButton( hint: Text("Select Gender"), value: selectedGender, items: gender .map( (gender) => DropdownMenuItem( value: gender, child: Text( gender, style: TextStyle( // color: Colors.grey, ), ), ), ) .toList(), onChanged: (v) { setState(() { selectedGender = v; }); }, ), ), Divider(), SizedBox( height: 10, ), Container( width: MediaQuery.of(context).size.width, child: Text( "Teso uses this data to personalize your experiences, to help business understand their customers, and more. " + "We will always keep your personal data private.", textAlign: TextAlign.center, ), ), SizedBox( height: 20, ), Visibility( visible: error, child: Container( width: MediaQuery.of(context).size.width, child: Text( message, style: TextStyle( color: Colors.red, ), ), ), ), RaisedGradientButton( onPressed: completeProfile, child: Text( "Submit", style: TextStyle( fontSize: 18, color: Colors.white, ), ), gradient: LinearGradient( begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [ accentMain, darkAccent, ], // stops: [0.1, 0.4, 0.7, 0.8], ), ), ], ), ), ), ); } }