Flutter PageView + BottomNavigationBar预加载

时间:2019-03-02 05:37:19

标签: flutter

我现在有问题。

我的主页有三页,

所有三个页面都继承State并带有AutomaticKeepAliveClientMixin 页面状态已保存

现在的问题是,成功登录后,第一个子页面通常会创建一个实例。当我选择第二个页面时,第三个页面也会创建一个实例。

希望我选择一个页面时,仅创建当前页面的一个实例,而不是多个实例。

代码:

    import 'package:dada/pages/home/content.dart';
     import 'package:dada/pages/home/person.dart';
     import 'package:dada/pages/home/home.dart';
     import 'package:flutter/material.dart';

     class HomePage extends StatefulWidget {
       _HomePageState createState() => _HomePageState();
      }

    class _HomePageState extends State<HomePage> {

     final _bottomColor = Colors.blue;
     int _currentIndex = 0;
     var _pageController =  PageController(initialPage: 0);

     List<Widget> widgetList = [];

     @override
     void initState() {
       widgetList
       ..add(Home())
       ..add(Content())
       ..add(Person());
       super.initState();
         }

     @override
     void dispose(){
       super.dispose();
         _pageController.dispose();
      }

     void _pageChange(int index) {
       setState(() {
         if (_currentIndex != index) {
           _currentIndex = index;
          }
       });
     }

     @override
     Widget build(BuildContext context) {
      return Scaffold(
       body: PageView.builder(
          onPageChanged: _pageChange,
          controller: _pageController,
          itemBuilder: (BuildContext context,int index){
               return widgetList[index];
          },

          itemCount: widgetList.length,

      ), 
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: _currentIndex,
        onTap: (int index){
           setState(() {
            _currentIndex = index;
          });
        _pageController.animateToPage(index, duration:  Duration(seconds: 2),curve: ElasticOutCurve(0.8));
        },
        items: [
          BottomNavigationBarItem(
              icon: Icon(
                Icons.home,
                color: _bottomColor,
              ),
              title: Text("主页", style: TextStyle(color: _bottomColor))),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.contacts,
                color: _bottomColor,
              ),
              title: Text("内容", style: TextStyle(color: _bottomColor))),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.person,
                color: _bottomColor,
              ),
              title: Text("个人中心", style: TextStyle(color: _bottomColor)))
        ],
      ),
    );
  }
}

子页面:

import 'package:flutter/material.dart';

class Home extends StatefulWidget {
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> with AutomaticKeepAliveClientMixin {


@override
void initState(){
  super.initState();
  print("ttttt");
}

 @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      body: Center(
        child: RaisedButton(
          child: Text("测试"),
          onPressed: (){
            print("---");
          },
        ),
      ),
    );
  }
}

0 个答案:

没有答案