flutter-BottomNavigationBar切换页面保持状态

 flutter-BottomNavigationBar切换页面保持状态

PageView加AutomaticKeepAliveClientMixin解决

import 'package:flutter/material.dart';

class HomeRoute extends StatefulWidget {
  @override
  _HomeRouteState createState() => _HomeRouteState();
}

class _HomeRouteState extends State {
  int _currentIndex = 0;
  final List _pages = [
    Mainpage(),
    CatePage(),
    ShoppingCart(),
    MePage()
  ];
  PageController _controller;

  @override
  void initState() {
    super.initState();
    _controller = PageController(initialPage: 0);
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("卧槽"),
      ),
      bottomNavigationBar: _buildBottomNavigationBarWidget(),
      body: _buildBodyWidget(),
    );
  }

  Widget _buildBodyWidget() {
    return PageView.builder(
      controller: _controller,
      itemCount: _pages.length,
      physics: NeverScrollableScrollPhysics(),
      itemBuilder: (context, index) {
        return _pages[index];
      },
      onPageChanged: (index) {
        setState(() {
          _currentIndex = index;
        });
      },
    );
  }

  Widget _buildBottomNavigationBarWidget() {
    return BottomNavigationBar(
      type: BottomNavigationBarType.fixed,
      backgroundColor: Colors.white,
      currentIndex: _currentIndex,
      fixedColor: Colors.red,
      unselectedItemColor: Colors.grey,
      items: [
        BottomNavigationBarItem(title: Text("首页"), icon: Icon(Icons.home)),
        BottomNavigationBarItem(title: Text("分类"), icon: Icon(Icons.widgets)),
        BottomNavigationBarItem(
            title: Text("购物车"), icon: Icon(Icons.child_friendly)),
        BottomNavigationBarItem(title: Text("我的"), icon: Icon(Icons.person))
      ],
      onTap: (index) {
        _controller.jumpToPage(index);
      },
    );
  }
}

class Mainpage extends StatefulWidget {
  @override
  _MainpageState createState() => _MainpageState();
}

class _MainpageState extends State
    with AutomaticKeepAliveClientMixin {
  @protected
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container(child: Column(
      children: [
        Text("data")
      ],
    ),);
  }
}

class CatePage extends StatefulWidget {
  @override
  _CatePageState createState() => _CatePageState();
}

class _CatePageState extends State
    with AutomaticKeepAliveClientMixin {
  int a = 0;
  @override
  bool get wantKeepAlive => true;
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container(
      child: Column(
        children: [
          Text("a$a"),
          Text("a$a"),
          Text("a$a"),
          Text("a$a"),
          Text("a$a"),
          Text("a$a"),
          Text("a$a"),
          Text("a$a"),
          Text("a$a"),
          Text("a$a"),
          Text("a$a"),
          RaisedButton(
            child: Text("点击"),
            onPressed: () {
              a++;
              setState(() {});
            },
          ),
        ],
      ),
    );
  }

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

class ShoppingCart extends StatefulWidget {
  @override
  _ShoppingCartState createState() => _ShoppingCartState();
}

class _ShoppingCartState extends State
    with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container();
  }

  @override
  bool get wantKeepAlive => true;
}

class MePage extends StatefulWidget {
  @override
  _MePageState createState() => _MePageState();
}

class _MePageState extends State {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

 

你可能感兴趣的:(flutter踩坑记)