首页 > 解决方案 > 弯曲的导航包在点击时重建页面

问题描述

我在flutter中使用了一个名为curd_navigation_bar的包。我遇到的唯一问题是,每次我点击一个按钮时,该页面都会重新构建。我正在主页上获取一些数据,并且在获取我的数据时有一个加载微调器。但是,每次我转到不同的选项卡,然后返回主页时,都会再次获取数据,因此每次返回时都必须重新加载,因此使用了微调器。有没有办法只获取一次数据?

main.dart(我从其他文件导入了所有其他小部件):

Widget build(BuildContext context) {
    return Scaffold(
      body: _currentIndex == 0
          ? Home()
          : _currentIndex == 1
              ? Data()
              : _currentIndex == 2 ? Precautions() : Settings(),
      bottomNavigationBar: CurvedNavigationBar(
        height: 60,
        animationDuration: Duration(milliseconds: 250),
        backgroundColor: accentColor,
        color: mainColor,
        buttonBackgroundColor: mainColor,
        items: [
          NavBarItem(Icons.home, _currentIndex == 0 ? null : 'Home'),
          NavBarItem(Icons.data_usage, _currentIndex == 1 ? null : 'Data'),
          NavBarItem(
              Icons.favorite_border, _currentIndex == 2 ? null : 'Precautions'),
          NavBarItem(Icons.settings, _currentIndex == 3 ? null : 'Settings'),
        ],
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
    );
  }
}

home.dart 中的代码,它使用 futurebuilder 来获取数据:


Map<String, dynamic> local;

Future<Map<String, dynamic>> fetchLocal() async {
    Map<String, dynamic> result;
    String url = 'api that i am using to fetch data.com';
    var responseLocal = await http.get(url);
    var convertedLocal = jsonDecode(responseLocal.body);

    result = convertedLocal[convertedLocal.length - 1];
    local = result;
    return result;
  }

标签: flutterflutter-navigation

解决方案


您可以使用bdnavs。它每次都更新数据。


推荐阅读