How to passing data API with bottom navigation bar on flutter

Viewed 519

I have an app that routes to a homescreen after login. This class contains a bottom navigation bar with 3 routes inside. body, forum, and profile. Inside body/ mainscreen i want to display an weather widget. I get repo code for add some weather widget. This weather widget need refresh/loading for initialize to get data from API, i want to put this loading class inside my Homescreen. but my problem is the bottom navigator bar not calling/showing on mainscreen/body. I think its only calling Get API DATA on this

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

not Bottom navigation bar

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

My question is How to merge bottom navigaton bar and this calling API data, so every routes to body is calling loading to get api data and show bottom navigation bar.

this is my 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(), Forum(), 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, "/body", arguments: {
      "weatherData": WeatherData.fromJson(data),
      "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], fontFamily: 'Roboto', 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,
      ),
    );
  }
}
0 Answers
Related