如何访问两个State-full Widget之间的变量? -颤振

时间:2019-05-06 21:38:21

标签: dart flutter

我是Flutter的新手,我试图在 build 方法中访问两个Statefull Widget之间的变量,但是我的方法无法正常工作。

我从how to access an object created in one stateful widget in another stateful widget in flutter那里获得了帮助,但对我没有帮助。

  

这里是错误(第二类):

bottomNavigationBar: new Material(
        child: new TabBar(
          controller: tabController,   //can't accessible 
            tabs: <Widget>[
              new Tab(
                icon: new Icon(Icons.account_balance),
              ),
              new Tab(
                icon: new Icon(Icons.wb_sunny),
              )
            ],
        ),
  

这是我的头等舱


class BottomNavBar extends StatefulWidget {

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

class _BottomNavBarState extends State<BottomNavBar>
    with SingleTickerProviderStateMixin /*used for vsync*/ {

  /*controller*/
  TabController tabController; //wanna access this to Second Class

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    tabController =new TabController(length: 2, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return new TabBarView(
      children: <Widget>[
        new NewPage("1st"),
        new NewPage("2st"),
      ],
      controller: tabController,
    );
  }
}
  

这是我的第二堂课


class BottomNav extends StatefulWidget {
  @override
  _BottomNavState createState() => _BottomNavState();
}

class _BottomNavState extends State<BottomNav> {

  //BottomNavBar _bottomNavBar=new BottomNavBar();

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text("Drawer App"),
        elevation: TargetPlatform.android != null ? 0 : 5,
      ),
      body: new BottomNavBar(),
      bottomNavigationBar: new Material(
        child: new TabBar(
          controller: tabController,   //can't accessible 
            tabs: <Widget>[
              new Tab(
                icon: new Icon(Icons.account_balance),
              ),
              new Tab(
                icon: new Icon(Icons.wb_sunny),
              )
            ],
        ),
      ),
    );
  }
}

0 个答案:

没有答案