Giter Club home page Giter Club logo

another_xlider's Introduction

another_xlider

ko-fi

image

(Flutter Slider) A material design slider and range slider, horizontal and vertical, with rtl support and lots of options and customizations for flutter

IMPORTANT: Breaking Changes!

  • Flutter Version 2.x - Take any version up to 1.1.1
  • Flutter Version 3.x - Take any version from 1.1.2

Get Started

Single Slider

A single slider

FlutterSlider(
  values: [300],
  max: 500,
  min: 0,
  onDragging: (handlerIndex, lowerValue, upperValue) {
    _lowerValue = lowerValue;
    _upperValue = upperValue;
    setState(() {});
  },
)

To make slider Right To Left use rtl: true

 FlutterSlider(
  ...
  rtl: true,
  ...
)

Range Slider

A simple example of range slider

FlutterSlider(
  values: [30, 420],
  rangeSlider: true,
  max: 500,
  min: 0,
  onDragging: (handlerIndex, lowerValue, upperValue) {
    _lowerValue = lowerValue;
    _upperValue = upperValue;
    setState(() {});
  },
)

Vertical Axis

You can change the axis of your slider by setting axis to Axis.vertical. Default is horizontal

FlutterSlider(
  ...
  axis: Axis.vertical,
  ...
)

Handlers

You can customize handlers using handler and rightHandler properties.
Both handler and rightHandler accept FlutterSliderHandler class which has following properties:

  1. child: is a widget
  2. disabled: to disable the handler
  3. decoration, foregroundDecoration and transform are come from Container() widget
FlutterSlider(
  ...
  handler: FlutterSliderHandler(
    decoration: BoxDecoration(),
    child: Material(
      type: MaterialType.canvas,
      color: Colors.orange,
      elevation: 3,
      child: Container(
          padding: EdgeInsets.all(5),
          child: Icon(Icons.adjust, size: 25,)),
    ),
  ),
  rightHandler: FlutterSliderHandler(
    child: Icon(Icons.chevron_left, color: Colors.red, size: 24,),
  ),
  ...
)

Handler Scale Animation

You can control the scale animation type of your handlers, it's duration and it's scale size using handlerAnimation
handlerAnimation accepts a FlutterSliderHandlerAnimation class which has 4 properties as following

FlutterSlider(
  ...
    handlerAnimation: FlutterSliderHandlerAnimation(
      curve: Curves.elasticOut,
      reverseCurve: Curves.bounceIn,
      duration: Duration(milliseconds: 500),
      scale: 1.5
    ),
  ...
)

if you don't want scale animation, then just pass 1 to scale property
if you don't want reverseCurve, just ignore it. default is null

Trackbars

To customize track bars you can use FlutterSliderTrackBar. You can see the details here

FlutterSlider(
  ...
    trackBar: FlutterSliderTrackBar(
      activeTrackBarHeight: 5,
    ),
  ...
)

inactiveTrackBarColor and activeTrackBarColor properties are removed. use inactiveTrackBar and activeTrackBar instead.

FlutterSlider(
  ...
    trackBar: FlutterSliderTrackBar(
      inactiveTrackBar: BoxDecoration(
        borderRadius: BorderRadius.circular(20),
        color: Colors.black12,
        border: Border.all(width: 3, color: Colors.blue),
      ),
      activeTrackBar: BoxDecoration(
        borderRadius: BorderRadius.circular(4),
        color: Colors.blue.withOpacity(0.5)
      ),
    ),
  ...
)

Central Widget

If you want to have a widget in the middle of your slider, you can use centralWidget

FlutterSlider(
  ...
    trackBar: FlutterSliderTrackBar(
        centralWidget: Container(
          decoration: BoxDecoration(
              color: trackBarColor,
            borderRadius: BorderRadius.circular(50)
          ),
          width: 9,
          height: 9,
        ),
    ),
  ...
)

Tooltips

In order to customize your tooltips, you can use FlutterSliderTooltip class. You can see all properties here

FlutterSlider(
  ...
  tooltip: FlutterSliderTooltip(
    textStyle: TextStyle(fontSize: 17, color: Colors.white),
    boxStyle: FlutterSliderTooltipBox(
      decoration: BoxDecoration(
        color: Colors.redAccent.withOpacity(0.7)
      )
    )
  ),
  ...
)

