Skip to content

Commit

Permalink
Merge pull request #32 from mathieupost/master
Browse files Browse the repository at this point in the history
Implement looping integer list view
  • Loading branch information
MarcinusX committed Jan 16, 2019
2 parents 7ebd2ab + d6f09ae commit 2ca7d95
Show file tree
Hide file tree
Showing 6 changed files with 324 additions and 83 deletions.
134 changes: 85 additions & 49 deletions example/lib/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,68 +31,74 @@ class MyHomePage extends StatefulWidget {

class _MyHomePageState extends State<MyHomePage> {
int _currentIntValue = 10;
int _currentInfIntValue = 10;
double _currentDoubleValue = 3.0;
NumberPicker integerNumberPicker;
NumberPicker integerInfiniteNumberPicker;
NumberPicker decimalNumberPicker;

_handleValueChanged(num value) {
if (value != null) {
if (value is int) {
setState(() => _currentIntValue = value);
} else {
setState(() => _currentDoubleValue = value);
}
}
}

_handleValueChangedExternally(num value) {
if (value != null) {
if (value is int) {
setState(() => _currentIntValue = value);
integerNumberPicker.animateInt(value);
} else {
setState(() => _currentDoubleValue = value);
decimalNumberPicker.animateDecimalAndInteger(value);
}
}
}

@override
Widget build(BuildContext context) {
integerNumberPicker = new NumberPicker.integer(
initialValue: _currentIntValue,
minValue: 0,
maxValue: 100,
step: 10,
onChanged: _handleValueChanged,
onChanged: (value) => setState(() => _currentIntValue = value),
);
integerInfiniteNumberPicker = new NumberPicker.integer(
initialValue: _currentInfIntValue,
minValue: 0,
maxValue: 99,
step: 10,
infiniteLoop: true,
onChanged: (value) => setState(() => _currentInfIntValue = value),
);
decimalNumberPicker = new NumberPicker.decimal(
initialValue: _currentDoubleValue,
minValue: 1,
maxValue: 5,
decimalPlaces: 2,
onChanged: _handleValueChanged);
initialValue: _currentDoubleValue,
minValue: 1,
maxValue: 5,
decimalPlaces: 2,
onChanged: (value) => setState(() => _currentDoubleValue = value),
);
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
integerNumberPicker,
new RaisedButton(
onPressed: () => _showIntDialog(),
child: new Text("Current int value: $_currentIntValue"),
),
decimalNumberPicker,
new RaisedButton(
onPressed: () => _showDoubleDialog(),
child: new Text("Current double value: $_currentDoubleValue"),
),
],
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
new Row(children: <Widget>[
Expanded(
child: Column(children: <Widget>[
Text("Default"),
integerNumberPicker,
new RaisedButton(
onPressed: () => _showIntDialog(),
child: new Text("Current int value: $_currentIntValue"),
),
]),
),
Expanded(
child: Column(children: <Widget>[
Text("With infinite loop"),
integerInfiniteNumberPicker,
new RaisedButton(
onPressed: () => _showInfIntDialog(),
child: new Text("Current int value: $_currentInfIntValue"),
),
]),
),
]),
decimalNumberPicker,
new RaisedButton(
onPressed: () => _showDoubleDialog(),
child: new Text("Current double value: $_currentDoubleValue"),
),
));
],
),
);
}

Future _showIntDialog() async {
Expand All @@ -106,7 +112,32 @@ class _MyHomePageState extends State<MyHomePage> {
initialIntegerValue: _currentIntValue,
);
},
).then(_handleValueChangedExternally);
).then((num value) {
if (value != null) {
setState(() => _currentIntValue = value);
integerNumberPicker.animateInt(value);
}
});
}

Future _showInfIntDialog() async {
await showDialog<int>(
context: context,
builder: (BuildContext context) {
return new NumberPickerDialog.integer(
minValue: 0,
maxValue: 99,
step: 10,
initialIntegerValue: _currentInfIntValue,
infiniteLoop: true,
);
},
).then((num value) {
if (value != null) {
setState(() => _currentInfIntValue = value);
integerInfiniteNumberPicker.animateInt(value);
}
});
}

Future _showDoubleDialog() async {
Expand All @@ -121,6 +152,11 @@ class _MyHomePageState extends State<MyHomePage> {
title: new Text("Pick a decimal number"),
);
},
).then(_handleValueChangedExternally);
).then((num value) {
if (value != null) {
setState(() => _currentDoubleValue = value);
decimalNumberPicker.animateInt(value);
}
});
}
}

0 comments on commit 2ca7d95

Please sign in to comment.