Skip to content

Conversation

@AcarFurkan
Copy link
Contributor

@AcarFurkan AcarFurkan commented Oct 30, 2023

fixes #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)
image

new (just an option for developer)
image

Code sample

expand to view the code sample
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;
              });
            },
          ),
        ),
      ),
    );
  }
}

Pre-launch Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [Tree Hygiene] wiki page, which explains my responsibilities.
  • I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement].
  • I signed the [CLA].
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is [test-exempt].
  • All existing and new tests are passing.

@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. labels Oct 30, 2023
@AcarFurkan
Copy link
Contributor Author

hi, @TahaTesser #137430 in this pr you gave me suggestions. l followed them. but l took errors again. l could not understand sorry.

@TahaTesser TahaTesser self-requested a review November 8, 2023 16:05
@TahaTesser
Copy link
Member

it seems there are a bunch of errors in this PR. Can you please take a look at the errors and fix them?

See
https://logs.chromium.org/logs/flutter/buildbucket/cr-buildbucket/8765795303426198961/+/u/Analyze/analyze/test_stdout

@AcarFurkan
Copy link
Contributor Author

Hi, @TahaTesser probably mission completed :)

@TahaTesser
Copy link
Member

@AcarFurkan
Great job up updating the PR. Provided some reviews.

@TahaTesser
Copy link
Member

Can you please link an issue this PR fixes? If it doesn't exist, please file an issue and link it in the PR description.

@AcarFurkan
Copy link
Contributor Author

#138289 This is a feature request and I'm adding it.

@TahaTesser
Copy link
Member

Looks like current tests are just duplications and not necessarily testing styleFrom itself. Removing these tests

@AcarFurkan
Copy link
Contributor Author

You asked me for a test for this in another thread, so I added all the tests.

@TahaTesser
Copy link
Member

TahaTesser commented Nov 14, 2023

You asked me for a test for this in another thread, so I added all the tests.

Since this is one of your first PRs. I'm writing tests for you. i will comment it soon.

@AcarFurkan
Copy link
Contributor Author

Oh thank you so much, I can help you if you give me a clue.

Copy link
Member

@TahaTesser TahaTesser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With my suggestions applied and after adding SegmentedButton.styleFrom specific test this looks ready
LGTM!

@AcarFurkan
Copy link
Contributor Author

AcarFurkan commented Nov 14, 2023

@TahaTesser This is my first contribution to flutter, what do you think? And thank you so much for your concern :)

Copy link
Contributor

@HansMuller HansMuller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't had a chance to review this carefully yet but at first blush it looks good. Can you include the example (and an example test)? The calendar example you've provided should work although you might want to configure a pair of of foo/disabledFoo properties just to amplify the idea.

@AcarFurkan
Copy link
Contributor Author

hi @HansMuller, I'm sorry I didn't understand exactly what you wanted. I added such an example.
https://github.com/AcarFurkan/segmented_button_example and
I added almost the same test as "SegmentedButton.styleFrom is applied to the SegmentedButton" l just added
multiSelectionEnabled: true

@TahaTesser
Copy link
Member

@AcarFurkan
Is there a reason you added add test for multi selection?

It doesn't look necessary. The test added is sufficient to test SegmentedButton.styleFrom

@AcarFurkan
Copy link
Contributor Author

AcarFurkan commented Nov 16, 2023

hans said "Can you include the example (and an example test)?" l don't understand actually what he wants. if you want l can remove

@AcarFurkan
Copy link
Contributor Author

hi @TahaTesser if you understand, can you explain what he wants? l added an example is it enough

@TahaTesser
Copy link
Member

TahaTesser commented Nov 16, 2023

hans said "Can you include the example (and an example test)?" l don't understand actually what he wants.

Feel free to ask. The test you added is a duplication of the existing, without any relevance.

Hans asked if you can add example and example test (not the widget test).
The examples and their tests are located here https://github.com/flutter/flutter/tree/master/examples/api