Here there is a range slider with customized handlers, trackbars and tooltips

Tooltip Prefix

You can use leftPrefix, leftSuffix, rightPrefix, rightSuffix to add your desired widget around tooltip content.

FlutterSlider(
  ...
    tooltip: FlutterSliderTooltip(
      leftPrefix: Icon(Icons.attach_money, size: 19, color: Colors.black45,),
      rightSuffix: Icon(Icons.attach_money, size: 19, color: Colors.black45,),
    ),
  ...
)

Tooltip Format

If you want to change the format of the value of tooltip you can use format method.

FlutterSlider(
  ...
    tooltip: FlutterSliderTooltip(
      format: (String value) {
        return value + ' ! ';
      }
    ),
  ...
)

Tooltip Callback

If you want to fully change tooltip widget and use your own customized widget, you can use custom function.

FlutterSlider(
  ...
    tooltip: FlutterSliderTooltip(
      custom: (value) {
        return Text(value.toString());
      }
    ),
  ...
)

Disable Tooltip

To disable tooltips, use disabled in FlutterSliderTooltip class

FlutterSlider(
  ...
    tooltip: FlutterSliderTooltip(
      disabled: true,
    ),
  ...
)

Tooltip Direction

To change the direction of the tooltip, you can use direction

FlutterSlider(
  ...
    tooltip: FlutterSliderTooltip(
      direction: FlutterSliderTooltipDirection.right,
    ),
  ...
)

Tooltip Position Offset

By default tooltip alignment is center, but you can modify it's top, left, right and bottom by using positionOffset

FlutterSlider(
  ...
    tooltip: FlutterSliderTooltip(
      positionOffset: FlutterSliderTooltipPositionOffset(
        top: -100
      ),
    ),
  ...
)

Always Show Tooltips

Tooltips always displayed if this property is set to true.

FlutterSlider(
  ...
  tooltip: FlutterSliderTooltip(
    alwaysShowTooltip: true,
  ),
  ...
)

Controls

Handlers width and height

By default both handlers size are 35 width and height, but you can change this by handlerWidth and handlerHeight

FlutterSlider(
  ...
  handlerWidth: 30,
  handlerHeight: 30,
  ...
)

Select By Tap

You can tap on the slider to select it's value.
if slider is range-slider, then the closest handler to the selected point will move to that point

FlutterSlider(
  ...
  selectByTap: true, // default is true
  ...
)

if you want to move your handlers by touching and moving active TrackBar, you have to set this to false

Jump

By default slider handlers move fluently, if you set jump to true, handlers will jump between intervals

FlutterSlider(
  ...
  jump: true,
  ...
)

Step

The amount the slider changes on movement can be set using step option

FlutterSlider(
  ...
  step: FlutterSliderStep(step: 1),
  ...
)

Range Step

If you want to have a non-linear slider with different steps, simply use rangeStep feature.

FlutterSlider(
  min: 0,
  max: 1000000,
  ...
  step: FlutterSliderStep(
    step: 1, // default
    isPercentRange: true, // ranges are percents, 0% to 20% and so on... . default is true
    rangeList: [
      FlutterSliderRangeStep(from: 0, to: 20, step: 10000),
      FlutterSliderRangeStep(from: 20, to: 100, step: 200000),
    ]
  ),
  ...
)

Ignore Steps

If your configurations requires that some steps are not available, you can use ignoreSteps property.
this property accepts a simple class to define from and to ranges.

FlutterSlider(
  ...
    ignoreSteps: [
      FlutterSliderIgnoreSteps(from: 8000, to: 12000),
      FlutterSliderIgnoreSteps(from: 18000, to: 22000),
    ],
  ...
)

Fixed Values

If you want to have an array of fixed items and slide through it, you can use fixedValues property. use FlutterSliderFixedValue to add your fixed values.
FlutterSliderFixedValue has following properties:

  1. percent: (int) ( between 0..100 inclusive). the position of fixed item
  2. value: (dynamic) the value of fixed item
  • when using fixedValues, values of values property, must be within 0..100
