Search code examples
flutterdartflutter-slider

Flutter- how to get currentValue out of FlutterSlider (flutter_xlider)


I am using the flutterSilder dependency since it gives me the possibility to use "FluterSliderSteps", making a non linear increase of the value possible (exponential). Now I got the problem that I don't know how to get the current value out of the slider, in order to use it to display the value as text elsewhere or to save/use it. Underneath I put some basic code, displaying the foundations. There is a padding field class with a text field on the top that saying "myFeedbackText" which should display the value instead. Does anybody have an Idea? Thank you! https://pub.dev/packages/flutter_xlider

class flutterSlider extends StatefulWidget {
  @override
  _flutterSliderState createState() => _flutterSliderState();
}

class _flutterSliderState extends State<flutterSlider> {

  RangeValues range = RangeValues(1, 100);
  double slide = 2;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(

      body: Container(

        color: Color(0xffE5E5E5),
        child: Center(
              child: Container(
                child: Align(
                  child: Material(
                    color: Colors.white,
                    elevation: 14.0,
                    borderRadius: BorderRadius.circular(24.0),
                    shadowColor: Color(0x802196F3),
                    child: Container(
                        width: 350.0,
                        height: 400.0,
                        child: Column(
                          children: <Widget>[
                            SizedBox(height: 30,),
                            Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Container(
                                  child: Text(
                                "myFeedbackText", // should display currentValue
                                style: TextStyle(
                                    color: Colors.black, fontSize: 22.0),
                              )),
                            ),

                            Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Container(
                               child: Builder( //Needed to find Scaffold.of(context) and display the snackbar
                                   builder: (BuildContext context) {
                                     return FlutterSlider(
                                       values: [300],
                                       max: 500,
                                       min: 0,
                                       onDragging: (handlerIndex, lowerValue, upperValue) {
                                         setState(() {});
                                       },
                                       step: FlutterSliderStep(
                                           step: 1, // default
                                           isPercentRange: true, // ranges are percents, 0% to 20% and so on... . default is true
                                           rangeList: [
                                             FlutterSliderRangeStep(from: 0, to: 100, step: 1),
                                             FlutterSliderRangeStep(from: 100, to: 300, step: 5),
                                             FlutterSliderRangeStep(from: 300, to: 500, step: 10),
                                           ]
                                       ),
                                     );
                                   }
                               ),
                              ),
                            ),

                          ],
                        )),
                  ),
                ),
              ),
            ),

      ),
    );
  }
}

Solution

  • You can copy paste run full code below
    You can define a variable _lowerValue and update it with onDragging and onDragCompleted

    code snippet

    double _lowerValue = 300;
    ... 
    Container(
          child: Text(
        "$_lowerValue",
    ...                         
    FlutterSlider(
        values: [_lowerValue],
        max: 500,
        min: 0,
        onDragging:
            (handlerIndex, lowerValue, upperValue) {
          setState(() {
            _lowerValue = lowerValue;
          });
        },
        onDragCompleted:
            (handlerIndex, lowerValue, upperValue) {
          setState(() {
            _lowerValue = lowerValue;
          });
        },
    

    working demo

    enter image description here

    full code

    import 'package:flutter/material.dart';
    import 'package:flutter_xlider/flutter_xlider.dart';
    
    class flutterSlider extends StatefulWidget {
      @override
      _flutterSliderState createState() => _flutterSliderState();
    }
    
    class _flutterSliderState extends State<flutterSlider> {
      RangeValues range = RangeValues(1, 100);
      double slide = 2;
      double _lowerValue = 300;
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          body: Container(
            color: Color(0xffE5E5E5),
            child: Center(
              child: Container(
                child: Align(
                  child: Material(
                    color: Colors.white,
                    elevation: 14.0,
                    borderRadius: BorderRadius.circular(24.0),
                    shadowColor: Color(0x802196F3),
                    child: Container(
                        width: 350.0,
                        height: 400.0,
                        child: Column(
                          children: <Widget>[
                            SizedBox(
                              height: 30,
                            ),
                            Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Container(
                                  child: Text(
                                "$_lowerValue", // should display currentValue
                                style:
                                    TextStyle(color: Colors.black, fontSize: 22.0),
                              )),
                            ),
                            Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Container(
                                child: Builder(
                                    //Needed to find Scaffold.of(context) and display the snackbar
                                    builder: (BuildContext context) {
                                  return FlutterSlider(
                                    values: [_lowerValue],
                                    max: 500,
                                    min: 0,
                                    onDragging:
                                        (handlerIndex, lowerValue, upperValue) {
                                      setState(() {
                                        _lowerValue = lowerValue;
                                      });
                                    },
                                    onDragCompleted:
                                        (handlerIndex, lowerValue, upperValue) {
                                      setState(() {
                                        _lowerValue = lowerValue;
                                      });
                                    },
                                    step: FlutterSliderStep(
                                        step: 1, // default
                                        isPercentRange:
                                            true, // ranges are percents, 0% to 20% and so on... . default is true
                                        rangeList: [
                                          FlutterSliderRangeStep(
                                              from: 0, to: 100, step: 1),
                                          FlutterSliderRangeStep(
                                              from: 100, to: 300, step: 5),
                                          FlutterSliderRangeStep(
                                              from: 300, to: 500, step: 10),
                                        ]),
                                  );
                                }),
                              ),
                            ),
                          ],
                        )),
                  ),
                ),
              ),
            ),
          ),
        );
      }
    }
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: flutterSlider(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
    
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  'You have pushed the button this many times:',
                ),
                Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.headline4,
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ),
        );
      }
    }