pushNavigator - Dart/Flutter

Asked

Viewed 282 times

-1

Hello, I’m learning how to develop Dart/Flutter apps.

What happens is that I pressed a Navigator.push, where it loads the previous route, in case I want to return, however, in my appbar instead of pressing the back button, it initializes the button that opens the Navigator Drawer.

Below the code and image of what occurs:

Nav.Dart

import 'package:flutter/material.dart';

    Future push(BuildContext context, Widget page, {bool replace = false}) {

    if(replace) {
      return Navigator.pushReplacement(
     context, MaterialPageRoute(builder: (BuildContext context) {
    return page;
    }));
   }
   return Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) {
    return page;
   }));
  }

List_view.Dart

  _body(context){
return Observer(
    //stream: _carMobx.stream,
    builder: (context){

      List<Carro> carros = _carMobx.carros;
        if(_carMobx.error != null){
          return TextError("Não foi possível retornar os dados\n\nClique Aqui para recarregar", onPressed: _fetch());
        }
        else{
          if(carros == null){
            return Center(child: CircularProgressIndicator(),);
          }else {
            return _listView(context, carros);
          }
        }
  }
  );
  }

  Container _listView(context, List<Carro> carros) {
   return Container(
  padding: EdgeInsets.all(16),
  child: ListView.builder(
      itemCount: carros.length,
      itemBuilder: (context, index){
        Carro c = carros[index];
        return Card(
          child: Container(
            padding: EdgeInsets.all(10),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Center(
                    child: Image.network(c.urlFoto ?? 
        "https://i.pinimg.com/originals/63/dc/de/63dcde7857ba4c976db71314fc79cf19.jpg",
                      width: 250,)
                ),
                Text(c.nome ?? "Sem nome",
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(fontSize: 18,),
                ),
                Text(c.descricao ?? "Sem Descrição",
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(fontSize: 14,),
                ),
                ButtonBarTheme(
                  data: ButtonBarThemeData(),
                  child: ButtonBar(
                    children: <Widget>[
                      FlatButton(
                        child: const Text('DETALHES'),
                        onPressed: () => _onClickDetalhes(context, c),
                      ),
                      FlatButton(
                        child: const Text('COMPARTILHAR'),
                        onPressed: () { /* ... */ },
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        );
      }),
      );
      }

      _onClickDetalhes(context, Carro c) {
        push(context, CarroPage(c));
      }

1 answer

0

It wasn’t very clear what your problem is, and the code you posted doesn’t help much, anyway as I understand you need to change the icon that appears in the Appbar, so that it allows you to return to the previous route and not to icon to open the menu, right?

If this is your case, on your new route you must have a Scaffold and add a new Appbar with the return button, example:

Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Segunda pagina'),
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () => {
             // Chame o Navigator aqui para voltar a rota precedente
          }
        ),
      ),
);

You can find this exact example in the official documentation here

Browser other questions tagged

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