FlutterSlider(
  ...
    values: [ 10, 50 ],
    fixedValues: [
      FlutterSliderFixedValue(percent: 0, value: "1000"),
      FlutterSliderFixedValue(percent: 10, value: "10K"),
      FlutterSliderFixedValue(percent: 50, value: 50000),
      FlutterSliderFixedValue(percent: 80, value: "80M"),
      FlutterSliderFixedValue(percent: 100, value: "100B"),
    ],
  ...
)

using above example, you get (string) 10K as upperValue or lowerValue (depends on handler), when you reach to 10 percent of the slider, you get (int) 50000 when you reach 50 percent of the slider and so on...

when using fixedValues, min and max are ignored

Minimum Distance

When using range slider, the minimum distance between two handlers can be defined using minimumDistance option

FlutterSlider(
  ...
    minimumDistance: 300,
  ...
)

Maximum Distance

This is the opposite of minimum distance, when using range slider, the maximum distance between two handlers can be defined using maximumDistance option

FlutterSlider(
  ...
    maximumDistance: 300,
  ...
)

Locked Handlers

If you want to lock your handlers by a certain value, you can use lockHandlers and lockDistance properties

FlutterSlider(
  ...
    lockHandlers: true,
    lockDistance: 50,
  ...
)

Hatch Mark

You can display a Hatch Mark underneath or beside of your slider based on axis. In order to display hatch mark you must
use FlutterSliderHatchMark class which has following properties:

  1. linesDistanceFromTrackBar: The distance of lines from slider. can be negative

  2. bigLine: The widget of big lines in hatch mark

  3. smallLine: The widget of small lines in hatch mark

  4. linesAlignment: the direct of lines, right or left which works as top or bottom on horizontal slider

  5. density: The number of lines per percent. 1 is default. any number less or more than 1 will decrease and increase lines respectively

  6. smallDensity: The number of small lines between any two big lines. 4 is default. any number less or more than 4 will decrease and increase lines respectively.

  7. displayLines: to display lines. by default is false for the sake of optimization

  8. labels: If you want to display some label or text at certain percent in your hatch mark, you can use labels

  9. labelBox: The widget of label box, however, you can define a widget for each label and have it's own style

  10. labelsDistanceFromTrackBar: The distance of labels from slider. can be negative

  11. disabled: to disabled the whole hatchmark ( hide )

labels alignment is center

Here is an example:

FlutterSlider(
  ...
    hatchMark: FlutterSliderHatchMark(
       density: 0.5, // means 50 lines, from 0 to 100 percent
       labels: [
         FlutterSliderHatchMarkLabel(percent: 0, label: Text('Start')),
         FlutterSliderHatchMarkLabel(percent: 10, label: Text('10,000')),
         FlutterSliderHatchMarkLabel(percent: 50, label: Text('50 %')),
         FlutterSliderHatchMarkLabel(percent: 80, label: Text('80,000')),
         FlutterSliderHatchMarkLabel(percent: 100, label: Text('Finish')),
       ],
     ),
  ...
)

Centered Origin

If you want the value of your slider originates from center of the slider, then you can use centeredOrigin property

FlutterSlider(
  ...
    centeredOrigin: true
  ...
  
  ...
    trackBar: FlutterSliderTrackBar(
      activeTrackBar: BoxDecoration(color: trackBarColor)
    ),
  ...
  
  ...
    onDragging: (handlerIndex, lowerValue, upperValue) {
        if (lowerValue > (max - min) / 2) {
          trackBarColor = Colors.blueAccent;
        } else {
          trackBarColor = Colors.redAccent;
        }
        setState(() {});
    })
  ...
)

Touch Size

You can control how big a handler's touch area could be. by default touch size is 25 The range is between 5 to 50

FlutterSlider(
  ...
  touchSize: 25,
  ...
)

To see the touchable area for handlers, set visibleTouchArea to true and test your slider

FlutterSlider(
  ...
  visibleTouchArea: true,
  ...
)

Disabled

to disable your slider, you can use disabled.

FlutterSlider(
  ...
  disabled: true,
  ...
)

RTL

makes the slider Right To Left

