Technology1 สิงหาคม 2564
Workshop: Flutter Auth Login #part8 send ScreenArguments to EditProfilePage

Loading...
visibility278 views

Create ScreenArguments.dart
class ScreenArguments {
final String name;
final String surname;
ScreenArguments(this.name, this.surname);
}
ProfilePage.dart
import 'ScreenArguments.dart';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ProfilePage'),
actions: [
IconButton(
onPressed: () {
_openEditPage();
},
icon: Icon(Icons.edit))
],
),
...
_openEditPage() {
//open EditProfilePage with ScreenArguments
Navigator.pushNamed(context, '/editprofile',
arguments: ScreenArguments(
profile['name'],
profile['surname'],
));
}
EditProfilePage.dart
import 'ScreenArguments.dart';
@override
Widget build(BuildContext context) {
final screenAgr =
ModalRoute.of(context)!.settings.arguments as ScreenArguments;
return Scaffold(
appBar: AppBar(
Text('${screenAgr.name} ${screenAgr.surname}')
),
Run & Debug
add FormBuider to EditProfilePage.dart and pull name , surename from ScreenArguments
@override
Widget build(BuildContext context) {
final screenAgr =
ModalRoute.of(context)!.settings.arguments as ScreenArguments;
...
FormBuilder(
key: _formKey,
initialValue: {
'name': screenAgr.name,
'surname': screenAgr.surname,
},
...
Run & Debug
ProfilePage.dart
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:convert' as convert;
import 'ScreenArguments.dart';
class ProfilePage extends StatefulWidget {
ProfilePage({Key? key}) : super(key: key);
@override
_ProfilePageState createState() => _ProfilePageState();
}
class _ProfilePageState extends State
{
SharedPreferences? sharePrefs;
Map profile = {'username': '', 'name': '', "surname": ''};
_getSharedPreferences() async {
sharePrefs = await SharedPreferences.getInstance();
var profileString = sharePrefs!.getString('profile');
print('profileString');
print(profileString);
if (profileString != null) {
setState(() {
profile = convert.jsonDecode(profileString);
});
}
}
@override
void initState() {
super.initState();
_getSharedPreferences();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ProfilePage'),
actions: ,
),
body: Container(
padding: const EdgeInsets.all(20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: } ',
style: TextStyle(fontSize: 20, color: Colors.grey)),
SizedBox(height: 20),
Text('name: ${profile} ',
style: TextStyle(fontSize: 20, color: Colors.grey)),
SizedBox(height: 20),
Text('surname: ${profile} ',
style: TextStyle(fontSize: 20, color: Colors.grey)),
SizedBox(height: 40),
MaterialButton(
onPressed: () {
_logout();
},
child:
Text("Logout", style: TextStyle(color: Colors.blue)),
),
],
),
],
)));
}
_logout() async {
sharePrefs = await SharedPreferences.getInstance();
await sharePrefs!.remove('token');
await sharePrefs!.remove('profile');
Navigator.of(context, rootNavigator: true)
.pushNamedAndRemoveUntil('/login', (route) => false);
}
_openEditPage() {
//open EditProfilePage with ScreenArguments
Navigator.pushNamed(context, '/editprofile',
arguments: ScreenArguments(
profile,
profile,
));
}
}
EditProfilePage.dart
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'ScreenArguments.dart';
class EditProfilePage extends StatefulWidget {
EditProfilePage({Key? key}) : super(key: key);
@override
_EditProfilePageState createState() => _EditProfilePageState();
}
class _EditProfilePageState extends State {
final _formKey = GlobalKey();
@override
Widget build(BuildContext context) {
final screenAgr =
ModalRoute.of(context)!.settings.arguments as ScreenArguments;
return Scaffold(
appBar: AppBar(title: Text('${screenAgr.name} ${screenAgr.surname}')),
body: Container(
child: SingleChildScrollView(
child: Column(
children: ),
),
SizedBox(height: 15),
FormBuilderTextField(
name: 'surname',
decoration: InputDecoration(
labelText: 'surname',
filled: true,
),
validator: FormBuilderValidators.compose(),
),
SizedBox(height: 15),
Row(
children: ,
)
],
),
),
)
],
),
),
));
}
}