OK... poniżej załączam trochę moich wypocin z tutoriali z googla :)
Nie mogę sobie poradzić z przejściem ze screena LoginPage do HomePage (po poprawnym zalogowaniu).
Próbowałem: Navigator.of(context).push(MaterialPageRoute(builder: (context) => HomePage()));
ale dostałem błąd: "type 'Future' is not a subtype of type '(() => dynamic)?'"
Kopiuj
class LoginPage extends StatelessWidget {
LoginPage({super.key});
final emailController = TextEditingController();
final passwordController = TextEditingController();
void signUserIn() async {
try{
Response response = await post(
Uri.parse('https://www.somepage.com/login.php'),
body: {
'email' : emailController.text,
'password' : passwordController.text
}
);
if(response.statusCode == 200){
var data = jsonDecode(response.body.toString());
if(data['success'] == 1) {
print(data['message']);
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('email', emailController.text);
prefs.setString('token', data['token']);
// ---> tutaj chciałbym przejść do HomePage
} else {
print(data['status']);
print(data['message']);
}
}else {
print('Login failed...');
print(response.statusCode);
}
}catch(e){
print(e.toString());
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[300],
body: SafeArea(
child: Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(height: 25),
const Icon(
Icons.lock,
size: 100,
),
const SizedBox(height: 25),
Text(
'Witamy ponownie!',
style: TextStyle(
color: Colors.grey[700],
fontSize: 16,
),
),
const SizedBox(height: 25),
MyTextField(
controller: emailController,
hintText: 'Email',
obscureText: false,
),
const SizedBox(height: 10),
MyTextField(
controller: passwordController,
hintText: 'Hasło',
obscureText: true,
),
const SizedBox(height: 10),
const Padding(
padding: EdgeInsets.symmetric(horizontal: 25.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text(
'Nie pamiętasz hasła?',
style: TextStyle(color: Colors.blue, fontWeight: FontWeight.bold),
),
],
),
),
const SizedBox(height: 20),
MyButton(
onTap: signUserIn,
),
const SizedBox(height: 30),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: Row(
children: [
Expanded(
child: Divider(
thickness: 0.5,
color: Colors.grey[400],
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: Text(
'Albo kontynuuj z',
style: TextStyle(color: Colors.grey[700]),
),
),
Expanded(
child: Divider(
thickness: 0.5,
color: Colors.grey[400],
),
),
],
),
),
const SizedBox(height: 30),
const Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SquareTile(imagePath: 'lib/images/google.png'),
SizedBox(width: 10),
SquareTile(imagePath: 'lib/images/apple.png')
],
),
const SizedBox(height: 30),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Nie masz konta?',
style: TextStyle(color: Colors.grey[700]),
),
const SizedBox(width: 4),
const Text(
'Zarejestruj się',
style: TextStyle(color: Colors.blue, fontWeight: FontWeight.bold),
),
],
)
],
),
),
),
),
);
}
}
Kopiuj
class HomePage extends StatelessWidget {
HomePage({super.key});
void signUserOut() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.remove('email');
prefs.remove('token');
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[300],
appBar: AppBar(
backgroundColor: Colors.grey[900],
actions: [
IconButton(
onPressed: signUserOut,
icon: Icon(Icons.logout),
)
],
),
body: Center(
child: Text(
"TEST",
style: TextStyle(fontSize: 20),
)),
);
}
}