I have a problem with changing page with Flutter. I created two .dart Files and I want to switch from page 1 to page 2 by a button. Can someone help me please?
Container(
margin: const EdgeInsets.fromLTRB(30, 7, 30, 7),
child: ElevatedButton.icon(
style: ElevatedButton.styleFrom(
shadowColor: Colors.black,
fixedSize: const Size(300, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
primary: Colors.teal[700],
onPrimary: Colors.white,
textStyle: const TextStyle(
fontSize: 25,
fontStyle: FontStyle.normal,
),
),
label: const Text('Kontakt'),
icon: const Icon(Icons.person),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const Kontakt()),
);
},
onLongPress: () {},
),
),
On the Second page I wrote this:
class Kontakt extends StatelessWidget {
const Kontakt({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.teal,
body: ListView(
children: <Widget>[
Center(
child: ListView(
children: [
Container(
margin: const EdgeInsets.fromLTRB(20, 27, 20, 20),
child: const Image(
image: AssetImage('assets/ENLogo.png'),
width: 200,
height: 200,
fit: BoxFit.contain,
)),
Container(
margin: const EdgeInsets.fromLTRB(30, 7, 30, 7),
child: ElevatedButton.icon(
style: ElevatedButton.styleFrom(
shadowColor: Colors.black,
fixedSize: const Size(300, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
primary: Colors.teal[700],
onPrimary: Colors.white,
textStyle: const TextStyle(
fontSize: 25,
fontStyle: FontStyle.normal,
),
),
label: const Text('E-Mail'),
icon: const Icon(Icons.mail),
onPressed: () {
},
onLongPress: () {
},
),
),
],
),
),
],
)),
);
}}
What have I to change, so that i can navigate through my files?
In Android Studio I can’t find any ERROR Message or anything like that.
I thank you in advance for your support.
2
Answers
thank you for your quick reply. I took the corrected code from you. Unfortunately it still doesn't work, in the terminal I get confirmation that it works, but in the emulator I also stay on the first page.
Do you see any other mistake?
I'll send the whole code of the first page:
"NEW" Second Page:
So you need to remove the
MaterialApp()
widget in on the second page:->First Page
->Second Page
Just reference your first page in your main class
Check the images below