Yusof Antar
Yusof Antar

Reputation: 309

passing arguments in pushreplacement named flutter

i am trying to use CurveBottomNavigatorBar in my flutter project...

i used my Curve... in the main dart like this:

class _BottomNavigatorBarState extends State<BottomNavigatorBar> {
  int selectedIndex = 0;
  final screen = [
    HomeScreen(),
    CartScreen(),
    MyStore(),
    SettingScreen(),
  ];
  @override
  Widget build(BuildContext context) {
    int args = ModalRoute.of(context).settings.arguments;

    return Scaffold(
      body: screen[selectedIndex],
      bottomNavigationBar: CurvedNavigationBar(
        height: 55,
        backgroundColor: Colors.transparent,
        buttonBackgroundColor: Theme.of(context).accentColor,
        color: Theme.of(context).primaryColor,
        animationCurve: Curves.easeOutCubic,
        index: selectedIndex,
        items: <Widget>[
          // code here
        ],
        onTap: (index) {
          setState(() {
            args = selectedIndex;
            selectedIndex = index;
          });
        },
      ),
    );
  }
}

when i go to my store screen there is an add button that take me to a Form to complete it but when i go back it doesn't show me the BOTNAVBAR on my screen so i though that if i can pass arguments so when i pushback to my main it open my my stores automatically

this is the adding store page:

  @override
  Widget build(BuildContext context) {
    int index = 2;
    _onBackPressed() {
      Navigator.of(context).pushReplacementNamed(
        '/myStore',
        arguments: index,
      );
    }

    return WillPopScope(
      onWillPop: () {
        return _onBackPressed();
      },
      child: Scaffold(
        appBar: AppBar(
          title: Text('Add Store'),
          leading: IconButton(
            onPressed: () {
              Navigator.of(context).pushReplacementNamed(
                '/myStore',
                arguments: index,
              );
            },
            icon: Icon(Icons.arrow_back),
          ),
        ),

sp i did it like that and i know its wrong cause its not working

anyhelp please and excuse my bad english :(

Upvotes: 2

Views: 3666

Answers (2)

Chirag Rathod
Chirag Rathod

Reputation: 167

Please refer this : https://flutter.dev/docs/cookbook/navigation/navigate-with-arguments

Navigator.pushNamed(
  context,
  ExtractArgumentsScreen.routeName,
  arguments: MyStore(index),
);

Make your MyStore() widget that accepts argument like,

int Index;

MyStore(this.Index)

and use in that screen like,

widget.Index

Please go through above link once.

Upvotes: 3

Guillaume Roux
Guillaume Roux

Reputation: 7318

You have a parameters arguments which you can pass to pushReplacementName:

Future<T?> pushReplacementNamed <T extends Object?, TO extends Object?>(
  BuildContext context,
  String routeName,
  {
    TO? result,
    Object? arguments,
  }
)

You can call it like this:

// Considering a variable named my_args_obj
Navigator.of(context).pushReplacementNamed('/myStore', arguments: my_args_obj);

Then to get it from your page:

final args = ModalRoute.of(context).settings.arguments;

Upvotes: 0

Related Questions