Last active
April 29, 2022 10:44
-
-
Save blackholeearth/3afb6f295e0916e91e898d9baf48f416 to your computer and use it in GitHub Desktop.
customstep flutter slider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'dart:math'; | |
void main() => runApp(const MyApp()); | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
static const String _title = 'Flutter Code CustomStep_RangeSlider'; | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: _title, | |
home: Scaffold( | |
appBar: AppBar(title: const Text(_title)), | |
body: const Custom_RangeSliderWidget(), | |
), | |
); | |
} | |
} | |
class Custom_RangeSliderWidget extends StatefulWidget { | |
const Custom_RangeSliderWidget({Key? key}) : super(key: key); | |
@override | |
State<Custom_RangeSliderWidget> createState() => _Custom_RangeSliderWidgetState(); | |
} | |
class _Custom_RangeSliderWidgetState extends State<Custom_RangeSliderWidget> { | |
//This fella actually holds Indexes notValues. | |
RangeValues _currentRangeValues = const RangeValues(0, 49); | |
//edit: Use this funcs to getGbvalues from slider min max handle | |
double GetSliderMinValue(){ | |
var tmp= getGbFromRangeValue(_currentRangeValues.start.round()); | |
return tmp; | |
} | |
double GetSliderMaxValue(){ | |
var tmp = getGbFromRangeValue(_currentRangeValues.end.round()) ; | |
return tmp; | |
} | |
List<double> generateList(double min,double max,double stepSize) | |
{ | |
//List<double>.generate(7, (i) => i * 0.05) | |
List<double> tmplist = []; | |
var stepCount = (max -min)/stepSize; | |
for( var i = 0 ; i < stepCount ; i++ ) { | |
tmplist.add(min+ i*stepSize); | |
} | |
return tmplist ; | |
} | |
List<double> gblist = []; | |
List<double> generateGbArray() { | |
return | |
generateList(0.0 ,0.30 ,0.05)+ | |
generateList(0.3 ,1 ,0.1)+ | |
generateList( 1 ,5 ,0.5)+ | |
generateList( 5 ,10 ,1)+ | |
generateList( 10 ,50 ,5)+ | |
generateList( 50 ,100 ,10)+ | |
generateList( 100 ,1000 ,100) | |
+ [1000 ] /*1terabyte */ | |
+[1000 * 1024 ] /*1000terabyte , u cant even download this*/ | |
; | |
} | |
double getGbFromRangeValue(int rangeValue) { | |
if (gblist.isEmpty) { | |
gblist = generateGbArray(); | |
} | |
//return -1; | |
if(rangeValue>=gblist.length) | |
return -2;//index out of bounds | |
var retval = gblist.elementAt(rangeValue); | |
return retval; | |
} | |
@override | |
Widget build(BuildContext context) { | |
//this func inits "gblist" ifEmpty | |
var dump= getGbFromRangeValue(1); | |
var listLength = 49; | |
// Dont have to write 49 anymore | |
listLength = gblist.length; | |
return RangeSlider( | |
values: _currentRangeValues, | |
max: listLength, | |
divisions: listLength, | |
labels: RangeLabels( | |
GetSliderMinValue() | |
.toStringAsFixed(2) + "gb", | |
GetSliderMaxValue() | |
.toStringAsFixed(2) + "gb", | |
), | |
onChanged: (RangeValues values) { | |
setState(() { | |
_currentRangeValues = values; | |
//Debug-start- | |
//_currentRangeValues is holding Indexes | |
// Word "Value is misguiding, i know" | |
//Get Indexes 1 to 49 | |
print(_currentRangeValues); | |
//GetValues 0 to 1000terabyte | |
var min =GetSliderMinValue(); | |
print(min); | |
var max =GetSliderMaxValue(); | |
print(max); | |
//Debug-end- | |
}); | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment