AutomaticKeepAliveClientMixin不能与BottomNavigationBar一起使用吗?

时间:2019-01-03 13:21:15

标签: flutter flutter-navigation

我有一个ListView,它从异步http get调用中接收项目。我在initState上设置了列表项,然后只想使用RefreshIndicator.onRefresh()重新加载它们,但是由于AutomaticKeepAliveClientMixin每次我更改窗口小部件都不起作用时,它将调用窗口小部件的initState。 我是否缺少某些东西,还是只应该与TabView

一起使用?
class Home extends StatefulWidget {
    @override
    State<StatefulWidget> createState() {
    return _HomeState();
    }
}

class _HomeState extends State<Home> {
    final Key keyNews = PageStorageKey('newsHome');

    int _selectedIndex;
    Widget selectedPage;

    NewsHome newsHome;
    TeamHome teamHome;
    StarsHome starsHome;

    List<Widget> _widgetOptions;

    @override
    Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            title: Image(image: AssetImage('assets/images/logo.png')),
            centerTitle: true,
        ),
        body: Container(
            decoration: BoxDecoration(
                image: DecorationImage(
                    image: AssetImage('assets/images/fundo_conteudos.png'),
                    fit: BoxFit.cover)),
            child: Column(children: [
            Expanded(
                child: selectedPage)
            ])),
        bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            fixedColor: Colors.orange[900],
            items: [
            BottomNavigationBarItem(
                title: Text('HOME'), icon: Icon(Icons.home)),
            BottomNavigationBarItem(
                title: Text('EQUIPA'),
                icon: ImageIcon(
                    AssetImage('assets/images/bottom/equipa.png'),
                )),
            BottomNavigationBarItem(
                title: Text('ESTRELAS'),
                icon: ImageIcon(
                    AssetImage('assets/images/bottom/estrelas.png'),
                ))
            ],
            currentIndex: _selectedIndex,
            onTap: _onItemTapped,
        ));
    }

    void _onItemTapped(int index) {
    setState(() {
        _selectedIndex = index;
        selectedPage = _widgetOptions[index];
    });
    }

    @override
    void initState() {
    super.initState();

    starsHome = StarsHome();
    teamHome = TeamHome();
    newsHome = NewsHome(key: keyNews);

    _selectedIndex = 0;
    selectedPage = newsHome;

    _widgetOptions = [newsHome, teamHome, starsHome];
    }
}

class NewsHome extends StatefulWidget {

    List news;

    NewsHome({Key key, this.news}) : super(key: key);

    @override
    State<StatefulWidget> createState() {
    return _NewsHomeState();
    }
}

Future<List> fetchNews(int number) async {
    List _news = new List();

    final response =
    await http.get('http://someapiwebsite/api/news');

    dynamic jsondecode = json.decode(response.body);

    for(int i = 0; i < number; i++){
    if(jsondecode[i] != null){
        _news.add(News.fromJson(jsondecode[i]));
    }
    }
    /*for (var news in jsondecode) {
    _news.add(new News.fromJson(news));
    }*/
    print('ran fetchNews');

    return _news;
}

class _NewsHomeState extends State<NewsHome> with AutomaticKeepAliveClientMixin<NewsHome>{
    List _news = new List();

    @override
    Widget build(BuildContext context) {
    super.build(context);
    return Container(
        child: RefreshIndicator(child: ListView.builder(
                physics: const AlwaysScrollableScrollPhysics(),
                padding: EdgeInsets.all(0),
                itemBuilder: (context, index) {
                    return NewsListItem(news:_news[index]);
                },
                itemCount: _news.length,

                ), onRefresh: () {
                return fetchNews(3).then((value) {
                    setState(() {
                    _news = value;
                    });
                });
    }));
    }

    @override
    void initState() {
    super.initState();
    fetchNews(2).then((value){
        setState(() {
        _news = value;
        });
    });
    }

    @override
    bool get wantKeepAlive => true;
}

还有另一种我不知道的ListView填充方法吗?

0 个答案:

没有答案