Skip to content

SegmentedButton doesn't clip properly when one of the segments has ColorFiltered #144990

@dannygmr

Description

@dannygmr

Steps to reproduce

Segmented button with:

  emptySelectionAllowed: true
  showSelectedIcon: false

initialize with a ButtonSegment that has an SVG icon. (used flutter_svg: ^2.0.10+1)
when onSelectionChanged is called, change the icon color for the selection using a ColorFiltered.

Expected results

Icon color will change according to selection and what's defined, and segmented button will render correctly.

Actual results

Icon colors change, but the rightmost segment is not clipped correctly, resulting in a stadium border with a rectangular selected background.

Code sample

Code sample
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';

/// Flutter code sample for [SegmentedButton].

void main() {
  runApp(const SegmentedButtonApp());
}

class SegmentedButtonApp extends StatelessWidget {
  const SegmentedButtonApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(useMaterial3: true),
      home: const Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Spacer(),
              MultipleChoice(),
              Spacer(),
            ],
          ),
        ),
      ),
    );
  }
}

enum Sizes {
  extraSmall,
  small,
}

class SvgPath {
  static const String heat = "assets/svg/circle-heat-svgrepo-com.svg";
  static const String cloudArrow =
      "assets/svg/cloud-arrow-down-svgrepo-com.svg";
}

class MultipleChoice extends StatefulWidget {
  const MultipleChoice({super.key});

  @override
  State<MultipleChoice> createState() => _MultipleChoiceState();
}

class _MultipleChoiceState extends State<MultipleChoice> {
  Set<Sizes> selection = {};
  var segments = <ButtonSegment<Sizes>>[
    ButtonSegment<Sizes>(
        value: Sizes.extraSmall, icon: getSvgFromPath(SvgPath.cloudArrow)),
    ButtonSegment<Sizes>(
        value: Sizes.small, icon: getSvgFromPath(SvgPath.heat)),
  ];

  @override
  Widget build(BuildContext context) {
    return SegmentedButton<Sizes>(
      style: SegmentedButton.styleFrom(splashFactory: NoSplash.splashFactory),
      segments: segments,
      selected: selection,
      emptySelectionAllowed: true,
      showSelectedIcon: false,
      onSelectionChanged: (Set<Sizes> newSelection) {
        final selectedSegments = segments.map((e) {
          if (newSelection.contains(e.value)) {
            return ButtonSegment<Sizes>(
              value: e.value,
              icon: ColorFiltered(
                colorFilter:
                    const ColorFilter.mode(Colors.red, BlendMode.srcIn),
                child: e.icon,
              ),
            );
          } else {
            return ButtonSegment<Sizes>(
              value: e.value,
              icon: ColorFiltered(
                colorFilter:
                    const ColorFilter.mode(Colors.black, BlendMode.srcIn),
                child: e.icon,
              ),
            );
          }
        }).toList();
        setState(() {
          selection = newSelection;
          segments = selectedSegments;
        });
      },
      multiSelectionEnabled: true,
    );
  }
}

Widget getSvgFromPath(String path, [Color color = Colors.black]) {
  return SvgPicture.asset(
    path,
    colorFilter: ColorFilter.mode(
      color,
      BlendMode.srcIn,
    ),
  );
}

Screenshots or Video

Note the rightmost corner:

Screenshots / Video demonstration

segmented_demo

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.19.3, on macOS 14.3 23D56 darwin-arm64, locale en-IL)
    • Flutter version 3.19.3 on channel stable at /Users/gamerd/SDK/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ba39319843 (5 days ago), 2024-03-07 15:22:21 -0600
    • Engine revision 2e4ba9c6fb
    • Dart version 3.3.1
    • DevTools version 2.31.1
    • Pub download mirror https://artifactory.scrdairy.com/artifactory/api/pub/pub-virtual

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/gamerd/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15E204a
    • CocoaPods version 1.14.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)

[✓] VS Code (version 1.87.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.84.0

[✓] Connected device (3 available)
    • iPhone 15 (mobile) • 438DBD07-A66E-489B-B2D4-99689DE8F9D5 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-17-2 (simulator)
    • macOS (desktop)    • macos                                • darwin-arm64   • macOS 14.3 23D56 darwin-arm64
    • Chrome (web)       • chrome                               • web-javascript • Google Chrome 122.0.6261.112
    ! Error: Browsing on the local area network for iPhone 12. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)

[✓] Network resources
    • All expected network resources are available.```

</details>

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work lista: imagesLoading, displaying, rendering imagesf: material designflutter/packages/flutter/material repository.found in release: 3.19Found to occur in 3.19found in release: 3.21Found to occur in 3.21frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onteam-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

Type

No type

Projects

Status

Done (PR merged)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions