Flutter:如何将数据从父状态状态小部件传递到BottomNavigationBar中的选项卡之一?

时间:2018-11-02 06:35:20

标签: flutter

import 'package:flutter/material.dart';

class HomeScreen extends StatefulWidget {
  HomeScreen();

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

class _HomeScreenState extends State<HomeScreen> {
  int _currentIndex = 0;
  final List<Widget> _children = [
    MapsScreen(),
    HistoryScreen(),
  ];

  @override
  void initState() {
    super.initState();
    RestAPI.loadMapsFromNetwork();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home screen'),
      ),
      body: _children[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        onTap: onTabTapped, // new
        currentIndex: _currentIndex,
        items: [
          BottomNavigationBarItem(
            icon: new Icon(Icons.map),
            title: new Text('Maps'),
          ),
          BottomNavigationBarItem(
            icon: new Icon(Icons.change_history),
            title: new Text('History'),
          )
        ],
      ),
    );
  }

  void onTabTapped(int index) {
    setState(() {
      _currentIndex = index;
    });
  }
}

此home.dart使用initState方法进行网络调用。 如何将客户端从网络接收的地图列表传递给诸如MapsScreen的选项卡之一?我需要使用ScopedModel或InheritedWidget还是有更好的方法?所有要呈现的逻辑都在MapsScreen类中。

1 个答案:

答案 0 :(得分:-1)

您可以像这样通过json响应传递值。

class MapScreen extends StatefulWidget { 
Map<List<String,dynamic>> data ;

MapScreen({this.data}) ;

  _MapScreenState createState() => _MapScreenState() ;

 }

class _MapScreenState extends State<MapScreen> { 
  @override 
  Widget build(BuildContext context) { 
    return Container( 
       child: ListView( 
         /* use the data over here */ 
       ), 
    ); 
  } 
}