@AcarFurkan
Copy link
Contributor Author

ah thank you but l can not call styleFrom method here because this file
import 'package:flutter/material.dart';
path:
/Users/furkanacar/flutter-projects/remotef/flutter/examples/api/lib/material/segmented_button/

image

@TahaTesser
Copy link
Member

You should be able to use SegmentedButton.styleFrom locally when you clone your forked Flutter repository.
Make sure to execute flutter pub get in the examples/api app.

@AcarFurkan
Copy link
Contributor Author

awesome let's wait then @TahaTesser thanks for everything :)

Copy link
Contributor

@HansMuller HansMuller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good, just some minor feedback.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this have been written in terms of ButtonStyle.styleFrom()? If there are any differences you could write something like return ButtonStyle.styleFrom(...).copyWith(...)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AcarFurkan seems to be lost so I've addressed this feedback.

Added TextButton.styleFrom with .copyWith for background, foreground, and overlay color.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's a good idea to duplicate aspects of _M3Defaults here; we want this stuff to change automatically when we regenerate _M3Defaults. Adding methods to the _M3Defaults template is OK. When you do be sure to explain the use cases in comments.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea. I added a static function in _M3Defaults which uses the tokens to return color with opacity in the selected and unselected states.

@TahaTesser
Copy link
Member

TahaTesser commented Dec 18, 2023

@AcarFurkan
Please address the feedback from Hans and reply to the comments, mention if you addressed the provided feedback.

@AcarFurkan
Copy link
Contributor Author

I solved 4 of them, but I couldn't understand the remaining 2. @HansMuller If you have the time, can you give some details for the 2 that I can't figure out?

@AcarFurkan
Copy link
Contributor Author

@TahaTesser @HansMuller hii, I know you are very busy and I am sorry to bother you, but there are two feedbacks that I cannot solve, if you can explain them, I will solve them. I would be very happy if you get back to me when you are available. thank you :)

AcarFurkan and others added 2 commits January 2, 2024 12:20
Revert "added SegmentedButton.stylefrom to SegmentedButton"

This reverts commit 34b8696ad759bef8ab577b9751e86b4f342c3acf.

reverted commit and fix string problems

Refactor: deleting custom deprecated messages

Update packages/flutter/lib/src/material/segmented_button.dart

update documentation

Co-authored-by: Taha Tesser <[email protected]>

Update packages/flutter/lib/src/material/segmented_button.dart

remove unnecessary explanations from the documentation

Co-authored-by: Taha Tesser <[email protected]>

Update packages/flutter/lib/src/material/segmented_button.dart

Co-authored-by: Taha Tesser <[email protected]>

Update packages/flutter/lib/src/material/segmented_button.dart

update _SegmentedButtonDefaultElevation to MaterialStatePropertyAll<double>

Co-authored-by: Taha Tesser <[email protected]>

Update packages/flutter/lib/src/material/segmented_button.dart

delete unnecessary empty lines

Co-authored-by: Taha Tesser <[email protected]>

Update packages/flutter/lib/src/material/segmented_button.dart

Co-authored-by: Taha Tesser <[email protected]>

Refactor: changing overlay opacity values according to m3

Update packages/flutter/lib/src/material/segmented_button.dart

Update packages/flutter/test/material/segmented_button_theme_test.dart

Update packages/flutter/test/material/segmented_button_theme_test.dart

Update packages/flutter/test/material/segmented_button_theme_test.dart

Update packages/flutter/test/material/segmented_button_theme_test.dart

Update packages/flutter/lib/src/material/segmented_button.dart

Update packages/flutter/lib/src/material/segmented_button.dart

Update packages/flutter/lib/src/material/segmented_button.dart

Update packages/flutter/lib/src/material/segmented_button.dart

Add test

add test for multi selection

Update packages/flutter/test/material/segmented_button_test.dart

style added to segmented_button example and added test to segmented button

