Skip to content

Instantly share code, notes, and snippets.

@Mravuri96
Created September 16, 2019 03:05
Show Gist options
  • Save Mravuri96/1716c1679cff359cc93f8b3735346325 to your computer and use it in GitHub Desktop.
Save Mravuri96/1716c1679cff359cc93f8b3735346325 to your computer and use it in GitHub Desktop.
import 'package:flutter/widgets.dart';
class HorizWheel extends StatefulWidget {
final TextStyle selectTextStyle;
final TextStyle unSelectTextStyle;
final Function(dynamic) onValueChanged;
final List<dynamic> datas;
final int startPosition;
final double itemSize;
final double squeeze;
final double magnification;
final double perspective;
final double listHeight;
final double listWidth;
final List<Widget> children;
static const double _defaultitemSize = 48.0;
HorizWheel({
@required this.onValueChanged,
@required this.datas,
this.selectTextStyle,
this.unSelectTextStyle,
this.startPosition = 0,
this.squeeze = 1.0,
this.itemSize = _defaultitemSize,
this.magnification = 1,
this.perspective = 0.01,
this.listWidth,
this.listHeight,
}) : assert(perspective <= 0.01),
children = null;
HorizWheel.custom({
@required this.onValueChanged,
@required this.children,
this.datas,
this.startPosition = 0,
this.squeeze = 1.0,
this.itemSize = _defaultitemSize,
this.magnification = 1,
this.perspective = 0.01,
this.listWidth,
this.listHeight,
}) : assert(perspective <= 0.01),
assert(datas == null || datas.length == children.length),
selectTextStyle = null,
unSelectTextStyle = null;
HorizWheel.integer({
@required this.onValueChanged,
@required int maxValue,
@required int minValue,
int initValue,
int step = 1,
this.selectTextStyle,
this.unSelectTextStyle,
this.squeeze = 1.0,
this.itemSize = _defaultitemSize,
this.magnification = 1,
this.perspective = 0.01,
this.listWidth,
this.listHeight,
}) : assert(perspective <= 0.01),
assert(minValue < maxValue || minValue > maxValue),
assert(initValue == null || initValue >= minValue),
assert(initValue == null || maxValue >= initValue),
children = null,
datas = _createIntegerList(minValue, maxValue, step),
startPosition = initValue == null ? 0 : initValue - minValue;
static List<int> _createIntegerList(int minValue, int maxValue, int step) {
List<int> result = [];
for (int i = minValue; i <= maxValue; i += step) {
result.add(i);
}
return result;
}
@override
_HorizWheelState createState() {
return _HorizWheelState();
}
}
class _HorizWheelState extends State<HorizWheel> {
FixedExtentScrollController fixedExtentScrollController;
int currentPosition;
@override
void initState() {
super.initState();
currentPosition = widget.startPosition;
fixedExtentScrollController = FixedExtentScrollController(
initialItem: currentPosition,
);
}
void _listener(int position) {
setState(() {
currentPosition = position;
});
if (widget.datas == null) {
widget.onValueChanged(currentPosition);
} else {
widget.onValueChanged(widget.datas[currentPosition]);
}
}
@override
Widget build(BuildContext context) {
return Container(
height: widget.listHeight ?? double.infinity,
width: widget.listWidth ?? double.infinity,
child: ListWheelScrollView(
onSelectedItemChanged: _listener,
perspective: widget.perspective,
squeeze: widget.squeeze,
controller: fixedExtentScrollController,
physics: FixedExtentScrollPhysics(),
children: widget.children ?? _buildListItems(),
//useMagnifier: true,
//magnification: widget.magnification,
itemExtent: 64, //widget.itemSize,
));
}
List<Widget> _buildListItems() {
List<Widget> result = [];
for (int i = 0; i < widget.datas.length; i++) {
result.add(
RotatedBox(
quarterTurns: 3,
child: Text(
widget.datas[i].toString(),
textAlign: TextAlign.center,
textScaleFactor: 1.5,
style: i == currentPosition
? widget.selectTextStyle ?? null
: widget.unSelectTextStyle ?? null,
),
),
);
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment