Flutter传递数据并从有状态的小部件向另一个有状态的小部件调出方法

问题描述

美好的一天!我的MainMenu页面和抽屉中有一些代码块。 我需要将数据从MainMenu(这是一个有状态的小部件)传递到Drawer(也是有状态的小部件),以便可以使用MainMenu中的数据和方法。 有人可以帮我还是在下面复制我的代码

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,],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",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,size: 30.0),title: Text("Stations",1),ListTile(
                leading: Icon(Icons.settings,title: Text("Account Settings",2),Divider(
                        height: 595,thickness: 0.5,color: Colors.white.withOpacity(0.3),indent: 32,endindent: 32,ListTile(
                leading: Icon(Icons.exit_to_app,onTap: () {
                 //widget.signOut();
                  },title: Text("logout",);
  }
}

我在MainMenu的构建小部件中遇到此错误

未定义命名参数“ onTap”。 尝试将名称更正为现有命名参数的名称,或定义名称为“ onTap”的命名参数。

解决方法

此部分:

Function onTap;

_MyDrawerState({this.onTap});

此参数及其在构造函​​数中的存在应在MyDrawer公共类中,而不是私有State类中。

出现指定的错误是因为MyDrawer类没有这个。

您可以通过onTap变量(是_MyDrawerState类的实例)访问widget中的MyDrawer函数