How to open another screen in Flutter?

Asked

Viewed 2,133 times

1

I have an app for QRCODE scanner and I have the following check:

// Executa função para o scanner
Future _scan() async {
  String barcode = await scanner.scan();
  if (barcode.contains("cx-")) {
  // this._outputController.text = barcode;
    onPressed: (){
      Navigator.push(
        context, 
        MaterialPageRoute(
          builder: (context) => ScanCx()
        ),
      );
    };
  }else {
    print("Código não encontrado!");
  }
}

However, when it falls into the if contains Cx, it arrives in the onPress he is not doing the navigation to the screen ScanCx.

What am I doing wrong?

2 answers

3


A tip, if your application starts to grow so you don’t have to type this "huge" line and to get more organized, you can create routes.

To do this, simply insert the routes to the screens in the main.Dart build as in the example below:

Widget build(BuildContext context) {
 return MaterialApp(
    debugShowCheckedModeBanner: false,
    title: 'Teste',
    routes: <String, WidgetBuilder>{
        '/homepage' : (BuildContext context) => new HomePage(),
    },
 );}

Then just use the line below to call the desired screen.

Navigator.pushNamed(context, '/homepage');

Good studies!

2

Hello! It seems to me that what’s left in your code is just the Onpressed. As you are calling in a _scan function, you should just run the Navigator.push function( ... ). Because onPressed is an event, normally used to link to a widget, associated with the user’s tightening action.

With the change the code should be +/- like this:

// Executa função para o scanner
Future _scan() async {

String barcode = await scanner.scan();
if (barcode.contains("cx-")) { 
    Navigator.push(
      context, 
      MaterialPageRoute(
        builder: (context) => ScanCx()
      ),
    ); 
} else {
  print("Código não encontrado!");
}

}

Browser other questions tagged

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