FlutterSlider(
  ...
  rtl: true,
  ...
)

Events

There are 3 events

onDragStarted: fires when drag starts
onDragCompleted fires when drag ends
onDragging keeps firing when dragging

All three of above functions returns three values.

(int handlerIndex, dynamic lowerValue, dynamic upperValue)

First value is handlerIndex, which determines the handler. 0 is Left Handler and 1 refers to Right Handler

FlutterSlider(
  ...
  onDragging: (handlerIndex, lowerValue, upperValue) {
    _lowerValue = lowerValue;
    _upperValue = upperValue;
    
    if(handlerIndex == 0)
        print(" Left handler ");
    
    setState(() {});
  },
  ...
)

Working with Dates

Working with dates are simple and straightforward. just pass standard unix timestamp as values like so:

FlutterSlider(
  ...
  values: [
    new DateTime(2019,6,1,0,0,0).millisecondsSinceEpoch.toDouble(), // lower date : 2019-06-01
    new DateTime(2019,9,1,0,0,0).millisecondsSinceEpoch.toDouble(), // upper date : 2019-09-01
  ],
  min: new DateTime(2019,1,1,0,0,0).millisecondsSinceEpoch.toDouble(), // start date : 2019-01-01
  max: new DateTime(2020,1,1,0,0,0).millisecondsSinceEpoch.toDouble(), // finish date : 2020-01-01
  step: FlutterSliderStep(step: 86400), // 1 day
  ...

  ...
  tooltip: FlutterSliderTooltip(
    custom: (value) {
      DateTime dtValue = DateTime.fromMillisecondsSinceEpoch(value.toInt());
      String valueInTime = dtValue.year.toString() + '-' + dtValue.month.toString() + '-' + dtValue.day.toString();
      
      return Text( valueInTime );
    }
  )
  ...

)

Credits

based on https://pub.dev/packages/flutter_xlider

another_xlider's People

Contributors

chriselliotuk avatar erknvl avatar felixgabler avatar john-paradym avatar loonix avatar yasufumimuranaka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

another_xlider's Issues

FlutterSliderTooltip disableAnimation and alwaysShowTooltip odd

Hi, I tested your xlider and have some odd issues when setting disableAnimation and alwaysShowTooltip:

First, if the tooltip is like this

          tooltip: FlutterSliderTooltip(
            disableAnimation: true,
            alwaysShowTooltip: false,
            direction: FlutterSliderTooltipDirection.right,
          ),

The tooltip will animate from top to right.
If you set FlutterSliderTooltipDirection.top it will popup on top as expected.

Second, when tooltip is like this

          tooltip: FlutterSliderTooltip(
            disableAnimation: true,
            alwaysShowTooltip: true,
            direction: FlutterSliderTooltipDirection.right,
          ),

Tooltip will always show on top, and the FlutterSliderTooltipDirection.left/top/right has little to no effect
image

Third, when tooltip is like this

          tooltip: FlutterSliderTooltip(
            disableAnimation: false,
            alwaysShowTooltip: true,
            direction: FlutterSliderTooltipDirection.right,
          ),

The tooltip are actually behind the knob
image

Is this by design or is it buggy?

Dragging when selectByTap is false

Hi! Thanks for supporting this component. It's super useful!

I have encountered one issue: when I set selectByTap to false I still can tap on the slider outside of the handlebar and it jumps to this position. Is it possible to specify behavior when only if user drags the handlebar then it changes it's position and calls all dragging related callbacks?

The problem actually comes from UI where we have multiple sliders in the list and when user tries to scroll through the list of these sliders they start changing values although user triggered scrolling by tapping outside of handlebars.

Help in detail is needed

First off, good job with your widget, but your help doc is a bit lacking in depth, IMHO. Simple things like if values is an initial value, how and when is used, why it is required. Your code sample can be give certain guide but it cannot be a substitute for written documentation.

This is a humble suggestion only.

Fixed values range

Hi I found the bug. (or I'm just so tired that's not a bug only a feature)

