Skip to content

[animations package] weird State object instantiations when using PageTransitionSwitcher and FooTransition widgets included in package. #53550

Closed

Description

@grinder15

I have two stateful widgets to switch using PageTransitionSwitcher but it looks like my app instantiate state objects twice, while the animation is running and after. This not happens when using AnimatedSwitcher. I think this is an optimization issue?

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: HomePage(),
    );
  }
}

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

class _HomePageState extends State<HomePage> {
  bool _isCounterPage = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home Page'),
      ),
      body: PageTransitionSwitcher(
        duration: const Duration(milliseconds: 500),
        transitionBuilder: (child, animation, secondaryAnimation) {
          return FadeThroughTransition(
            child: child,
            animation: animation,
            secondaryAnimation: secondaryAnimation,
          );
        },
        child: _isCounterPage ? CounterPage() : StatefulPage(),
      ),
      floatingActionButton: FloatingActionButton(
          child: Icon(Icons.sync),
          onPressed: () {
            setState(() {
              _isCounterPage = !_isCounterPage;
            });
          }),
    );
  }
}

class CounterPage extends StatefulWidget {
  @override
  _CounterPageState createState() => _CounterPageState();
}

class _CounterPageState extends State<CounterPage> {
  int _counter = 0;

  @override
  void initState() {
    super.initState();
    print('CounterPage initState called.');
  }

  @override
  void dispose() {
    print('CounterPage dispose called.');
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Text('Counter value: $_counter'),
          RaisedButton(
            child: Text('Increment'),
            onPressed: () {
              setState(() {
                _counter++;
              });
            },
          ),
        ],
      ),
    );
  }
}

class StatefulPage extends StatefulWidget {
  @override
  _StatefulPageState createState() => _StatefulPageState();
}

class _StatefulPageState extends State<StatefulPage> {
  String _text;
  TextEditingController _controller;

  @override
  void initState() {
    super.initState();
    _text = '';
    _controller = TextEditingController();
    print('StatefulPage initState called.');
  }

  @override
  void dispose() {
    print('StatefulPage dispose called.');
    _controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Text('Saved text: $_text'),
          TextField(
            controller: _controller,
          ),
          RaisedButton(
            child: Text('Save text'),
            onPressed: () {
              setState(() {
                _text = _controller.text;
                _controller.text = '';
              });
            },
          )
        ],
      ),
    );
  }
}

console output

before switch:

I/flutter (20212): CounterPage initState called.

after switch:

I/flutter (20212): CounterPage initState called.
I/flutter (20212): StatefulPage initState called.
I/flutter (20212): CounterPage dispose called.
I/flutter (20212): StatefulPage initState called.
I/flutter (20212): StatefulPage dispose called.
I/flutter (20212): CounterPage dispose called.

flutter doctor

[√] Flutter (Channel beta, v1.15.17, on Microsoft Windows [Version 10.0.18362.535], locale en-PH)
    • Flutter version 1.15.17 at C:\src\flutter
    • Framework revision 2294d75bfa (3 weeks ago), 2020-03-07 00:28:38 +0900
    • Engine revision 5aff311948
    • Dart version 2.8.0 (build 2.8.0-dev.12.0 9983424a3c)


[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at C:\Users\Jerome\AppData\Local\Android\sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.2
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 3.6)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 44.0.2
    • Dart plugin version 192.7761
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)

[√] VS Code (version 1.42.1)
    • VS Code at C:\Users\Jerome\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.8.1

[√] Connected device (3 available)
    • AOSP on IA Emulator • emulator-5554 • android-x86    • Android 9 (API 28) (emulator)
    • Chrome              • chrome        • web-javascript • Google Chrome 80.0.3987.149
    • Web Server          • web-server    • web-javascript • Flutter Tools

• No issues found!

Activity

  1. self-assigned this
    on Mar 30, 2020
  2. added
    packageflutter/packages repository. See also p: labels.
    and removed on Mar 30, 2020
  3. github-actions commented on Aug 22, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  4. locked as resolved and limited conversation to collaborators on Aug 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

a: qualityA truly polished experiencehas reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: animationsThe animations packagepackageflutter/packages repository. See also p: labels.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions