Giter Club home page Giter Club logo

Comments (2)

karamba121 avatar karamba121 commented on August 22, 2024

With some research, I found a workaround. Maybe it's good to incorporate to behavior of a TabView in this Lib.

Based on post from Diego Velasquez, at https://medium.com/@diegoveloper/flutter-persistent-tab-bars-a26220d322bc,
Here it goes:

import 'package:fluent_ui/fluent_ui.dart';

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

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

  @override
  State<TestPage> createState() => _TestPageState();
}

class _TestPageState extends State<TestPage> {
  int currentIndex = 0;
  @override
  Widget build(BuildContext context) {
    return FluentApp(
      home: TabView(
        currentIndex: currentIndex,
        onChanged: (value) => setState(() {
          currentIndex = value;
        }),
        tabs: [
          Tab(text: const Text('test 1'), body: const CounterPage()),
          Tab(text: const Text('test 2'), body: const CounterPage()),
          Tab(text: const Text('test 3'), body: const CounterPage()),
        ],
      ),
    );
  }
}

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

  @override
  State<CounterPage> createState() => _CounterPageState();
}

class _CounterPageState extends State<CounterPage> with AutomaticKeepAliveClientMixin<CounterPage> {
  int counter = 0;

  void increment(){
    setState(() {
      counter++;
    });
  }
  @override
  Widget build(BuildContext context) {
    return Column(children: [
      Text('$counter'),
      Button(onPressed: increment, child: const Text('increment'))
    ],);
  }

  
  @override
  bool get wantKeepAlive => true;
}

just add the "AutomaticKeepAliveClientMixin" in the state of stateful widget and override the "wantKeepAlive" property to "true", boom! the magic it did.

Thanks in advance, the post from Diego Velasquez and the fluent_ui by itself is the most incredible thing that I see in flutter in the last years.

PS: I'm brazilian and my english maybe is not the best, forgive me by the prose, please.

from fluent_ui.

bdlukaa avatar bdlukaa commented on August 22, 2024

TabView uses PageView under the hood, which doesn't support the automatic usage of KeepAlives, like list view or sliver lists do. It is up to the developer to add it, via AutomaticKeepAliveClientMixin, since adding it automatically including it can lead to several performance problems.

In case of usage of go_router, one can use StatefulShellRoute to keep the state of the routes. Another options are also available, such as page storage.

from fluent_ui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.