如何在颤动上使用底部导航栏传递数据 API

问题描述

我有一个登录后路由到 homescreen 的应用程序。这个类包含一个底部导航栏,里面有 3 条路线。 body,forum,and profile。在 body/ 主屏幕中,我想显示一个天气小部件。我得到了用于添加一些天气小部件的回购代码。这个天气小部件需要刷新/加载初始化才能从 API 获取数据,我想把这个加载类放在我的 Homescreen 中。但我的问题是底部导航栏没有在主屏幕/body调用/显示。我认为它只是在此调用 Get API DATA

Navigator.pushReplacementNamed(context,"/body",arguments: {
      "weatherData": WeatherData.fromJson(data),"selectedLocation": arguments
    });

不是底部导航栏

_layoutPage = [Body(),Forum(),Profile()];

我的问题是如何合并底部导航栏和这个调用 API 数据, 所以每条到 body 的路由都会调用加载来获取 api 数据并显示底部导航栏。

这是我的Homescreen

class HomeScreen extends StatefulWidget {
  HomeScreen({Key key,this.title}) : super(key: key);

  final String title;

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

class _HomeScreenState extends State<HomeScreen> {
  int _selectedindex = 0;

  @override
  void initState() {
    Provider.of<FirebaSEOperations>(context,listen: false)
        .initUserData(context);
    super.initState();
  }

  final _layoutPage = [Body(),Profile()];

  void _onTapItem(int index) {
    setState(() {
      _selectedindex = index;
    });
  }

  // Weather
  String apiKey = "MY API KEY";
  SimpleLocationResult arguments;
  getData({lat,lon}) async {
    String latitude = lat == null ? "-6.2146" : lat.toString();
    String longitude = lon == null ? "106.8451" : lon.toString();

    Response response = await get(
        "https://api.openweathermap.org/data/2.5/weather?units=metric&lat=$latitude&lon=$longitude&appid=$apiKey");
    Map data = jsonDecode(response.body);

    Navigator.pushReplacementNamed(context,"selectedLocation": arguments
    });
    
  }

  @override
  Widget build(BuildContext context) {
    //loading weather
    arguments = ModalRoute.of(context).settings.arguments;
    Future.delayed(
        Duration(seconds: 1),() => {
              arguments != null
                  ? getData(lat: arguments.latitude,lon: arguments.longitude)
                  : getData()
            });
    return Scaffold(
      body: _layoutPage.elementAt(_selectedindex),bottomNavigationBar: BottomNavigationBar(
        backgroundColor: Colors.white,selectedItemColor: kDarkGreenColor,unselectedItemColor: kMainColor,selectedLabelStyle:
            TextStyle(color: kMainColor,fontFamily: 'Roboto',fontSize: 14.0),unselectedLabelStyle: TextStyle(
            color: Colors.grey[600],fontSize: 12.0),showUnselectedLabels: true,items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(icon: Icon(EvaIcons.home),label: 'Home'),BottomNavigationBarItem(
              icon: Icon(EvaIcons.messageSquare),label: 'Forum'),BottomNavigationBarItem(
              icon: Icon(EvaIcons.person),label: 'Profile'),],currentIndex: _selectedindex,onTap: _onTapItem,),);
  }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)