Skip to content

Automatic Keep Alive not work #16346

@chaokongzwp

Description

@chaokongzwp

Steps to R

I use the AutomaticKeepAliveClientMixin in my widget , but it does not work ,when the widget scroll out the viewport , the widget doesn't keep alive

this is my demo code, my purpose is keeping DetailCommentListHeader widget alive when DetailCommentListHeader scroll out the viewport, but when DetailCommentListHeader scorll out, DetailCommentHeaderState dispose was call , pls see the log

import 'package:flutter/material.dart';


class DetailCommentListHeader extends StatefulWidget{
  DetailCommentListHeader({Key key}):super(key:key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new DetailCommentHeaderState();
  }
}

class DetailCommentHeaderState extends State<DetailCommentListHeader> with AutomaticKeepAliveClientMixin<DetailCommentListHeader> {
  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print('DetailCommentHeaderState initState');
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    print("DetailCommentListHeader dispose");
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    String text = 'need keep alive';

    // TODO: implement build
    return new Container(
      color: Colors.white,
      height: 48.0,
      child: new Padding(padding: new EdgeInsets.fromLTRB(16.0, 14.0, 0.0 , 14.0,),
        child: new Text(text, style: new TextStyle(fontSize: 16.0, fontWeight: FontWeight.bold),),
      ),
    );
  }
}

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Keep Alive Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new ListView.builder(
          itemCount: 50,
          itemBuilder: (BuildContext context, int index){
            if (index == 0){
              return new DetailCommentListHeader();
            }
            return new Container(
              height: 44.0,
              child: new Text('just padding'),
              color: Colors.yellow.shade300,
            );
          },
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

Logs

DetailCommentListHeader dispose
DetailCommentHeaderState initState
DetailCommentListHeader dispose
DetailCommentHeaderState initState
DetailCommentListHeader dispose
DetailCommentHeaderState initState

Flutter Doctor

[✓] Flutter (Channel beta, v0.2.8, on Mac OS X 10.13.4 17E199, locale zh-Hans-CN)
• Flutter version 0.2.8 at /Users/chaokong/flutterSDK
• Framework revision b397406 (7 days ago), 2018-04-02 13:53:20 -0700
• Engine revision c903c21
• Dart version 2.0.0-dev.43.0.flutter-52afcba357

[!] Android toolchain - develop for Android devices (Android SDK 27.0.3)
• Android SDK at /Users/chaokong/Library/Android/sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-27, build-tools 27.0.3
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-915-b08)
! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses

[✓] iOS toolchain - develop for iOS devices (Xcode 9.3)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 9.3, Build version 9E145
• ios-deploy 1.9.2
• CocoaPods version 1.2.0

[✓] Android Studio (version 3.0)
• Android Studio at /Applications/Android Studio.app/Contents
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-915-b08)

[✓] IntelliJ IDEA Community Edition (version 2017.3.5)
• IntelliJ at /Applications/IntelliJ IDEA CE.app
• Flutter plugin installed
• Dart plugin version 173.4548.30

[✓] Connected devices (2 available)
• 张伟平的 iPhone • fb87fda18a77f01197d25047bed12a782b21e01f • ios • iOS 11.2.2
• iPhone X • E1F86F6C-535C-4691-9C4F-1D90F5A7668C • ios • iOS 11.3 (simulator)

! Doctor found issues in 1 category.

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions