0

我有两个班级MyHomepageBodyLayout

MyHomePage是脚手架并具有BodyLayoutaschild

现在我想从of访问MyHomepage变量onTappedBodyLayout

是否可以?或者我应该怎么做?

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title; 
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<Category> cats = [];
  List<Widget> _myLayouts = [];
  int _currentIndex = 0;

  @override
  void initState() {
    super.initState();
    _myLayouts = [
      new BodyLayout("latest","",key: Key('1')),
      new BodyLayout("pop","",key: Key('2')),
    ];
  }
  void _onItemTapped(int index) {
    setState(() {
      _currentIndex = index; 
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(children: _myLayouts, index: _currentIndex,),

      bottomNavigationBar: BottomNavigationBar(
      items: const <BottomNavigationBarItem>[
        BottomNavigationBarItem(
          icon: Icon(Icons.home),
          title: Text('Latest'),
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.business),
          title: Text('Fab'),
        ),
      ],
        currentIndex: _currentIndex,
        selectedItemColor: Colors.amber[800],
        onTap: _onItemTapped,
      ),
    );
  }
}



class BodyLayout extends StatefulWidget {
  final String mode;
  final String catKey;
  BodyLayout(this.mode,this.catKey,{Key key}) : super(key: key);
  @override
  _BodyLayoutState createState() => _BodyLayoutState();
}
class _BodyLayoutState extends State<BodyLayout>{

  void initState(){
    super.initState();


  }

  void onTapped(context,var url) {
   // I want to get variables '_currentIndex' of _MyHomePageState here.
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        ListView.builder(
          controller: _controller,
          itemCount: articles.length,
          itemBuilder: (context, index) {
            return ListTile(
              title: Text(articles[index].title),
              onTap: () => onTapped(context,articles[index].url),
            );
          },
        ),
      ]
    );
  }
}
4

1 回答 1

1

您正在尝试以完全不推荐的方式访问类的私有变量。但是有更好的方法来解决您的问题。为什么不从父级传递函数?

在 _MyHomePageState 内创建一个 onTapped 函数并将该函数传递给 BodyLayout

class _MyHomePageState extends State<MyHomePage> {
 ....
 void onTapped(context,var url) {
   // variables are accessible here
  }

@override
  void initState() {
    super.initState();
    _myLayouts = [
      new BodyLayout("latest","",key: Key('1'), onTapped: onTapped),
      new BodyLayout("pop","",key: Key('2'), onTapped: onTapped),
    ];
  }
 ....
}

现在在正文布局中捕获此参数并将其传递给 onTap 方法

class BodyLayout extends StatefulWidget {
  ...
  final Function onTapped;
  BodyLayout(this.mode,this.catKey,{Key key, this.onTapped}) : super(key: key);
  @override
  _BodyLayoutState createState() => _BodyLayoutState(onTapped: onTapped);
}

class _BodyLayoutState extends State<BodyLayout>{
  final Function onTapped;

  _BodyLayoutState({this.onTapped});

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        ListView.builder(
          controller: _controller,
          itemCount: articles.length,
          itemBuilder: (context, index) {
            return ListTile(
              title: Text(articles[index].title),
              onTap: () => onTapped(context,articles[index].url),
            );
          },
        ),
      ]
    );
  }

}
于 2019-11-29T19:51:11.533 回答