Flutter passing data and calling out method from a stateful widget to another statefulwidget

523 views Asked by At

Good day! I have here some block of codes of my MainMenu page and Drawer. I need to pass data from MainMenu which is a statefulwidget to Drawer which is also a statefulwidget so that I can use the datas and method from MainMenu. Can someone help me or reproduce my code below.

class MainMenu extends StatefulWidget {

  
  final VoidCallback signOut;

  MainMenu(this.signOut);
  

  @override
  _MainMenuState createState() => _MainMenuState();
}

class _MainMenuState extends State<MainMenu> {

  int index = 0;
 

  List<Widget> list = [
    HomeScreen(),
    Stations(),
    AccountPage(),

    
  ];

  signOut() {
    setState(() {
      widget.signOut();
    });
  }

  int currentIndex = 0;
  String selectedIndex = 'TAB: 0';

  String email = "", id = "", fname= "";
  TabController tabController;

  

  getPref() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    setState(() {
       id = preferences.getString('id');
       email = preferences.getString('email');
       fname = preferences.getString('fname');
    
    
    });
    print("id:" + id);
    print("user:" + email);
    print("address:" + fname);

  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getPref();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          actions: <Widget>[
          IconButton(
            onPressed: () {
              signOut();
            },
            icon: Icon(Icons.lock_open),
          )
        ],
          backgroundColor: Color(0xFF262AAA),
           iconTheme: IconThemeData(color: Colors.lightBlue),
          centerTitle: true,
        title: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('DVO',
            style: TextStyle(color: Colors.lightBlue,fontWeight: FontWeight.w700),
            ),
            SizedBox(width: 1.3),
            Text(
              'REPORT',
              style: TextStyle(color: Colors.white,fontWeight: FontWeight.w700),
            ),
          ],
        ),
        elevation: 0,
      ),
        body: list[index],
        drawer:  MyDrawer(onTap: (lol, i) {
          setState(() {
            index = i;
            Navigator.pop(lol);
          });
        }),
      ),
    );
  }
}

class MyDrawer extends StatefulWidget {
    

    @override
  _MyDrawerState createState() => _MyDrawerState();
}

    
 class _MyDrawerState extends State<MyDrawer> { 

   Function onTap;
   _MyDrawerState(
    {this.onTap
    });
  
  

  @override
  Widget build(BuildContext context) {
    
    return SizedBox(
      width: MediaQuery
          .of(context)
          .size
          .width * 0.7,
      child: Drawer(
        child: Container(
          color: Colors.white,
          child: ListView(
            padding: EdgeInsets.all(0),
            children: <Widget>[
              
              UserAccountsDrawerHeader(
                decoration: BoxDecoration(
                color: Colors.white,
                 image: DecorationImage(
                  image: AssetImage("assets/badge.jpg"),
                     fit: BoxFit.cover,
                      colorFilter: new ColorFilter.mode(Colors.black.withOpacity(0.8), BlendMode.dstATop)),
               ),
                accountEmail: Text("[email protected]"),
                accountName: Text("Dummy", 
                style: TextStyle(color: Colors.white,fontWeight: FontWeight.w700, fontSize: 25),
                ),
                currentAccountPicture: CircleAvatar(
                  backgroundColor: Colors.grey[400],
                  child: Icon(
                            Icons.perm_identity,
                            color: Colors.white,
                  ),
                
                ),
              ),
              ListTile(
                selected: true,
                leading: Icon(Icons.announcement, color: Colors.cyan,size: 26.0),
                title: Text("News And Announcements", 
               
                style: TextStyle(color: Colors.black,fontWeight: FontWeight.w500, fontSize: 18),
                ),
                onTap: () => onTap(context, 0),
                
              ),
              ListTile(
                leading: Icon(Icons.place,color: Colors.cyan, size: 30.0),
                title: Text("Stations",
                style: TextStyle(color: Colors.black,fontWeight: FontWeight.w500, fontSize: 18),
                ),
                onTap: () => onTap(context, 1),
              ),
              ListTile(
                leading: Icon(Icons.settings,color: Colors.cyan, size: 30.0),
                title: Text("Account Settings",
                style: TextStyle(color: Colors.black,fontWeight: FontWeight.w500, fontSize: 18),
                ),
                 onTap: () => onTap(context, 2),
              ),
               Divider(
                        height: 595,
                        thickness: 0.5,
                        color: Colors.white.withOpacity(0.3),
                        indent: 32,
                        endIndent: 32,
                      ),
               ListTile(
                leading: Icon(Icons.exit_to_app,color: Colors.cyan, size: 30.0),
                 onTap: () {
                 //widget.signOut();
                  },
                title: Text("Logout",
                style: TextStyle(color: Colors.black,fontWeight: FontWeight.w500, fontSize: 18),
              
                ),
              ),
         
            ],
          ),
        ),
      ),
    );
  }
}

I'm getting this error on build widget in MainMenu.

The named parameter 'onTap' isn't defined. Try correcting the name to an existing named parameter's name, or defining a named parameter with the name 'onTap'.

1

There are 1 answers

2
Arvind On BEST ANSWER

This part:

Function onTap;

_MyDrawerState({this.onTap});

This parameter and its presence in the constructor should be in the MyDrawer public class rather than a private State class.

The specified error comes because MyDrawer class doesn't have this.

You can access onTap function in _MyDrawerState through the widget variable which is an instance of MyDrawer class