如何使用BottomNavigationBar从父类运行函数

时间:2019-01-30 13:50:02

标签: dart flutter bottomnavigationview

我有一个应用程序,当用户登录时,该应用程序会路由到我的内容类,并且其中包含一个BottomNavigationBar。导航栏的每个项目都是一个显示信息的类

final List<Widget> _children = [
HomePage(),
LibraryPage(),
PlaceholderWidget(Colors.green),
PlaceholderWidget(Colors.blue),
PlaceholderWidget(Colors.black)
];

我遇到的问题是HomePage类,它是子类之一。我在AppBar中有一个用于注销用户的小部件,所以我想使用父类(ContentPage)中的函数(_signOut()),以便它可以使用Auth和VoidCallBack来注销用户。从路由类(RootPage)传递。我无法将其设置为静态函数,因为尝试使用Content类中的成员之一时出现错误。

[dart] Instance members can't be accessed from a static method. [instance_member_access_from_static]

因此,如果有某种方式,我可以在不使用静态修饰符的情况下运行该函数,那就太好了。

class ContentPage extends StatefulWidget {


ContentPage({this.auth, this.onSignedOut});

  final BaseAuth auth;
  final VoidCallback onSignedOut;


  _ContentPageState createState() => _ContentPageState();
}

class _ContentPageState extends State<ContentPage> {


  int _currentIndex = 0;
  final List<Widget> _children = [
    HomePage(),
    LibraryPage(),
    PlaceholderWidget(Colors.green),
    PlaceholderWidget(Colors.blue),
    PlaceholderWidget(Colors.black)
  ];

  void onTabTapped(int index) {
   setState(() {
     _currentIndex = index;
   });
 }

  _signout() async {
   try {
     await widget.auth.signOut();
     widget.onSignedOut();
   } catch (e) {
     print(e);
   }
 }




  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xFF000000),
      body: _children[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        onTap: onTabTapped,
        fixedColor: Colors.black,
        currentIndex: _currentIndex,
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            title: Text('Home'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.search),
            title: Text('Search'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.mic),
            title: Text('Battle'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.message),
            title: Text('Messages'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            title: Text('Account'),
          ),
        ],
      ),
    );
  }
}

0 个答案:

没有答案