• Furkan Acar's avatar
    Add `SegmentedButton.styleFrom` (#137542) · 83ac7605
    Furkan Acar authored
    fixes https://github.com/flutter/flutter/issues/138289
    
    ---
    
    SegmentedButtom.styleFrom has been added to the segment button, so there is no longer any need to the button style from the beginning. It works like ElevatedButton.styleFrom only I added selectedForegroundColor, selectedBackgroundColor. In this way, the user will be able to change the color first without checking the MaterialState states. I added tests of the same controls.
    
    #129215 I opened this problem myself, but I was rejected because I handled too many items in a PR. For now, I wrote a structure that only handles MaterialStates instead of users.
    
    old (still avaliable)
    <img width="626" alt="image" src="https://github.com/flutter/flutter/assets/65075121/9446b13b-c355-4d20-bda2-c47a23d42d4f">
    
    new (just an option for developer)
    <img width="483" alt="image" src="https://github.com/flutter/flutter/assets/65075121/0a645257-4c83-4029-9484-bd746c02265f">
    
    ### Code sample
    
    <details>
    <summary>expand to view the code sample</summary> 
    
    ```dart
    import 'package:flutter/material.dart';
    
    /// Flutter code sample for [SegmentedButton].
    
    void main() {
      runApp(const SegmentedButtonApp());
    }
    
    enum Calendar { day, week, month, year }
    
    class SegmentedButtonApp extends StatefulWidget {
      const SegmentedButtonApp({super.key});
    
      @override
      State<SegmentedButtonApp> createState() => _SegmentedButtonAppState();
    }
    
    class _SegmentedButtonAppState extends State<SegmentedButtonApp> {
      Calendar calendarView = Calendar.day;
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          theme: ThemeData(useMaterial3: true),
          home: Scaffold(
            body: Center(
              child: SegmentedButton<Calendar>(
                style: SegmentedButton.styleFrom(
                  foregroundColor: Colors.amber,
                  visualDensity: VisualDensity.comfortable,
                ),
                // style: const ButtonStyle(
                //   foregroundColor: MaterialStatePropertyAll<Color>(Colors.deepPurple),
                //   visualDensity: VisualDensity.comfortable,
                // ),
                segments: const <ButtonSegment<Calendar>>[
                  ButtonSegment<Calendar>(
                      value: Calendar.day,
                      label: Text('Day'),
                      icon: Icon(Icons.calendar_view_day)),
                  ButtonSegment<Calendar>(
                      value: Calendar.week,
                      label: Text('Week'),
                      icon: Icon(Icons.calendar_view_week)),
                  ButtonSegment<Calendar>(
                      value: Calendar.month,
                      label: Text('Month'),
                      icon: Icon(Icons.calendar_view_month)),
                  ButtonSegment<Calendar>(
                      value: Calendar.year,
                      label: Text('Year'),
                      icon: Icon(Icons.calendar_today)),
                ],
                selected: <Calendar>{calendarView},
                onSelectionChanged: (Set<Calendar> newSelection) {
                  setState(() {
                    calendarView = newSelection.first;
                  });
                },
              ),
            ),
          ),
        );
      }
    }
    
    ```
    
    </details>
    83ac7605
Name
Last commit
Last update
..
action_chip_template.dart Loading commit data...
app_bar_template.dart Loading commit data...
badge_template.dart Loading commit data...
banner_template.dart Loading commit data...
bottom_app_bar_template.dart Loading commit data...
bottom_sheet_template.dart Loading commit data...
button_template.dart Loading commit data...
card_template.dart Loading commit data...
checkbox_template.dart Loading commit data...
chip_template.dart Loading commit data...
color_scheme_template.dart Loading commit data...
date_picker_template.dart Loading commit data...
dialog_template.dart Loading commit data...
divider_template.dart Loading commit data...
drawer_template.dart Loading commit data...
expansion_tile_template.dart Loading commit data...
fab_template.dart Loading commit data...
filter_chip_template.dart Loading commit data...
icon_button_template.dart Loading commit data...
input_chip_template.dart Loading commit data...
input_decorator_template.dart Loading commit data...
list_tile_template.dart Loading commit data...
menu_template.dart Loading commit data...
motion_template.dart Loading commit data...
navigation_bar_template.dart Loading commit data...
navigation_drawer_template.dart Loading commit data...
navigation_rail_template.dart Loading commit data...
popup_menu_template.dart Loading commit data...
progress_indicator_template.dart Loading commit data...
radio_template.dart Loading commit data...
search_bar_template.dart Loading commit data...
search_view_template.dart Loading commit data...
segmented_button_template.dart Loading commit data...
slider_template.dart Loading commit data...
snackbar_template.dart Loading commit data...
surface_tint.dart Loading commit data...
switch_template.dart Loading commit data...
tabs_template.dart Loading commit data...
template.dart Loading commit data...
text_field_template.dart Loading commit data...
time_picker_template.dart Loading commit data...
token_logger.dart Loading commit data...
typography_template.dart Loading commit data...