How to navigate from Radiobutton?

Asked

Viewed 28 times

0

I’m doing a project where the user has to choose one of the languages below and then click the "finish" button. However, as I would navigate between screens, if there are three possible screens that the "finish" button can go, according to the user’s choice?

inserir a descrição da imagem aqui

import 'package:flutter/material.dart';
import 'package:projeto_tcc/Tela_Principal.dart';

class Tela_Idioma extends StatefulWidget {
@override
_Tela_IdiomaState createState() => _Tela_IdiomaState();

void _abrirTela_Principal(){
BuildContext context;
Navigator.push(
    context,
    MaterialPageRoute(builder: (context)=> Tela_Principal() ));
}
}

class _Tela_IdiomaState extends State<Tela_Idioma> {

String _escolhaUsuario;
void _Lingua( String escolha){
setState(() {
  _escolhaUsuario = escolha;
  print("escolha" + _escolhaUsuario);

  switch(_escolhaUsuario){
    case "espanhol" :

      break;
    case "coreano" :

      break;

    case "ingles" :

      break;
  }

  });
  }
  @override
  Widget build(BuildContext context) {
  Size size = MediaQuery.of(context).size;
  return Scaffold(
  backgroundColor: Colors.white,
    body: Container(
      child: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Container(
              height: 592,
              padding: EdgeInsets.fromLTRB(30, 0 , 30, 30),
              decoration: BoxDecoration(
                  image: DecorationImage(
                      image: AssetImage("Imagens/Fundo login.jpg"),
                      fit: BoxFit.fill
                  )
              ),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[

                  Padding(
                    padding: EdgeInsets.fromLTRB(20, 50, 20, 10),
                    child: Image.asset(
                      "Imagens/Coreano.jpg",
                      width: 500,
                      height: 50,
                    ),
                  ),

                  Container(

                    child:  Padding(
                      padding: EdgeInsets.fromLTRB(50, 0, 0, 0),
                      child: RadioListTile(
                          title: Text("Coreano"),
                          activeColor: Color(0xff5CE6B8),
                          value: "coreano",
                          groupValue: _escolhaUsuario,
                          onChanged: _Lingua,

                      ),
                    ),
                  ),

                  Padding(
                    padding: EdgeInsets.fromLTRB(20, 0, 20, 10),
                    child: Image.asset(
                      "Imagens/Espanhol.jpg",
                      width: 500,
                      height: 53,
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.fromLTRB(50, 0, 0, 0),
                    child: RadioListTile(
                        title: Text("Espanhol"),
                        activeColor: Color(0xff5CE6B8),
                        value: "espanhol",
                        groupValue: _escolhaUsuario,
                        onChanged: _Lingua,
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.fromLTRB(20, 0, 20, 10),
                    child: Image.asset(
                      "Imagens/Ingles.jpg",
                      width: 500,
                      height: 43,
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.fromLTRB(50, 0, 0, 0),
                    child: RadioListTile(
                        title: Text("Inglês"),
                        activeColor: Color(0xff5CE6B8),
                        value: "ingles",
                        groupValue: _escolhaUsuario,
                        onChanged: _Lingua,
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.only(top: 10),
                    child: RaisedButton(
                        shape: new RoundedRectangleBorder(borderRadius:
                        new BorderRadius.circular(30.0)),
                        color: Color(0xff5CE6B8),
                        padding: EdgeInsets.all(17),
                        child: Text(
                          "Finalizar",
                          style: TextStyle(
                              fontSize: 16
                          ),
                        ),
                        onPressed: (){
                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => Tela_Principal()
                              )
                          );
                        }
                    ),
                  )
                ],
              ),
            ),
          ],
        ),
      ),
      ),
      );
      }
      }
  • 1

    You already own one switch-case within the method _Lingua()... Just do it the same at the click of the button and each marry you make a push for the related screen.

1 answer

0

You have already created the other navigation screens, the main() function has been created, is on the main screen( Tela_main())? In my opinion this switch would not be necessary , just state the language chosen by the user, follows:

import 'package:flutter/material.dart';
import 'package:projeto_tcc/Tela_Principal.dart';

void main() {
  () => Tela_Idioma();
}

class Tela_Idioma extends StatefulWidget {
@override
_Tela_IdiomaState createState() => _Tela_IdiomaState();

void _abrirTela_Principal(){
BuildContext context;
Navigator.push(
    context,
    MaterialPageRoute(builder: (context)=> Tela_Principal() ));
}
}

class _Tela_IdiomaState extends State<Tela_Idioma> {

String _escolhaUsuario;
void _Lingua( String escolha){
setState(() {
  _escolhaUsuario = escolha;
  print("escolha " + _escolhaUsuario);

  });
  }
  @override
  Widget build(BuildContext context) {
  Size size = MediaQuery.of(context).size;
  return Scaffold(
  backgroundColor: Colors.white,
    body: Container(
      child: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Container(
              height: 592,
              padding: EdgeInsets.fromLTRB(30, 0 , 30, 30),
              decoration: BoxDecoration(
                  image: DecorationImage(
                      image: AssetImage("Imagens/Fundo login.jpg"),
                      fit: BoxFit.fill
                  )
              ),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[

                  Padding(
                    padding: EdgeInsets.fromLTRB(20, 50, 20, 10),
                    child: Image.asset(
                      "Imagens/Coreano.jpg",
                      width: 500,
                      height: 50,
                    ),
                  ),

                  Container(

                    child:  Padding(
                      padding: EdgeInsets.fromLTRB(50, 0, 0, 0),
                      child: RadioListTile(
                          title: Text("Coreano"),
                          activeColor: Color(0xff5CE6B8),
                          value: "coreano",
                          groupValue: _escolhaUsuario,
                          onChanged: _Lingua,

                      ),
                    ),
                  ),

                  Padding(
                    padding: EdgeInsets.fromLTRB(20, 0, 20, 10),
                    child: Image.asset(
                      "Imagens/Espanhol.jpg",
                      width: 500,
                      height: 53,
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.fromLTRB(50, 0, 0, 0),
                    child: RadioListTile(
                        title: Text("Espanhol"),
                        activeColor: Color(0xff5CE6B8),
                        value: "espanhol",
                        groupValue: _escolhaUsuario,
                        onChanged: _Lingua,
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.fromLTRB(20, 0, 20, 10),
                    child: Image.asset(
                      "Imagens/Ingles.jpg",
                      width: 500,
                      height: 43,
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.fromLTRB(50, 0, 0, 0),
                    child: RadioListTile(
                        title: Text("Inglês"),
                        activeColor: Color(0xff5CE6B8),
                        value: "ingles",
                        groupValue: _escolhaUsuario,
                        onChanged: _Lingua,
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.only(top: 10),
                    child: RaisedButton(
                        shape: new RoundedRectangleBorder(borderRadius:
                        new BorderRadius.circular(30.0)),
                        color: Color(0xff5CE6B8),
                        padding: EdgeInsets.all(17),
                        child: Text(
                          "Finalizar",
                          style: TextStyle(
                              fontSize: 16
                          ),
                        ),
                        onPressed: (){
                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => if( _escolhaUsuario == "ingles")  Tela_Ingles() else if (_escolhaUsuario == "coreano") 
                                    Tela_Principal()
                                   else 
                                      Tela_Principal()
                                  }
                              )
                          );
                        }
                    ),
                  )
                ],
              ),
            ),
          ],
        ),
      ),
      ),
      );
      }
      }

Browser other questions tagged

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