added license header

buttonStyle instance moved to outside of button widget

moving example and test to right file

Refactor: minimize example

Update packages/flutter/lib/src/material/segmented_button.dart

Co-authored-by: Taha Tesser <[email protected]>

Update examples/api/lib/material/segmented_button/segmented_button.1.dart

Co-authored-by: Taha Tesser <[email protected]>

Update examples/api/lib/material/segmented_button/segmented_button.1.dart

Co-authored-by: Taha Tesser <[email protected]>

Fix example and update test

Fix documentation

Update examples/api/lib/material/segmented_button/segmented_button.1.dart

Co-authored-by: Hans Muller <[email protected]>

Refactor: adding tool snippet

Refactor: example link added

Refactor: shortened the example

Refactor: ButtonStyle updated to ButtonStyle.styleFrom

Refactor: Buttonstyle.from json to Buttonstyle
Address comments
@TahaTesser
Copy link
Member

@HansMuller Please take a look the address review commit c690ee1

@HansMuller
Copy link
Contributor

@HansMuller Please take a look the address review commit c690ee1

Your changes LGTM. This PR appears to be ready to land.

Copy link
Member

@TahaTesser TahaTesser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@HansMuller HansMuller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@HansMuller HansMuller added the autosubmit Merge PR when tree becomes green via auto submit App label Jan 3, 2024
@auto-submit auto-submit bot merged commit 83ac760 into flutter:master Jan 3, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jan 3, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jan 5, 2024
tarrinneal added a commit to flutter/packages that referenced this pull request Jan 5, 2024
Manual roll Flutter from 11def8e to cc40425 (118 revisions)

Manual roll requested by [email protected]

flutter/flutter@11def8e...cc40425

