Navigate to a new screen in Flutter

后端 未结 7 1416
日久生厌
日久生厌 2020-12-11 18:34

How do you navigate to a new screen in Flutter?

These questions are similar, but are asking more than I am.

  • Flutter - Navigate to a new screen, and cle
相关标签:
7条回答
  • 2020-12-11 18:59

    Navigate to next screen with back using Navigator.push()

    Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => SecondRoute()),);
    

    Navigate to next screen without back using Navigator.pushReplacement()

    Navigator.pushReplacement(
    context,MaterialPageRoute(builder: (context) => SecondRoute()),);
    
    0 讨论(0)
  • 2020-12-11 19:00
    onTap: () {
      Navigator.push(context,
          MaterialPageRoute(builder: (context) => NextScreenName()));
    }
    
    0 讨论(0)
  • 2020-12-11 19:02

    If you are familiar with web development this approach is similar to routing.

    main.dart

    void main() {
      setupLocator();
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          routes: {
            '/' : (BuildContext context)=>HomePage(),
            '/register' : (BuildContext context)=>RegisterPage(),
          },
        );
      }
    }
    

    You can add button onPressed event from the homepage.dart to navigate register.dart as follows.

    onPressed: (){
        Navigator.pushReplacementNamed(context, '/register');
     },
    
    0 讨论(0)
  • 2020-12-11 19:15

    You can also avoid Navigator / MaterialPageRoute altogether. The main reason is animations.

    The problem with Material route is that excepting Hero animation, it only has slide-in animation for most page transitions.

    Now consider Flutter canned animation package.

    To have something like this you'd need to code your page without using Material Route. All page transitions here work using Container transformation or sort-of widget switch.

    So personally I'd structure my app based on how the pages transition. If the page I navigate to has shared elements with the previous page then I would go with Material Route. An example would be image gallery (which shares the image with the icon clicked). For normal page transitions I would actually go without it.

    0 讨论(0)
  • 2020-12-11 19:18

    Navigate to a new screen:

    Navigator.of(context).push(MaterialPageRoute(builder: (context) => NewScreen()));
    

    where context is the BuildContext of a widget and NewScreen is the name of the second widget layout.

    Code

    main.dart

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(primarySwatch: Colors.blue),
          home: HomeScreen(),
        );
      }
    }
    
    class HomeScreen extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text('Home Screen')),
          body: Center(
            child: RaisedButton(
              child: Text(
                'Navigate to a new screen >>',
                style: TextStyle(fontSize: 24.0),
              ),
              onPressed: () {
                _navigateToNextScreen(context);
              },
            ),
          ),
        );
      }
    
      void _navigateToNextScreen(BuildContext context) {
        Navigator.of(context).push(MaterialPageRoute(builder: (context) => NewScreen()));
      }
    }
    
    class NewScreen extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text('New Screen')),
          body: Center(
            child: Text(
              'This is a new screen',
              style: TextStyle(fontSize: 24.0),
            ),
          ),
        );
      }
    }
    

    See also

    • Documentation
    • Navigator and Routes and Transitions... Oh, My! - Simon Lightfoot | Flutter Europe
    0 讨论(0)
  • 2020-12-11 19:18

    You can try with the following code

    Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => YourNextScreen())),
    
    0 讨论(0)
提交回复
热议问题