widget.values[0] <= 100)) {

In this condition the actual value is compared to the percents min 0 max 100. Thats why the thow occurs every time when value is bigger than 100 or smaller than 0. You need to calculate the percent of value with the highest fixedValues or compare with _widgetMax and the verify if fixedValues do not contain velues bigger than _widgetMax

if (!(widget.fixedValues != null &&
widget.values[0] >= _widgetMin! &&
widget.values[0] <= _widgetMax! )) {
throw 'When using fixedValues, you should set values within the range of fixedValues';
}

the same for the range slider

Flutter 3 support (Warning: Operand of null-aware operation '!' has type 'WidgetsBinding')

When using the package with Flutter 3.0.0:

./../../../.pub-cache/hosted/pub.dartlang.org/another_xlider-1.0.1+2/lib/another_xlider.dart:442:20: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
 - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../../fvm/versions/stable/packages/flutter/lib/src/widgets/binding.dart').
    WidgetsBinding.instance!.addPostFrameCallback((_) {

Mix max both equal zero cause BoxConstraints exception

I built a range slider. If I put max and min both equal 0 as following code:

FlutterSlider(
    values: [_lowerValue, _upperValue],  // both are 0
    touchSize: 5,
    rangeSlider: true,
    trackBar: FlutterSliderTrackBar(
      activeTrackBarDraggable: false,
      inactiveTrackBar: BoxDecoration(
        borderRadius: BorderRadius.circular(20),
        color: Colors.black12,
      ),
      activeTrackBar: BoxDecoration(
        borderRadius: BorderRadius.circular(4.0),
        color: accentBlue,
      ),
    ),
    max: maxValue, // is 0
    min: minValue,  // is 0
)

I got following error when dragging handlers, then all the handlers jump to the top-left of the screen:

The following assertion was thrown building Container(bg: BoxDecoration(color: Color(0xff1884db), borderRadius: BorderRadius.circular(4.0)), constraints: BoxConstraints(NaN<=w<=NaN, h=3.5; NOT NORMALIZED)):
BoxConstraints has NaN values in minWidth and maxWidth.

The offending constraints were: BoxConstraints(NaN<=w<=NaN, h=3.5; NOT NORMALIZED)

Hint from BorderRadius.circular(4.0), I guess the problem is from the activeTrackBar.

P/S: The error occurs when setting all max, min, _lowerValue, _upperValue to any equal number.

_stopHandlerAnimation should stop force unwrapping potentially null parameters

Seeing occasional crashes on line 1686 in another_xlider.dart when it's trying to force unwrap a null object i.e.

controller!.reset();

Obviously controller (and animation) can be passed in as null, so the function should check for this

If I get time, I'll try submitting a pull request to fix this, but wanted to capture this as an issue

Thanks
John

Broken off-handler dragging in 3.0.1 version

Looks like 3.0.1 version broke the off-handler dragging behaviour.
On 3.0.0 version, when you drag your finger on the track (off handler) it drags properly, however on the 3.0.1 version you can see value changes only after dragging ends (you take finger up off screen)
Im attaching videos to compare the 3.0.0 version behaviour with 3.0.1.

v.3.0.0.mp4
v.3.0.1.mp4

Method 'addPostFrameCallback' cannot be called on 'WidgetsBinding?' because it is potentially null.

Error:

/F:/SDK/flutter_windows_2.10.5-stable/flutter/.pub-cache/hosted/pub.flutter-io.cn/another_xlider-1.1.0/lib/another_xlider.dart:441:29: Error: Method 'addPostFrameCallback' cannot be called on 'WidgetsBinding?' because it is potentially null.

  • 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('/F:/SDK/flutter_windows_2.10.5-stable/flutter/packages/flutter/lib/src/widgets/binding.dart').
    Try calling using ?. instead.
    WidgetsBinding.instance.addPostFrameCallback((_) {
    ^^^^^^^^^^^^^^^^^^^^

Issue with touch-area / touch sensitivity

I'm facing issues with the touch-area of the slider.

If you try to scroll in an app but you touch the xlider widget you catch the widget instead of scrolling. This happens even if stepByTap is disabled.

I've uploaded a video showing the issue:

vsdc-sr.2023-12-06.12-54-30.mp4

Is it possible to set the touch sensitivity of handlers and the trackbar or reduced the tapping to only the handler?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.