• 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
..
about_test.dart Loading commit data...
action_chip_test.dart Loading commit data...
action_icons_theme_test.dart Loading commit data...
adaptive_text_selection_toolbar_test.dart Loading commit data...
animated_icons_test.dart Loading commit data...
app_bar_test.dart Loading commit data...
app_bar_theme_test.dart Loading commit data...
app_builder_test.dart Loading commit data...
app_test.dart Loading commit data...
arc_test.dart Loading commit data...
autocomplete_test.dart Loading commit data...
back_button_test.dart Loading commit data...
badge_test.dart Loading commit data...
badge_theme_test.dart Loading commit data...
banner_test.dart Loading commit data...
banner_theme_test.dart Loading commit data...
bottom_app_bar_test.dart Loading commit data...
bottom_app_bar_theme_test.dart Loading commit data...
bottom_navigation_bar_test.dart Loading commit data...
bottom_navigation_bar_theme_test.dart Loading commit data...
bottom_sheet_test.dart Loading commit data...
bottom_sheet_theme_test.dart Loading commit data...
button_bar_test.dart Loading commit data...
button_bar_theme_test.dart Loading commit data...
button_style_test.dart Loading commit data...
button_theme_test.dart Loading commit data...
calendar_date_picker_test.dart Loading commit data...
card_test.dart Loading commit data...
card_theme_test.dart Loading commit data...
checkbox_list_tile_test.dart Loading commit data...
checkbox_test.dart Loading commit data...
checkbox_theme_test.dart Loading commit data...
chip_test.dart Loading commit data...
chip_theme_test.dart Loading commit data...
choice_chip_test.dart Loading commit data...
circle_avatar_test.dart Loading commit data...
color_scheme_test.dart Loading commit data...
colors_test.dart Loading commit data...
data_table_test.dart Loading commit data...
data_table_test_utils.dart Loading commit data...
data_table_theme_test.dart Loading commit data...
date_picker_test.dart Loading commit data...
date_picker_theme_test.dart Loading commit data...
date_range_picker_test.dart Loading commit data...
debug_test.dart Loading commit data...
desktop_text_selection_toolbar_button_test.dart Loading commit data...
desktop_text_selection_toolbar_test.dart Loading commit data...
dialog_test.dart Loading commit data...
dialog_theme_test.dart Loading commit data...
divider_test.dart Loading commit data...
divider_theme_test.dart Loading commit data...
drawer_button_test.dart Loading commit data...
drawer_test.dart Loading commit data...
drawer_theme_test.dart Loading commit data...
dropdown_form_field_test.dart Loading commit data...
dropdown_menu_test.dart Loading commit data...
dropdown_menu_theme_test.dart Loading commit data...
dropdown_test.dart Loading commit data...
elevated_button_test.dart Loading commit data...
elevated_button_theme_test.dart Loading commit data...
elevation_overlay_test.dart Loading commit data...
expand_icon_test.dart Loading commit data...
expansion_panel_test.dart Loading commit data...
expansion_tile_test.dart Loading commit data...
expansion_tile_theme_test.dart Loading commit data...
feedback_test.dart Loading commit data...
feedback_tester.dart Loading commit data...
filled_button_test.dart Loading commit data...
filled_button_theme_test.dart Loading commit data...
filter_chip_test.dart Loading commit data...
flexible_space_bar_collapse_mode_test.dart Loading commit data...
flexible_space_bar_stretch_mode_test.dart Loading commit data...
flexible_space_bar_test.dart Loading commit data...
floating_action_button_location_test.dart Loading commit data...
floating_action_button_test.dart Loading commit data...
floating_action_button_theme_test.dart Loading commit data...
flutter_logo_test.dart Loading commit data...
grid_title_test.dart Loading commit data...
icon_button_test.dart Loading commit data...
icon_button_theme_test.dart Loading commit data...
icons_test.dart Loading commit data...
inherited_theme_test.dart Loading commit data...
ink_paint_test.dart Loading commit data...
ink_sparkle_test.dart Loading commit data...
ink_splash_test.dart Loading commit data...
ink_well_test.dart Loading commit data...
input_chip_test.dart Loading commit data...
input_date_picker_form_field_test.dart Loading commit data...
input_decorator_test.dart Loading commit data...
list_tile_test.dart Loading commit data...
list_tile_theme_test.dart Loading commit data...
localizations_test.dart Loading commit data...
magnifier_test.dart Loading commit data...
material_button_test.dart Loading commit data...
material_state_mixin_test.dart Loading commit data...
material_state_property_test.dart Loading commit data...
material_states_controller_test.dart Loading commit data...
material_test.dart Loading commit data...
menu_anchor_test.dart Loading commit data...
menu_bar_theme_test.dart Loading commit data...
menu_button_theme_test.dart Loading commit data...
menu_style_test.dart Loading commit data...
menu_theme_test.dart Loading commit data...
mergeable_material_test.dart Loading commit data...
navigation_bar_test.dart Loading commit data...
navigation_bar_theme_test.dart Loading commit data...
navigation_drawer_test.dart Loading commit data...
navigation_drawer_theme_test.dart Loading commit data...
navigation_rail_test.dart Loading commit data...
navigation_rail_theme_test.dart Loading commit data...
outlined_button_test.dart Loading commit data...
outlined_button_theme_test.dart Loading commit data...
page_selector_test.dart Loading commit data...
page_test.dart Loading commit data...
page_transitions_theme_test.dart Loading commit data...
paginated_data_table_test.dart Loading commit data...
persistent_bottom_sheet_test.dart Loading commit data...
popup_menu_test.dart Loading commit data...
popup_menu_theme_test.dart Loading commit data...
progress_indicator_test.dart Loading commit data...
progress_indicator_theme_test.dart Loading commit data...
radio_list_tile_test.dart Loading commit data...
radio_test.dart Loading commit data...
radio_theme_test.dart Loading commit data...
range_slider_test.dart Loading commit data...
raw_material_button_test.dart Loading commit data...
refresh_indicator_test.dart Loading commit data...
reorderable_list_test.dart Loading commit data...
scaffold_test.dart Loading commit data...
scrollbar_paint_test.dart Loading commit data...
scrollbar_test.dart Loading commit data...
scrollbar_theme_test.dart Loading commit data...
search_anchor_test.dart Loading commit data...
search_bar_theme_test.dart Loading commit data...
search_test.dart Loading commit data...
search_view_theme_test.dart Loading commit data...
segmented_button_test.dart Loading commit data...
segmented_button_theme_test.dart Loading commit data...
selection_area_test.dart Loading commit data...
slider_test.dart Loading commit data...
slider_theme_test.dart Loading commit data...
snack_bar_test.dart Loading commit data...
snack_bar_theme_test.dart Loading commit data...
spell_check_suggestions_toolbar_layout_delegate_test.dart Loading commit data...
spell_check_suggestions_toolbar_test.dart Loading commit data...
stepper_test.dart Loading commit data...
switch_list_tile_test.dart Loading commit data...
switch_test.dart Loading commit data...
switch_theme_test.dart Loading commit data...
tab_bar_theme_test.dart Loading commit data...
tabbed_scrollview_warp_test.dart Loading commit data...
tabs_test.dart Loading commit data...
text_button_test.dart Loading commit data...
text_button_theme_test.dart Loading commit data...
text_field_focus_test.dart Loading commit data...
text_field_helper_text_test.dart Loading commit data...
text_field_restoration_test.dart Loading commit data...
text_field_splash_test.dart Loading commit data...
text_field_test.dart Loading commit data...
text_form_field_restoration_test.dart Loading commit data...
text_form_field_test.dart Loading commit data...
text_selection_test.dart Loading commit data...
text_selection_theme_test.dart Loading commit data...
text_selection_toolbar_test.dart Loading commit data...
text_selection_toolbar_text_button_test.dart Loading commit data...
text_theme_test.dart Loading commit data...
theme_data_test.dart Loading commit data...
theme_defaults_test.dart Loading commit data...
theme_test.dart Loading commit data...
time_picker_test.dart Loading commit data...
time_picker_theme_test.dart Loading commit data...
time_test.dart Loading commit data...
toggle_buttons_test.dart Loading commit data...
toggle_buttons_theme_test.dart Loading commit data...
tooltip_test.dart Loading commit data...
tooltip_theme_test.dart Loading commit data...
tooltip_visibility_test.dart Loading commit data...
typography_test.dart Loading commit data...
user_accounts_drawer_header_test.dart Loading commit data...
value_indicating_slider_test.dart Loading commit data...
will_pop_test.dart Loading commit data...