Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

遇到一个KeepAlive的问题希望能帮忙解答下 #203

Closed
iBotasky opened this issue Dec 28, 2018 · 1 comment
Closed

遇到一个KeepAlive的问题希望能帮忙解答下 #203

iBotasky opened this issue Dec 28, 2018 · 1 comment

Comments

@iBotasky
Copy link

iBotasky commented Dec 28, 2018

我的主界面有3个Tab,每个Tab都继承了AutomaticKeepAliveClientMixin,在主界面点击Tab切换页面,确实每个页面也只走了一遍initState,但是很操蛋的是我这时候跳转到了第二个界面,在回来,点击Tab切换的时候,每个页面重新走了initState,我看了项目的代码,貌似跟我的没什么差,但是你的从第二个界面出来后还保持原来状态,不会重新init
下面是我的一个可以复现的小代码:

import 'package:flutter/material.dart';
//import 'package:flutter_cybird/constant/Constant.dart';
//import 'package:flutter_cybird/ui/Home.dart';
//import 'package:flutter_cybird/ui/SplashScreen.dart';

//void main() => runApp(MaterialApp(
//      title: 'Fybird',
//      theme: ThemeData(primaryColor: Colors.blue[800]),
//      home: SplashScreen(),
//      routes: {
//        HOME_SCREEN: (BuildContext context) => HomePage(),
//        ANIMATED_SPLASH: (BuildContext context) => new SplashScreen(),
//      },
//    ));
void main() => runApp(MaterialApp(
      title: 'Fybird',
      theme: ThemeData(primaryColor: Colors.blue[800]),
      home: DefaultTabController(
        length: 3,
        child: Scaffold(
          appBar: AppBar(
            bottom: TabBar(
              tabs: [
                Tab(icon: Icon(Icons.directions_car)),
                Tab(icon: Icon(Icons.directions_transit)),
                Tab(icon: Icon(Icons.directions_bike)),
              ],
            ),
            title: Text('First Page'),
          ),
          body: TabBarView(
            children: [
              TabView(),
              TabView(),
              TabView(),
            ],
          ),
        ),
      ),
    ));

class TabView extends StatefulWidget {
  @override
  _TabViewState createState() => _TabViewState();
}

class _TabViewState extends State<TabView> with AutomaticKeepAliveClientMixin {
  @override
  void initState() {
    super.initState();
    debugPrint("Tab View init");
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        child: Center(
          child: RaisedButton(
            onPressed: () {
              Navigator.push(context,
                  MaterialPageRoute(builder: (context) => SecondPage()));
            },
            child: Text("Init all TabViews and tap to New Page"),
          ),
        ));
  }

  @override
  bool get wantKeepAlive => true;
}

class SecondPage extends StatefulWidget {
  @override
  _SecondPageState createState() => _SecondPageState();
}

class _SecondPageState extends State<SecondPage> with AutomaticKeepAliveClientMixin{
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Second Page"),
      ),
      body: Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        child: Center(
            child: Text(
                "Back to the first page, and tap the TapView will init again, not alive.")),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
@iBotasky
Copy link
Author

已经解决了,我在flutter的issue也提了这个问题,他们说是没有在继承AutomaticKeepAliveMixin的build方法调用super.build(context);,我调用之后就可以了。

@CarGuo CarGuo mentioned this issue Nov 21, 2019
@w497763094 w497763094 mentioned this issue Mar 10, 2020
@JevonShi JevonShi mentioned this issue Mar 10, 2020
@HaobinXu HaobinXu mentioned this issue Mar 20, 2020
@luan823230 luan823230 mentioned this issue Mar 24, 2020
@hzg1105 hzg1105 mentioned this issue Jun 14, 2020
@wangkr wangkr mentioned this issue Jul 4, 2020
@jayVRIY jayVRIY mentioned this issue Dec 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant