Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the RangeSlider widget to the 2024 Material Design appearance #163736

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

TahaTesser
Copy link
Member

Fixes Update RangeSlider for Material 3 redesign

Description

This PR updates RangeSlider with year2023 flag which can be used to opt into the 2024 `RangeSlider design appearance.

Code Sample

expand to view the code sample
import 'package:flutter/material.dart';

void main() => runApp(const RangeSliderExampleApp());

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        sliderTheme: const SliderThemeData(
          showValueIndicator: ShowValueIndicator.always,
        ),
      ),
      home: Scaffold(
        appBar: AppBar(title: const Text('RangeSlider Sample')),
        body: const RangeSliderExample(),
      ),
    );
  }
}

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

  @override
  State<RangeSliderExample> createState() => _RangeSliderExampleState();
}

class _RangeSliderExampleState extends State<RangeSliderExample> {
  RangeValues _currentRange1Values = const RangeValues(20.0, 60.0);
  RangeValues _currentRange2Values = const RangeValues(30.0, 80.0);

  @override
  Widget build(BuildContext context) {
    return Column(
      spacing: 20.0,
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        RangeSlider(
          year2023: false,
          values: _currentRange1Values,
          max: 100,
          onChanged: (RangeValues values) {
            setState(() {
              _currentRange1Values = values;
            });
          },
        ),
        RangeSlider(
          year2023: false,
          values: _currentRange2Values,
          max: 100,
          divisions: 10,
          labels: RangeLabels(
            _currentRange2Values.start.round().toString(),
            _currentRange2Values.end.round().toString(),
          ),
          onChanged: (RangeValues values) {
            setState(() {
              _currentRange2Values = values;
            });
          },
        ),
      ],
    );
  }
}
Preview 1 Preview 2

Custom track gap and thumb size

Screenshot 2025-02-20 at 15 26 09 Screenshot 2025-02-20 at 15 27 19

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. labels Feb 20, 2025
@TahaTesser TahaTesser force-pushed the m3_range_slider_design branch 3 times, most recently from 0d2efdc to 04e0d7e Compare February 21, 2025 09:44
@TahaTesser TahaTesser marked this pull request as ready for review February 21, 2025 11:24
@TahaTesser TahaTesser requested review from QuncCccccc and removed request for matanlurey February 21, 2025 11:24
Copy link
Contributor

@QuncCccccc QuncCccccc left a comment

Choose a reason for hiding this comment

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

Thanks so much for adding the new M3 style for RangeSlider! I just did the first round review and it looks really great!

if (_active && thumbWidth != null && pressedThumbWidth != null && trackGap != null) {
delta = thumbWidth - pressedThumbWidth;
if (thumbWidth > 0.0) {
thumbWidth = pressedThumbWidth;
Copy link
Contributor

Choose a reason for hiding this comment

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

When thumbWidth is > 0, thumbWidth equals to pressedThumbWidth, but is it possible that thumbWidth is <= 0?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good question! The thumb shape actually renders the thumb with a non-negative size when passing a negative thumbWidth. For instance, passing -10 will render a thumb shape with a width of 10 pixels.

As a result, the thumbWidth > 0.0 condition actually ignores the pressed width. We don't need this condition since the thumb renders just fine with a negative width, and it should have a pressed width as well. I'll remove this condition.

@TahaTesser TahaTesser force-pushed the m3_range_slider_design branch from 04e0d7e to 3749d09 Compare March 19, 2025 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

Update RangeSlider for Material 3 redesign
2 participants