如何在不使用MaterialPageRoute的情况下传递参数函数setState?

问题描述 投票:0回答:1

我有更新状态的问题。我需要将带有setState的函数传递到小部件中,但是我不知道如何做。问题是我需要传递一个静态函数,在其中不能执行setState。我必须修复哪些选项?

我的代码

class NavigationBar extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _NavigationBarState();
  }
}

class _NavigationBarState extends State<NavigationBar> {
  bool showMusicTab = false;
  bool openMusicTab = false;
  int index = 4;
  final List<Widget> screens = [
    Home(),
    Search(),
    AddPost(),
    Notifications(),
    Profile(showMusicTabAndPlay)
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: screens[index],
      persistentFooterButtons: showMusicTab
          ? <Widget>[
              Container(
                color: Colors.black,
                height: 20,
                width: MediaQuery.of(context).size.width,
                child: ListTile(
                  leading: Icon(Icons.favorite_border, color: Colors.white),
                  title: Center(
                    child: InkWell(
                      onTap: () {},
                      child: Text(
                        'Break my soul',
                        style: TextStyle(
                            color: Colors.white,
                            fontFamily: kRobotoBold,
                            fontWeight: FontWeight.bold),
                      ),
                    ),
                  ),
                  trailing: Icon(
                    Icons.pause_circle_filled,
                    color: Colors.white,
                  ),
                ),
              )
            ]
          : null,
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: index,
        onTap: (int index) {
          setState(() {
            this.index = index;
          });
        },
        type: BottomNavigationBarType.fixed,
        showSelectedLabels: false,
        showUnselectedLabels: false,
        backgroundColor: Colors.black,
        items: [
          new BottomNavigationBarItem(
            icon: new Icon(
              Icons.home,
              color: index == 0 ? Colors.pinkAccent : Colors.white,
            ),
            title: new Text('Home'),
          ),
          new BottomNavigationBarItem(
            icon: new Icon(
              Icons.search,
              color: index == 1 ? Colors.pinkAccent : Colors.white,
            ),
            title: new Text('Search'),
          ),
          new BottomNavigationBarItem(
            icon: new Icon(
              Icons.add_circle,
              color: index == 2 ? Colors.pinkAccent : Colors.white,
            ),
            title: new Text('Add post'),
          ),
          new BottomNavigationBarItem(
            icon: new Icon(
              Icons.notifications,
              color: index == 3 ? Colors.pinkAccent : Colors.white,
            ),
            title: new Text('Notifications'),
          ),
          new BottomNavigationBarItem(
              icon: Icon(
                Icons.person,
                color: index == 4 ? Colors.pinkAccent : Colors.white,
              ),
              title: Text('Profile'))
        ],
      ),
    );
  }

  static void showMusicTabAndPlay() {
    setState(() {
      showMusicTab = true;
    });
  }
}
flutter dart flutter-layout
1个回答
0
投票

[您的问题是,您试图通过在字段声明中直接向其分配对象的screens来首次创建该窗口小部件时填充List:>

class _NavigationBarState extends State<NavigationBar> {
  final List<Widget> screens = [
    Home(),
    Search(),
    AddPost(),
    Notifications(),
    Profile(showMusicTabAndPlay)
  ];

  ...
}

当您像这样填充对象时,只能使用静态字段和方法来填充它。对于showMusicTabAndPlay,这是一个问题,因为它调用的是实例方法setState,无法从静态方法中调用它。

相反,应该在构造函数或screens方法(建议使用后者)中填充initState

class _NavigationBarState extends State<NavigationBar> {
  List<Widget> screens;

  @override
  void initState() {
    super.initState();
    screens = [
      Home(),
      Search(),
      AddPost(),
      Notifications(),
      Profile(showMusicTabAndPlay)
    ];
  }

  ...
}

有了这个,您现在可以使showMusicTabAndPlay成为实例方法而不是静态方法,并且错误将消失。

© www.soinside.com 2019 - 2024. All rights reserved.