Home > OS >  how to return to previous page
how to return to previous page

Time:04-09

on appbar, i want to return to previous page using icon. but it keep saying that the argument type 'context' cant be assigned to the parameter type 'BuildContext'.

AppBar app_bar_parking ({String title = ''}) { 

  return AppBar( 
    backgroundColor: Colors.white, 
    centerTitle: true,
    title: Text('Parking & Pay'), 
    elevation: 0,
    titleTextStyle: TextStyle(color: Colors.black, fontWeight: FontWeight.bold, fontSize: 20),
    leading: GestureDetector(  
      child: IconButton(  
      icon: Icon(Icons.arrow_back_ios_new_outlined, 
      size: 20, 
      color: Colors.lightBlue,),
      onPressed: () { 
        Navigator.pop(context);
       }
    ),
    )
        
  );
}

CodePudding user response:

Pass the context in the function.

AppBar app_bar_parking ({String title = '',BuildContext context}) { 

  return AppBar( 
    backgroundColor: Colors.white, 
    centerTitle: true,
    title: Text('Parking & Pay'), 
    elevation: 0,
    titleTextStyle: TextStyle(color: Colors.black, fontWeight: FontWeight.bold, fontSize: 20),
    leading: GestureDetector(  
      child: IconButton(  
      icon: Icon(Icons.arrow_back_ios_new_outlined, 
      size: 20, 
      color: Colors.lightBlue,),
      onPressed: () { 
        Navigator.pop(context);
       }
    ),
    )
        
  );
}

Now use inside the build()

@override
 Widget build(BuildContext context) {
   return Scaffold(
     appBar: app_bar_parking('',context),
// rest of the code
   );
}

CodePudding user response:

Because into your class you do not have a context declaration.

Add BuildContext context to the parameters you need.

AppBar app_bar_parking(BuildContext context, {String title = ''}) {
  return AppBar(
      backgroundColor: Colors.white,
      centerTitle: true,
      title: Text('Parking & Pay'),
      elevation: 0,
      titleTextStyle:
          TextStyle(color: Colors.black, fontWeight: FontWeight.bold, fontSize: 20),
      leading: GestureDetector(
        child: IconButton(
            icon: Icon(
              Icons.arrow_back_ios_new_outlined,
              size: 20,
              color: Colors.lightBlue,
            ),
            onPressed: () {
              Navigator.pop(context);
            }),
      ));
}

and when you use your app_bar_parking remember to add the context as:

app_bar_parking(context, "MyTitle");

CodePudding user response:

I think you are have some conflict with the library import, some common conflict library is:

import 'package:path/path.dart'
import 'dart:js';

you can change it to

import 'package:path/path.dart' as Path
  • Related