Skip to content

🐛 Regression: page state is not saved #1300

@artdedeco

Description

@artdedeco

Describe the bug
Prior to 4.14, I've been successfully using AutomaticKeepAliveClientMixin to keep state widgets alive between page changes in a NavigationView. With 4.14 that behavior broke, and changing pages (panes) in a navigation view now disposes and reinitializes the corresponding state widget every time.

To Reproduce
Steps to reproduce the behavior:

import 'dart:math';

import 'package:fluent_ui/fluent_ui.dart';

void main() {
  runApp(const MainApp());
}

class MainApp extends StatefulWidget {
  const MainApp({super.key});

  @override
  State<MainApp> createState() => _MainApp();
}

class _MainApp extends State<MainApp> {
  int index = 0;

  @override
  Widget build(BuildContext context) {
    return FluentApp(
      home: NavigationView(
        pane: NavigationPane(
          selected: index,
          onChanged: (index) => setState(() => this.index = index),
          items: [
            PaneItem(
              icon: Icon(FluentIcons.home),
              title: Text('Home'),
              body: Center(child: Text('Home Page')),
            ),
            PaneItem(
              icon: Icon(FluentIcons.settings),
              title: Text('Settings'),
              body: SettingsPage(),
            ),
          ],
        ),
      ),
    );
  }
}

class SettingsPage extends StatefulWidget {
  const SettingsPage({super.key});

  @override
  State<SettingsPage> createState() => _SettingsPageState();
}

class _SettingsPageState extends State<SettingsPage>
    with AutomaticKeepAliveClientMixin {
  int _counter = 0;

  @override
  void initState() {
    super.initState();
    _counter = Random().nextInt(100);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Center(child: Text('Counter: $_counter'));
  }

  @override
  bool get wantKeepAlive => true;
}

With fluent_ui 4.14: Switching between the two pages causes a different number to be displayed each time.
With fluent_ui 4.13: The same number is displayed every time.

Expected behavior
The state is maintained.

Screenshots
n/a

Additional context
n/a

Metadata

Metadata

Assignees

No one assigned

    Labels

    w: NavigationViewRelated to the NavigationView widget

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions