Flutter calling a dialog inside an onTap()

Asked

Viewed 599 times

-1

I’m having a problem in a APP development on flutter, I’m trying to call a method inside an onTap(); however this presenting the following error:

"The Return type 'void' isn’t a 'Widget', as defined by Anonymous closure."

Even when I use the method as a Widget not sure.

Follows code:

import '*';
import 'descanso.dart';
import 'jornada.dart';

class Menu extends StatefulWidget {
  @override
  _MenuState createState() => _MenuState();
}

class _MenuState extends State<Menu> {
  void _dismissDialog() {
    Navigator.pop(context);
  }

  void _showMaterialDialog() { -----> Estou tentando chamar esse metodo.
    showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text('Material Dialog'),
            content: Text('This is the content of the material dialog'),
            actions: <Widget>[
              FlatButton(
                  onPressed: () {
                    _dismissDialog();
                  },
                  child: Text('Close')),
              FlatButton(
                onPressed: () {
                  print('HelloWorld!');
                  _dismissDialog();
                },
                child: Text('Print HelloWorld!'),
              )
            ],
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    List<String> itemNames = [
      'Simulador',
      'Simulador',
      'Regras',
      'Pesquisa',
    ];

    List<Color> tileColors = [
      null,
      null,
      null,
      null,
    ];

    List<Color> splashColors = [
      invertColorsMaterial(context),
      MaterialColors.blue,
      MyColors.accentColor,
      MyColors.accentColor,
    ];

    return Scaffold(
      backgroundColor: Colors.white,
      body: Column(
        children: <Widget>[
          Expanded(
            flex: 5,
            child: Container(
              child: Image.asset("assets/img_emb.jpeg"),
            ),
          ),
          Expanded(
            flex: 8,
            child: Container(
              child: Padding(
                padding: EdgeInsets.only(top: 0.0),
                child: Scaffold(
                  backgroundColor: Colors.white,
                  body: Container(
                    child: Column(
                      children: <Widget>[
                        Padding(
                          padding: EdgeInsets.only(
                            left: 20.0,
                            bottom: 10.0,
                          ),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.start,
                            children: <Widget>[
                              Text(
                                "versao 0.01",
                                style: TextStyle(
                                  color: Colors.black,
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                            ],
                          ),
                        ),
                        Expanded(
                          child: GridView.count(
                            crossAxisCount: 1,
                            childAspectRatio: 2.5,
                            children: List.generate(
                              itemNames.length,
                              (index) {
                                return Hero(
                                  tag: 'titulo$index',
                                  child: Titulos(
                                    context,
                                    tileColors[index],
                                    splashColors[index],
                                    Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.center,
                                      crossAxisAlignment:
                                          CrossAxisAlignment.center,
                                      children: <Widget>[
                                        Text(
                                          "${itemNames[index]}",
                                          style: TextStyle(
                                            fontWeight: FontWeight.w700,
                                            fontSize: 20.0,
                                            color: Color(0xFF1E3C96),
                                          ),
                                          softWrap: true,
                                          overflow: TextOverflow.fade,
                                          maxLines: 1,
                                        ),
                                      ],
                                    ),
                                    onTap: () {
                                      Navigator.push(
                                        context,
                                        CupertinoPageRoute(
                                          builder: (context) {
                                            if (index == 0) {
                                              return Jornada();
                                            } else if (index == 1) {
                                              return Descanso();
                                            } else if (index == 2) {

                                              return _showMaterialDialog(); -------> Onde o erro esta aparecendo.

                                            } else if (index == 3) {
                                              return new Scaffold(
                                                backgroundColor: Colors.white,
                                                body: new MyReviewPage(),
                                              );
                                            } else {
                                              return null;
                                            }
                                          },
                                        ),
                                      );
                                    },
                                  ),
                                );
                              },
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

1 answer

1


Your mistake is passing one showDialog for a Navigator...

Modify the block of OnTap() as follows:

[...]

onTap: () {
  if (index == 2)
    _showMaterialDialog();
  else {
      Navigator.push(
        context,
        CupertinoPageRoute(
          builder: (context) {
            if (index == 0) {
              return Jornada();
            } else if (index == 1) {
              return Descanso();
            }  if (index == 3) {
              return Scaffold(
                backgroundColor: Colors.white,
                body: MyReviewPage(),
              );
            } else
              return null;          
          },
        ),
      );
  }


},

[...]

Utilize return only for when its function is, for example in your case, Widget MontarTela(){}.

Browser other questions tagged

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