2024-01-05 [email protected] Roll Flutter Engine from
0bbb4d61ce82 to f60d9a9a3395 (1 revision) (flutter/flutter#140993)
2024-01-04 [email protected] Roll Flutter Engine from
b2a9ce88a19e to 0bbb4d61ce82 (3 revisions) (flutter/flutter#140990)
2024-01-04 [email protected] [web] Fix and unskip a few more CanvasKit
tests (flutter/flutter#140821)
2024-01-04 [email protected] Roll Flutter Engine from
bd175aa5e0b6 to b2a9ce88a19e (1 revision) (flutter/flutter#140986)
2024-01-04 [email protected] Pin package:vm_service
(flutter/flutter#140972)
2024-01-04 [email protected] Add scrollbar
for menus (flutter/flutter#140941)
2024-01-04 [email protected] manual pub roll to pick up dds
fixes (flutter/flutter#140979)
2024-01-04 [email protected] Roll Flutter Engine from
b81023eb71c9 to bd175aa5e0b6 (2 revisions) (flutter/flutter#140980)
2024-01-04 [email protected] Temporarily remove env variable for leak
tracking bots. (flutter/flutter#140978)
2024-01-04 [email protected] Add Flutter CI status to README
(flutter/flutter#140513)
2024-01-04 [email protected] Reland "integrate testWidgets with leak
tracking" (#140521) (flutter/flutter#140928)
2024-01-04 [email protected] Run half of iOS
devicelab tests with Xcode 15 (flutter/flutter#140927)
2024-01-04 [email protected] Fix
`SegmentedButton` states update logic (flutter/flutter#140772)
2024-01-04 [email protected] Roll Flutter Engine from
f539acfb8c5a to b81023eb71c9 (1 revision) (flutter/flutter#140973)
2024-01-04 [email protected] [Fix] Consistency in
ButtonStyleButton related Tests (flutter/flutter#140610)
2024-01-04 [email protected] Roll Packages from
bbb4134 to 31fc7b5 (6 revisions) (flutter/flutter#140967)
2024-01-04 [email protected] Fix local engine use in macOS plugins
(flutter/flutter#140222)
2024-01-04 [email protected] Roll Flutter Engine from
7d5a120a601b to f539acfb8c5a (2 revisions) (flutter/flutter#140959)
2024-01-04 [email protected] Roll Flutter Engine from
1ff3cb885842 to 7d5a120a601b (1 revision) (flutter/flutter#140946)
2024-01-04 [email protected] Roll Flutter Engine from
c8bf51f0d4cd to 1ff3cb885842 (1 revision) (flutter/flutter#140943)
2024-01-04 [email protected] Roll Flutter Engine from
bfd2d8a100ec to c8bf51f0d4cd (2 revisions) (flutter/flutter#140939)
2024-01-04 [email protected] Roll Flutter Engine from
28ae9e35c331 to bfd2d8a100ec (1 revision) (flutter/flutter#140937)
2024-01-04 [email protected] Roll Flutter Engine from
ab4098c742f8 to 28ae9e35c331 (1 revision) (flutter/flutter#140936)
2024-01-04 [email protected] Roll Flutter Engine from
e169f3677008 to ab4098c742f8 (2 revisions) (flutter/flutter#140933)
2024-01-03 [email protected] Roll Flutter Engine from
7c2adb811059 to e169f3677008 (1 revision) (flutter/flutter#140929)
2024-01-03 [email protected] Remove deprecated bitcode stripping from
tooling (flutter/flutter#140903)
2024-01-03 [email protected] Add Windows leak
tracking targets (flutter/flutter#140423)
2024-01-03 [email protected] [github actions] refactor and fix
cherry pick actions (flutter/flutter#140499)
2024-01-03 [email protected] Migrate Xcode projects last version checks
to Xcode 15.1 (flutter/flutter#140256)
2024-01-03 [email protected] Roll Flutter Engine from
bf232c4da241 to 7c2adb811059 (3 revisions) (flutter/flutter#140920)
2024-01-03 [email protected] fix typo and reflow
(flutter/flutter#140925)
2024-01-03 98614782+auto-submit[bot]@users.noreply.github.com Reverts
"Re-land integrate testWidgets with leak tracking."
(flutter/flutter#140926)
2024-01-03 [email protected] Roll Flutter Engine from
bf979d220283 to bf232c4da241 (1 revision) (flutter/flutter#140915)
2024-01-03 [email protected] Changes the regular cursor to a
floating cursor when a long press occurs. (flutter/flutter#138479)
2024-01-03 [email protected] Add
`SegmentedButton.styleFrom` (flutter/flutter#137542)
2024-01-03 [email protected] Roll Flutter Engine from
c62bcff5b809 to bf979d220283 (1 revision) (flutter/flutter#140910)
2024-01-03 [email protected] Re-land integrate testWidgets with leak
tracking. (flutter/flutter#140521)
2024-01-03 [email protected] Roll Flutter Engine from
98b72c7ffe71 to c62bcff5b809 (2 revisions) (flutter/flutter#140905)
2024-01-03 [email protected] [flutter_tools] add support for
--enable-impeller to test device. (flutter/flutter#140899)
2024-01-03 [email protected] Roll Flutter Engine from
cf7536964a2f to 98b72c7ffe71 (1 revision) (flutter/flutter#140897)
2024-01-03 [email protected] Handle
KEYCODE_DPAD_CENTER and KEYCODE_ENTER (flutter/flutter#140808)
2024-01-03 [email protected] Add Lucas Saudon to AUTHORS
(flutter/flutter#139965)
2024-01-03 [email protected] Link to wiki page
about updating dependencies in each `pubspec.yaml` file
(flutter/flutter#140826)
2024-01-03 [email protected] Marks Linux_pixel_7pro
native_assets_android to be unflaky (flutter/flutter#140866)
...

---------

Co-authored-by: Tarrin Neal <[email protected]>
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App d: api docs Issues with https://api.flutter.dev/ d: examples Sample code and demos f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SegmentedButton.stylefrom feature for SegmentedButton

3 participants