Open second page when the TextButton is clicked:
import statement: (the path of the package may vary in your case)
import 'package:lessons/second_page.dart';
code:
TextButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return const SecondPage();
},
),
);
},
style: TextButton.styleFrom(
foregroundColor: Colors.green,
textStyle: const TextStyle(
fontWeight: FontWeight.bold,
),
),
child: const Text(
"Open Second Page",
),
),
second page:
import 'package:flutter/material.dart';
class SecondPage extends StatelessWidget {
const SecondPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Second Page"),
leading: const Icon(
Icons.star,
),
),
body: Center(
child: Column(
children: [
const Text(
"This is the body of the second page",
),
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text("Back to Main Page"),
),
],
),
),
);
}
}
No comments:
Post a Comment