Navigation in flutter
I want to navigate the Sign-in page to ListView Screen using the private method name is _navigateToListScreen but I am unable to od this I did not know the way how the private function call in the same class
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:signinproject/commen_widget/custome_elevate_button.dart';
import 'list_view.dart';
import 'list_view.dart';
class SignInpage extends StatelessWidget {
const SignInpage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Sign In Screen', textAlign: TextAlign.end),
elevation: 10,
),
body: _buildContent(),
backgroundColor: Colors.grey[300],
);
}
Widget _buildContent() {
return Padding(
padding: const EdgeInsets.all(10.0),
//color: Colors.cyan,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Text(
'Sign In',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 32.0, fontWeight: FontWeight.w400),
),
const SizedBox(
height: 15.0,
),
CustomeElevatedButton(
child: Row(
//crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Image.asset('Assets/Images/google.png'),
const Text(
'Sign In with Google',
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
Opacity(
child: Image.asset('Assets/Images/google.png'),
opacity: 0.0,
),
],
),
color: Colors.white,
borderRadius: 20.0,
onPressed: () {},
),
const SizedBox(
height: 16.0,
),
CustomeElevatedButton(
child: Row(
//crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Image.asset('Assets/Images/facebook.png'),
const Text(
'Sign In with Facebook',
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
Opacity(
child: Image.asset('Assets/Images/facebook.png'),
opacity: 0.0,
),
],
),
color: Colors.blue,
borderRadius: 20.0,
onPressed: () {},
),
const SizedBox(
height: 16.0,
),
CustomeElevatedButton(
child: const Text(
'Sign in with Email',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
color: Colors.blueGrey,
borderRadius: 20.0,
onPressed: () {}),
const SizedBox(
height: 8.00,
),
const Text(
'or',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
// fontFamily:f
),
),
const SizedBox(
height: 8.0,
),
CustomeElevatedButton(
child: const Text(
'Get Anomous',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.black),
),
color: Colors.limeAccent,
borderRadius: 20.0,
onPressed: () {}),
ElevatedButton(
onPressed: () {
// SignInpage()._navigateToListScreen(context);
// debugPrint('clicked me');
//_navigateToListScreen();
},
child: const Text('Go to next Screen'),
)
],
),
);
}
void _navigateToListScreen(BuildContext context) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ListViewA(),
),
);
}
}
And the other screen that i want to move is code is following
//import 'dart:html';
import 'package:flutter/material.dart';
class ListViewA extends StatelessWidget {
//ListViewA({Key? key}) : super(key: key);
final activeuser = [
'Tahir',
'Naseem',
'Tanveer',
'Ahktar',
'Hamayoun',
'saima'
];
ListViewA({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('List view Screen'),
),
body: ListView.builder(
itemBuilder: (context, index) {
return ListTile(
title: Text(activeuser[index]),
);
},
itemCount: activeuser.length,
));
}
}
CodePudding user response:
You need to pass context to _buildContent() as well.
Here's the updated code.
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:signinproject/commen_widget/custome_elevate_button.dart';
import 'list_view.dart';
import 'list_view.dart';
class SignInpage extends StatelessWidget {
const SignInpage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Sign In Screen', textAlign: TextAlign.end),
elevation: 10,
),
//pass context
body: _buildContent(context),
backgroundColor: Colors.grey[300],
);
}
Widget _buildContent(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(10.0),
//color: Colors.cyan,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Text(
'Sign In',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 32.0, fontWeight: FontWeight.w400),
),
const SizedBox(
height: 15.0,
),
CustomeElevatedButton(
child: Row(
//crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Image.asset('Assets/Images/google.png'),
const Text(
'Sign In with Google',
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
Opacity(
child: Image.asset('Assets/Images/google.png'),
opacity: 0.0,
),
],
),
color: Colors.white,
borderRadius: 20.0,
onPressed: () {},
),
const SizedBox(
height: 16.0,
),
CustomeElevatedButton(
child: Row(
//crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Image.asset('Assets/Images/facebook.png'),
const Text(
'Sign In with Facebook',
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
Opacity(
child: Image.asset('Assets/Images/facebook.png'),
opacity: 0.0,
),
],
),
color: Colors.blue,
borderRadius: 20.0,
onPressed: () {},
),
const SizedBox(
height: 16.0,
),
CustomeElevatedButton(
child: const Text(
'Sign in with Email',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
color: Colors.blueGrey,
borderRadius: 20.0,
onPressed: () {}),
const SizedBox(
height: 8.00,
),
const Text(
'or',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
// fontFamily:f
),
),
const SizedBox(
height: 8.0,
),
CustomeElevatedButton(
child: const Text(
'Get Anomous',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.black),
),
color: Colors.limeAccent,
borderRadius: 20.0,
onPressed: () {}),
ElevatedButton(
onPressed: () {
//pass context
_navigateToListScreen(context);
// SignInpage()._navigateToListScreen(context);
// debugPrint('clicked me');
},
child: const Text('Go to next Screen'),
)
],
),
);
}
void _navigateToListScreen(BuildContext context) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ListViewA(),
),
);
}
}
CodePudding user response:
Remove context from your builder method in MaterialRoute .And use the code in your onPressed just like this
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => ListViewA()));
}
