Skip to content

Instantly share code, notes, and snippets.

@huxaiphaer
Created May 13, 2020 22:06
Show Gist options
  • Save huxaiphaer/faf46591ac82758a98ebb3c16240f02e to your computer and use it in GitHub Desktop.
Save huxaiphaer/faf46591ac82758a98ebb3c16240f02e to your computer and use it in GitHub Desktop.
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:newvisionapp/src/models/authentication/google_auth_response.dart';
import 'package:newvisionapp/src/resources/local_providers/profile_preference_provider.dart';
import 'package:newvisionapp/src/resources/repository/profile/change_name_repository.dart';
import 'package:newvisionapp/src/ui/profile/edit_profile.dart';
import 'package:progress_indicators/progress_indicators.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:toast/toast.dart';
class ChangeName extends StatefulWidget {
final int userId;
const ChangeName({Key key, this.userId}) : super(key: key);
@override
_ChangeNameState createState() => _ChangeNameState();
}
class _ChangeNameState extends State<ChangeName> {
final _formKey = GlobalKey<FormState>();
final _firstNameController = TextEditingController();
final _lastNameController = TextEditingController();
var changeNameRepository = ChangeNameRepository();
bool _isVisibleLoader = false;
bool _isVisibleSaveChangesButton = true;
var status;
Future<SharedPreferences> _pref = SharedPreferences.getInstance();
static const String USER = "USER";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
iconTheme: IconThemeData(
color: Color(0xFFF7DA00), //change your color here
),
title: Text('Change Name'),
),
body: Container(
child: _changePasswordForm(context),
),
);
}
void _showToastMessage(String message) {
Toast.show(message, context,
duration: Toast.LENGTH_LONG,
backgroundRadius: 3.0,
backgroundColor: Colors.black,
gravity: Toast.BOTTOM);
}
Widget _changePasswordForm(BuildContext context) {
final bloc = Provider.of<PreferenceProvider>(context).bloc;
return Form(
key: _formKey,
child: Container(
margin: EdgeInsets.all(25.0),
child: Column(
children: <Widget>[
TextFormField(
controller: _firstNameController,
decoration: const InputDecoration(labelText: "Change First Name"),
validator: (value) {
if (value.isEmpty) {
return 'please enter your first name';
}
if (value.length < 3) {
return 'please enter more thatn 3 letters';
}
if (value.contains('@')) {
return 'please don\'t use symbols';
}
return null;
},
),
SizedBox(
height: 20,
),
TextFormField(
controller: _lastNameController,
decoration: const InputDecoration(labelText: "Change Last Name"),
validator: (value) {
if (value.isEmpty) {
return 'please enter your last name';
}
if (value.length < 3) {
return 'please enter more thatn 3 letters';
}
return null;
},
),
SizedBox(
height: 20,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
children: <Widget>[
Visibility(
visible: _isVisibleSaveChangesButton,
child: StreamBuilder<AuthResponse>(
builder: (context, snapshot) {
if (!snapshot.hasData){
return Container();
}
return FlatButton(
// ignore: sdk_version_ui_as_code
onPressed: () async => {
{
setState(() {
_isVisibleLoader = true;
_isVisibleSaveChangesButton = false;
})
},
if (_formKey.currentState.validate())
{
changeNameRepository
.editName(
_firstNameController.text,
_lastNameController.text,
'${widget.userId}')
.then((value) async {
bloc.editProfileDetails(value);
_showToastMessage(
"Name changed successfully.");
setState(() {
_isVisibleLoader = true;
_isVisibleSaveChangesButton = false;
});
Navigator.pop(context);
})
}
},
child: Text(
'SAVE CHANGES',
textAlign: TextAlign.right,
style: TextStyle(
color: Color(0xFFD2232A),
letterSpacing: 1.25,
fontSize: 18,
fontWeight: FontWeight.w500,
),
),
);
},
),
),
Visibility(
visible: _isVisibleLoader,
child: JumpingDotsProgressIndicator(
milliseconds: 200,
fontSize: 35.0,
color: Colors.red[500],
),
)
],
),
FlatButton(
onPressed: () {
Navigator.pop(context);
},
child: Text(
'CANCEL',
textAlign: TextAlign.right,
style: TextStyle(
color: Color(0xFF424242),
letterSpacing: 1.25,
fontSize: 18,
fontWeight: FontWeight.w500,
),
),
)
],
),
SizedBox(
height: 20,
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment