Skip to content

Render widgets based on AppLifeCycleState is not working (build method is never called for inactive state) #33236

@pravinarr

Description

@pravinarr

I have the following code which will replace the child with a container when the app goes to inactive or paused state.


import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: SecureScreen(
          child: Container(
            color: Colors.red,
          ),
        ),
      ),
    );
  }
}

class SecureScreen extends StatefulWidget {
  final Widget child;
  SecureScreen({@required this.child});

  @override
  SecureScreenState createState() => SecureScreenState();
}

class SecureScreenState extends State<SecureScreen>
    with WidgetsBindingObserver {
  AppLifecycleState _notification;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    print(state.toString());
    setState(() {
      _notification = state;
    });
  }

  @override
  Widget build(BuildContext context) {
    return (_notification == AppLifecycleState.paused ||
            _notification == AppLifecycleState.inactive)
        ? Container(
            color: Colors.greenAccent,
          )
        : widget.child;
  }
}


So when I double click the home button in IOS to view all the open applications, it is triggering
"inactive" state but it is not calling the build() method to render container with greenAccent color.

I have attached the screenshot for you reference.

Does this needs additional code to achieve the result ?
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectc: new featureNothing broken; request for a new capabilityengineflutter/engine related. See also e: labels.frameworkflutter/packages/flutter repository. See also f: labels.platform-iosiOS applications specifically

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions