Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance issue #24

Open
ilyesksr opened this issue Jan 30, 2025 · 0 comments
Open

Performance issue #24

ilyesksr opened this issue Jan 30, 2025 · 0 comments

Comments

@ilyesksr
Copy link

I notice that when the TabContainer widget get build, all it children get build at the same time even thought only one tab is displayed.To fix this issue you should treat the children property in your TabContainer widget as List and not a List so only the displayed widgets gets build and not all of them.

Here is an example of a solution :

import 'package:flutter/material.dart';
import 'package:tab_container/tab_container.dart';

class MyCustomTabBar extends StatefulWidget {
  final TabController controller;
  final TabEdge tabEdge;
  final double tabsStart;
  final double tabsEnd;
  final double? tabMaxLength;
  final BorderRadius borderRadius;
  final BorderRadius tabBorderRadius;
  final EdgeInsets childPadding;
  final TextStyle? selectedTextStyle;
  final TextStyle? unselectedTextStyle;
  final Color? color;
  final double? tabExtent;
  final List<Widget> tabs;
  final List<Widget> children;
  // Continu Add other properties

  const MyCustomTabBar(
      {super.key,
      required this.controller,
      required this.tabEdge,
      required this.tabsStart,
      required this.tabsEnd,
      this.tabMaxLength,
      required this.borderRadius,
      required this.tabBorderRadius,
      required this.childPadding,
      this.selectedTextStyle,
      this.unselectedTextStyle,
      this.color,
      this.tabExtent,
      required this.tabs,
      required this.children});

  @override
  State<MyCustomTabBar> createState() => _MyCustomTabBarState();
}

class _MyCustomTabBarState extends State<MyCustomTabBar> {
  late List<WidgetBuilder> _childrenBuilders; // Add this and use it
  @override
  void initState() {
    super.initState();
    _childrenBuilders = widget.children.map((e) => (context) => e).toList();
    widget.controller.addListener(() {
      if (widget.controller.indexIsChanging) {
        setState(() {});
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    widget.controller.removeListener(
      () {},
    );
  }

  @override
  Widget build(BuildContext context) {
    return TabContainer(
      controller: widget.controller,
      tabEdge: widget.tabEdge,
      tabsStart: widget.tabsStart,
      tabsEnd: widget.tabsEnd,
      tabMaxLength: widget.tabMaxLength ?? double.infinity,
      borderRadius: widget.borderRadius,
      tabBorderRadius: widget.tabBorderRadius,
      childPadding: widget.childPadding,
      selectedTextStyle: widget.selectedTextStyle,
      unselectedTextStyle: widget.unselectedTextStyle,
      color: widget.color,
      tabExtent: widget.tabExtent ?? 50,
      tabs: widget.tabs,
      child: _getTab(context),
      // Continu Add other properties
    );
  }

  Widget _getTab(BuildContext context) {
    return _childrenBuilders[widget.controller.index](context);
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant