Button Click Open Another File in Flutter

How can we transfer to another file on button click in Flutter. Or how can we transfer to another screen. This is going to be created. In the body section, paste the code of this elevated button given by us. On button click, we have used navigator.push. That means this does the work of transferring to another screen. Whichever screen the transfer takes place on, a class is created on that screen and the name of that class is given on our first screen.

main.dart
import 'secondscreen.dart';




ElevatedButton(
            onPressed: () {

                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => secondscreen()),
); }, style: ElevatedButton.styleFrom( backgroundColor: Colors.blue, padding: EdgeInsets.symmetric(horizontal: 20, vertical: 12), textStyle: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), ), child: Text('Continue'), ),
second.dart

We will create another file, that is we will create another screen, we will create it with the name second.dart. You can name the file anything. But the name of the class should always be the same as we had given on the first screen. The second screen will be the same as we had given on the first screen and we will create the class with the same name.
  
  
  
  
  import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    debugShowCheckedModeBanner: false,
    home: loging(),
  ));
}

class secondscreen extends StatelessWidget {
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Colors.red, leading: IconButton( icon: Icon(Icons.arrow_back, color: Colors.white), onPressed: () { Navigator.pop(context); }, ), title: Text( "Settings", style: TextStyle( color: Colors.white, fontSize: 20, fontWeight: FontWeight.bold, ), ), ), ); } }

Output






Post a Comment

0 Comments