• Taha Tesser's avatar
    Fix Scrollable `TabBar` for Material 3 (#131409) · 2c71881f
    Taha Tesser authored
    fixes [Material 3 `TabBar` does not take full width when `isScrollable: true`](https://github.com/flutter/flutter/issues/117722)
    
    ### Description
    1. Fixed the divider doesn't stretch to take all the available width in the scrollable tab bar in M3
    2. Added `dividerHeight` property.
    
    ### Code sample
    
    <details> 
    <summary>expand to view the code sample</summary> 
    
    ```dart
    import 'package:flutter/material.dart';
    
    /// Flutter code sample for [TabBar].
    
    void main() => runApp(const TabBarApp());
    
    class TabBarApp extends StatelessWidget {
      const TabBarApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return const MaterialApp(
          debugShowCheckedModeBanner: false,
          home: TabBarExample(),
        );
      }
    }
    
    class TabBarExample extends StatefulWidget {
      const TabBarExample({super.key});
    
      @override
      State<TabBarExample> createState() => _TabBarExampleState();
    }
    
    class _TabBarExampleState extends State<TabBarExample> {
      bool rtl = false;
      bool customColors = false;
      bool removeDivider = false;
      Color dividerColor = Colors.amber;
      Color indicatorColor = Colors.red;
    
      @override
      Widget build(BuildContext context) {
        return DefaultTabController(
          initialIndex: 1,
          length: 3,
          child: Directionality(
            textDirection: rtl ? TextDirection.rtl : TextDirection.ltr,
            child: Scaffold(
              appBar: AppBar(
                title: const Text('TabBar Sample'),
                actions: <Widget>[
                  IconButton.filledTonal(
                    tooltip: 'Switch direction',
                    icon: const Icon(Icons.swap_horiz),
                    onPressed: () {
                      setState(() {
                        rtl = !rtl;
                      });
                    },
                  ),
                  IconButton.filledTonal(
                    tooltip: 'Use custom colors',
                    icon: const Icon(Icons.color_lens),
                    onPressed: () {
                      setState(() {
                        customColors = !customColors;
                      });
                    },
                  ),
                  IconButton.filledTonal(
                    tooltip: 'Show/hide divider',
                    icon: const Icon(Icons.remove_rounded),
                    onPressed: () {
                      setState(() {
                        removeDivider = !removeDivider;
                      });
                    },
                  ),
                ],
              ),
              body: Column(
                children: <Widget>[
                  const Spacer(),
                  const Text('Scrollable - TabAlignment.start'),
                  TabBar(
                    isScrollable: true,
                    tabAlignment: TabAlignment.start,
                    dividerColor: customColors ? dividerColor : null,
                    indicatorColor: customColors ? indicatorColor : null,
                    dividerHeight: removeDivider ? 0 : null,
                    tabs: const <Widget>[
                      Tab(
                        icon: Icon(Icons.cloud_outlined),
                      ),
                      Tab(
                        icon: Icon(Icons.beach_access_sharp),
                      ),
                      Tab(
                        icon: Icon(Icons.brightness_5_sharp),
                      ),
                    ],
                  ),
                  const Text('Scrollable - TabAlignment.startOffset'),
                  TabBar(
                    isScrollable: true,
                    tabAlignment: TabAlignment.startOffset,
                    dividerColor: customColors ? dividerColor : null,
                    indicatorColor: customColors ? indicatorColor : null,
                    dividerHeight: removeDivider ? 0 : null,
                    tabs: const <Widget>[
                      Tab(
                        icon: Icon(Icons.cloud_outlined),
                      ),
                      Tab(
                        icon: Icon(Icons.beach_access_sharp),
                      ),
                      Tab(
                        icon: Icon(Icons.brightness_5_sharp),
                      ),
                    ],
                  ),
                  const Text('Scrollable - TabAlignment.center'),
                  TabBar(
                    isScrollable: true,
                    tabAlignment: TabAlignment.center,
                    dividerColor: customColors ? dividerColor : null,
                    indicatorColor: customColors ? indicatorColor : null,
                    dividerHeight: removeDivider ? 0 : null,
                    tabs: const <Widget>[
                      Tab(
                        icon: Icon(Icons.cloud_outlined),
                      ),
                      Tab(
                        icon: Icon(Icons.beach_access_sharp),
                      ),
                      Tab(
                        icon: Icon(Icons.brightness_5_sharp),
                      ),
                    ],
                  ),
                  const Spacer(),
                  const Text('Non-scrollable - TabAlignment.fill'),
                  TabBar(
                    tabAlignment: TabAlignment.fill,
                    dividerColor: customColors ? dividerColor : null,
                    indicatorColor: customColors ? indicatorColor : null,
                    dividerHeight: removeDivider ? 0 : null,
                    tabs: const <Widget>[
                      Tab(
                        icon: Icon(Icons.cloud_outlined),
                      ),
                      Tab(
                        icon: Icon(Icons.beach_access_sharp),
                      ),
                      Tab(
                        icon: Icon(Icons.brightness_5_sharp),
                      ),
                    ],
                  ),
                  const Text('Non-scrollable - TabAlignment.center'),
                  TabBar(
                    tabAlignment: TabAlignment.center,
                    dividerColor: customColors ? dividerColor : null,
                    indicatorColor: customColors ? indicatorColor : null,
                    dividerHeight: removeDivider ? 0 : null,
                    tabs: const <Widget>[
                      Tab(
                        icon: Icon(Icons.cloud_outlined),
                      ),
                      Tab(
                        icon: Icon(Icons.beach_access_sharp),
                      ),
                      Tab(
                        icon: Icon(Icons.brightness_5_sharp),
                      ),
                    ],
                  ),
                  const Spacer(),
                  const Text('Secondary - TabAlignment.fill'),
                  TabBar.secondary(
                    tabAlignment: TabAlignment.fill,
                    dividerColor: customColors ? dividerColor : null,
                    indicatorColor: customColors ? indicatorColor : null,
                    dividerHeight: removeDivider ? 0 : null,
                    tabs: const <Widget>[
                      Tab(
                        icon: Icon(Icons.cloud_outlined),
                      ),
                      Tab(
                        icon: Icon(Icons.beach_access_sharp),
                      ),
                      Tab(
                        icon: Icon(Icons.brightness_5_sharp),
                      ),
                    ],
                  ),
                  const Text('Secondary - TabAlignment.center'),
                  TabBar.secondary(
                    tabAlignment: TabAlignment.center,
                    dividerColor: customColors ? dividerColor : null,
                    indicatorColor: customColors ? indicatorColor : null,
                    dividerHeight: removeDivider ? 0 : null,
                    tabs: const <Widget>[
                      Tab(
                        icon: Icon(Icons.cloud_outlined),
                      ),
                      Tab(
                        icon: Icon(Icons.beach_access_sharp),
                      ),
                      Tab(
                        icon: Icon(Icons.brightness_5_sharp),
                      ),
                    ],
                  ),
                  const Spacer(),
                ],
              ),
            ),
          ),
        );
      }
    }
    ``` 
    	
    </details>
    
    ### Before
    
    ![Screenshot 2023-07-27 at 14 12 36](https://github.com/flutter/flutter/assets/48603081/1c08a9d2-ac15-4d33-8fa1-c765b4b10f92)
    
    ### After 
    
    ![Screenshot 2023-07-27 at 14 13 12](https://github.com/flutter/flutter/assets/48603081/7e662dfe-9f32-46c9-a128-3024a4782882)
    
    This also contains regression test for https://github.com/flutter/flutter/pull/125974#discussion_r1239089151
    
    ```dart
      // This is a regression test for https://github.com/flutter/flutter/pull/125974#discussion_r1239089151.
      testWidgets('Divider can be constrained', (WidgetTester tester) async {
    ```
    
    ![Screenshot 2023-07-27 at 14 16 37](https://github.com/flutter/flutter/assets/48603081/ac2ef49b-2410-46d0-8ae2-d9b77236abba)
    2c71881f
Name
Last commit
Last update
..
animated_icons Loading commit data...
shaders Loading commit data...
about.dart Loading commit data...
action_buttons.dart Loading commit data...
action_chip.dart Loading commit data...
action_icons_theme.dart Loading commit data...
adaptive_text_selection_toolbar.dart Loading commit data...
animated_icons.dart Loading commit data...
app.dart Loading commit data...
app_bar.dart Loading commit data...
app_bar_theme.dart Loading commit data...
arc.dart Loading commit data...
autocomplete.dart Loading commit data...
back_button.dart Loading commit data...
badge.dart Loading commit data...
badge_theme.dart Loading commit data...
banner.dart Loading commit data...
banner_theme.dart Loading commit data...
bottom_app_bar.dart Loading commit data...
bottom_app_bar_theme.dart Loading commit data...
bottom_navigation_bar.dart Loading commit data...
bottom_navigation_bar_theme.dart Loading commit data...
bottom_sheet.dart Loading commit data...
bottom_sheet_theme.dart Loading commit data...
button.dart Loading commit data...
button_bar.dart Loading commit data...
button_bar_theme.dart Loading commit data...
button_style.dart Loading commit data...
button_style_button.dart Loading commit data...
button_theme.dart Loading commit data...
calendar_date_picker.dart Loading commit data...
card.dart Loading commit data...
card_theme.dart Loading commit data...
checkbox.dart Loading commit data...
checkbox_list_tile.dart Loading commit data...
checkbox_theme.dart Loading commit data...
chip.dart Loading commit data...
chip_theme.dart Loading commit data...
choice_chip.dart Loading commit data...
circle_avatar.dart Loading commit data...
color_scheme.dart Loading commit data...
colors.dart Loading commit data...
constants.dart Loading commit data...
curves.dart Loading commit data...
data_table.dart Loading commit data...
data_table_source.dart Loading commit data...
data_table_theme.dart Loading commit data...
date.dart Loading commit data...
date_picker.dart Loading commit data...
date_picker_theme.dart Loading commit data...
debug.dart Loading commit data...
desktop_text_selection.dart Loading commit data...
desktop_text_selection_toolbar.dart Loading commit data...
desktop_text_selection_toolbar_button.dart Loading commit data...
dialog.dart Loading commit data...
dialog_theme.dart Loading commit data...
divider.dart Loading commit data...
divider_theme.dart Loading commit data...
drawer.dart Loading commit data...
drawer_header.dart Loading commit data...
drawer_theme.dart Loading commit data...
dropdown.dart Loading commit data...
dropdown_menu.dart Loading commit data...
dropdown_menu_theme.dart Loading commit data...
elevated_button.dart Loading commit data...
elevated_button_theme.dart Loading commit data...
elevation_overlay.dart Loading commit data...
expand_icon.dart Loading commit data...
expansion_panel.dart Loading commit data...
expansion_tile.dart Loading commit data...
expansion_tile_theme.dart Loading commit data...
feedback.dart Loading commit data...
filled_button.dart Loading commit data...
filled_button_theme.dart Loading commit data...
filter_chip.dart Loading commit data...
flexible_space_bar.dart Loading commit data...
floating_action_button.dart Loading commit data...
floating_action_button_location.dart Loading commit data...
floating_action_button_theme.dart Loading commit data...
flutter_logo.dart Loading commit data...
grid_tile.dart Loading commit data...
grid_tile_bar.dart Loading commit data...
icon_button.dart Loading commit data...
icon_button_theme.dart Loading commit data...
icons.dart Loading commit data...
ink_decoration.dart Loading commit data...
ink_highlight.dart Loading commit data...
ink_ripple.dart Loading commit data...
ink_sparkle.dart Loading commit data...
ink_splash.dart Loading commit data...
ink_well.dart Loading commit data...
input_border.dart Loading commit data...
input_chip.dart Loading commit data...
input_date_picker_form_field.dart Loading commit data...
input_decorator.dart Loading commit data...
list_tile.dart Loading commit data...
list_tile_theme.dart Loading commit data...
magnifier.dart Loading commit data...
material.dart Loading commit data...
material_button.dart Loading commit data...
material_localizations.dart Loading commit data...
material_state.dart Loading commit data...
material_state_mixin.dart Loading commit data...
menu_anchor.dart Loading commit data...
menu_bar_theme.dart Loading commit data...
menu_button_theme.dart Loading commit data...
menu_style.dart Loading commit data...
menu_theme.dart Loading commit data...
mergeable_material.dart Loading commit data...
motion.dart Loading commit data...
navigation_bar.dart Loading commit data...
navigation_bar_theme.dart Loading commit data...
navigation_drawer.dart Loading commit data...
navigation_drawer_theme.dart Loading commit data...
navigation_rail.dart Loading commit data...
navigation_rail_theme.dart Loading commit data...
no_splash.dart Loading commit data...
outlined_button.dart Loading commit data...
outlined_button_theme.dart Loading commit data...
page.dart Loading commit data...
page_transitions_theme.dart Loading commit data...
paginated_data_table.dart Loading commit data...
popup_menu.dart Loading commit data...
popup_menu_theme.dart Loading commit data...
progress_indicator.dart Loading commit data...
progress_indicator_theme.dart Loading commit data...
radio.dart Loading commit data...
radio_list_tile.dart Loading commit data...
radio_theme.dart Loading commit data...
range_slider.dart Loading commit data...
refresh_indicator.dart Loading commit data...
reorderable_list.dart Loading commit data...
scaffold.dart Loading commit data...
scrollbar.dart Loading commit data...
scrollbar_theme.dart Loading commit data...
search.dart Loading commit data...
search_anchor.dart Loading commit data...
search_bar_theme.dart Loading commit data...
search_view_theme.dart Loading commit data...
segmented_button.dart Loading commit data...
segmented_button_theme.dart Loading commit data...
selectable_text.dart Loading commit data...
selection_area.dart Loading commit data...
shadows.dart Loading commit data...
slider.dart Loading commit data...
slider_theme.dart Loading commit data...
snack_bar.dart Loading commit data...
snack_bar_theme.dart Loading commit data...
spell_check_suggestions_toolbar.dart Loading commit data...
spell_check_suggestions_toolbar_layout_delegate.dart Loading commit data...
stepper.dart Loading commit data...
switch.dart Loading commit data...
switch_list_tile.dart Loading commit data...
switch_theme.dart Loading commit data...
tab_bar_theme.dart Loading commit data...
tab_controller.dart Loading commit data...
tab_indicator.dart Loading commit data...
tabs.dart Loading commit data...
text_button.dart Loading commit data...
text_button_theme.dart Loading commit data...
text_field.dart Loading commit data...
text_form_field.dart Loading commit data...
text_selection.dart Loading commit data...
text_selection_theme.dart Loading commit data...
text_selection_toolbar.dart Loading commit data...
text_selection_toolbar_text_button.dart Loading commit data...
text_theme.dart Loading commit data...
theme.dart Loading commit data...
theme_data.dart Loading commit data...
time.dart Loading commit data...
time_picker.dart Loading commit data...
time_picker_theme.dart Loading commit data...
toggle_buttons.dart Loading commit data...
toggle_buttons_theme.dart Loading commit data...
toggleable.dart Loading commit data...
tooltip.dart Loading commit data...
tooltip_theme.dart Loading commit data...
tooltip_visibility.dart Loading commit data...
typography.dart Loading commit data...
user_accounts_drawer_header.dart Loading commit data...