Navigation between flutter screens

Asked

Viewed 854 times

-1

I have the following code, which is called in main.Dart to display, and I would like to navigate to another screen(class), but when I use routes, when I call the element navigator, he of the error in context of Widget navigator and does not navigate between pages, because instead of being Widget build(BuildContext context) {},he is, Widget _buildPageContent() {} only, and error in context of navigator.

I would like to know how to call the other screen using the login button defined in the code?

import 'package:flutter/material.dart';
import 'mainview.dart';

class LoginView extends StatelessWidget {
  static final String path = "lib/loginview.dart";


  Widget _buildPageContent() {
    return Container(
        padding: EdgeInsets.all(20.0),
        color: Colors.grey.shade800,
        child: ListView(
          children: <Widget>[
            Column(
              children: <Widget>[
                SizedBox(
                  height: 50,
                ),
                Container(
                  width: 200,
                ),
                Image.asset('lib/img/prospentlogov1.png', width: 150,),
                SizedBox(
                  height: 50,
                ),
                ListTile(
                    title: TextField(
                  style: TextStyle(color: Colors.white),
                  decoration: InputDecoration(
                      hintText: "Endereço de E-mail",
                      hintStyle: TextStyle(color: Colors.white70),
                      border: InputBorder.none,
                      icon: Icon(Icons.email, color: Colors.white30)),
                )),
                Divider(
                  color: Colors.cyan,
                ),
                ListTile(
                  title: TextField(
                    style: TextStyle(color: Colors.white),
                    decoration: InputDecoration(
                        hintText: "Senha:",
                        hintStyle: TextStyle(color: Colors.white70),
                        border: InputBorder.none,
                        icon: Icon(
                          Icons.lock,
                          color: Colors.white30,
                        )),
                  ),
                ),
                Divider(
                  color: Colors.cyan,
                ),
                SizedBox(
                  height: 20,
                ),
                Row(
                  children: <Widget>[
                    Expanded(
                      child: RaisedButton(
                        onPressed: () async {

                        },
                        color: Colors.blue,
                        child: Text(
                          'Login',
                          style:
                              TextStyle(color: Colors.white70, fontSize: 16.0),
                        ),
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  height: 40,
                ),
                Text(
                  'Esqueceu sua senha?',
                  style: TextStyle(color: Colors.lightGreen),
                )
              ],
            ),
          ],
        ));
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _buildPageContent(),
    );
  }
}
  • A tip: It is not good practice to put Widgets to be returned by methods. Prefer to create another Widget with the things you need and then call it where you want.

  • 1

    Thanks for the tip buddy. I’m a beginner in mobile dev, and I’m still adapting to flutter. Thanks for your tip, I’ll try to redo this way too

1 answer

2


In his job _buildPageContent() put the context parameter, so when you call it in the Scaffold body pass the context. Ex:

  Widget _buildPageContent(BuildContext context) {...seucodigo...}

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _buildPageContent(context),
    );
  }

That way, when you call Navigator, you will have access to the context

  • Thanks friend, helped a lot!! I’m beginner in mobile dev and I’m having some difficulties. But thanks for the strength

Browser other questions tagged

You are not signed in. Login or sign up in order to post.