Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: ability to specify the number of visible numbers in the spinner #25

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 83 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,88 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.dart_tool/
.atom/
.buildlog/
.history
.svn/
coverage/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
.vscode/

# Firebase
**/ios/GoogleService-Info.plist

# Flutter/Dart/Pub related
**/doc/api/
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
/build/

# Android related
**/android/**/gradle-wrapper.jar
**/android/.gradle
**/android/captures/
**/android/gradlew
**/android/gradlew.bat
**/android/local.properties
**/android/**/GeneratedPluginRegistrant.java

# iOS/XCode related
#**/ios/**/*.mode1v3
#**/ios/**/*.mode2v3
#**/ios/**/*.moved-aside
#**/ios/**/*.pbxuser
#**/ios/**/*.perspectivev3
#**/ios/**/*sync/
#**/ios/**/.sconsign.dblite
#**/ios/**/.tags*
#**/ios/**/.vagrant/
#**/ios/**/DerivedData/
#**/ios/**/Icon?
#**/ios/**/Pods/
#**/ios/**/.symlinks/
#**/ios/**/profile
#**/ios/**/xcuserdata
#**/ios/.generated/
#**/ios/Flutter/App.framework
#**/ios/Flutter/Flutter.framework
#**/ios/Flutter/Generated.xcconfig
#**/ios/Flutter/app.flx
#**/ios/Flutter/flutter_export_environment.sh
#**/ios/Flutter/app.zip
#**/ios/Flutter/flutter_assets/
#**/ios/ServiceDefinitions.json
#**/ios/Runner/GeneratedPluginRegistrant.*
#**/ios/Flutter/Flutter.podspec
#**/ios/Flutter/flutter_export_environment.sh

# Web related
lib/generated_plugin_registrant.dart

# Exceptions to above rules.
!**/ios/**/default.mode1v3
!**/ios/**/default.mode2v3
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages

build/
ios/.generated/
ios/Flutter/Generated.xcconfig
ios/Runner/GeneratedPluginRegistrant.*
.env*
!.env.example
!.env.development
!.env.production
12 changes: 6 additions & 6 deletions example/ios/Flutter/flutter_export_environment.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/bin/sh
# This is a generated file; do not edit or check into version control.
export "FLUTTER_ROOT=/Users/mjr/flutter"
export "FLUTTER_APPLICATION_PATH=/Users/mjr/Documents/code/libraries/dart/flutter_time_picker_spinner/example"
export "FLUTTER_TARGET=lib/main.dart"
export "FLUTTER_ROOT=C:\flutter"
export "FLUTTER_APPLICATION_PATH=C:\Users\DariusDavidonis\private\flutter_time_picker_spinner\example"
export "COCOAPODS_PARALLEL_CODE_SIGN=true"
export "FLUTTER_TARGET=lib\main.dart"
export "FLUTTER_BUILD_DIR=build"
export "SYMROOT=${SOURCE_ROOT}/../build/ios"
export "FLUTTER_BUILD_NAME=1.0.0"
export "FLUTTER_BUILD_NUMBER=1"
export "DART_OBFUSCATION=false"
export "TRACK_WIDGET_CREATION=false"
export "TRACK_WIDGET_CREATION=true"
export "TREE_SHAKE_ICONS=false"
export "PACKAGE_CONFIG=.packages"
export "PACKAGE_CONFIG=.dart_tool/package_config.json"
1 change: 0 additions & 1 deletion example/test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

//import 'package:example/main.dart';
Expand Down
45 changes: 24 additions & 21 deletions lib/flutter_time_picker_spinner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ class TimePickerSpinner extends StatefulWidget {
final double? spacing;
final bool isForce2Digits;
final TimePickerCallback? onTimeChange;
final int numberOfVisibleNums;

TimePickerSpinner(
{Key? key,
this.time,
this.numberOfVisibleNums = 3,
this.minutesInterval = 1,
this.secondsInterval = 1,
this.is24HourMode = true,
Expand Down Expand Up @@ -115,7 +117,7 @@ class _TimePickerSpinnerState extends State<TimePickerSpinner> {
bool isMinuteScrolling = false;
bool isSecondsScrolling = false;
bool isAPScrolling = false;

int offset = 0;
/// default settings
TextStyle defaultHighlightTextStyle =
TextStyle(fontSize: 32, color: Colors.black);
Expand Down Expand Up @@ -187,39 +189,39 @@ class _TimePickerSpinnerState extends State<TimePickerSpinner> {

@override
void initState() {
currentTime = widget.time == null ? DateTime.now() : widget.time;

offset = widget.numberOfVisibleNums ~/ 2;

currentSelectedHourIndex =
(currentTime!.hour % (widget.is24HourMode ? 24 : 12)) + _getHourCount();
currentTime = widget.time == null ? DateTime.now() : widget.time;
currentSelectedHourIndex = widget.is24HourMode ? currentTime!.hour : currentTime!.hour ~/ 12;
// currentSelectedHourIndex =
// (currentTime!.hour % (widget.is24HourMode ? 24 : 12)) + _getHourCount();
hourController = ScrollController(
initialScrollOffset:
(currentSelectedHourIndex - 1) * _getItemHeight()!);
(currentSelectedHourIndex - offset) * _getItemHeight()!);

currentSelectedMinuteIndex =
(currentTime!.minute / widget.minutesInterval).floor() +
(isLoop(_getMinuteCount()) ? _getMinuteCount() : 1);
minuteController = ScrollController(
initialScrollOffset:
(currentSelectedMinuteIndex - 1) * _getItemHeight()!);
//print(currentSelectedMinuteIndex);
//print((currentSelectedMinuteIndex - 1) * _getItemHeight()!);
(currentSelectedMinuteIndex - offset) * _getItemHeight()!);

currentSelectedSecondIndex =
(currentTime!.second / widget.secondsInterval).floor() +
(isLoop(_getSecondCount()) ? _getSecondCount() : 1);
secondController = ScrollController(
initialScrollOffset:
(currentSelectedSecondIndex - 1) * _getItemHeight()!);
(currentSelectedSecondIndex - offset) * _getItemHeight()!);

currentSelectedAPIndex = currentTime!.hour >= 12 ? 2 : 1;
apController = ScrollController(
initialScrollOffset: (currentSelectedAPIndex - 1) * _getItemHeight()!);
initialScrollOffset: (currentSelectedAPIndex - offset) * _getItemHeight()!);

super.initState();

if (widget.onTimeChange != null) {
WidgetsBinding.instance!
.addPostFrameCallback((_) => widget.onTimeChange!(getDateTime()));
WidgetsBinding.instance.addPostFrameCallback((_) => widget.onTimeChange!(getDateTime()));
}
}

Expand All @@ -229,7 +231,7 @@ class _TimePickerSpinnerState extends State<TimePickerSpinner> {
List<Widget> contents = [
SizedBox(
width: _getItemWidth(),
height: _getItemHeight()! * 3,
height: _getItemHeight()! * widget.numberOfVisibleNums,
child: spinner(
hourController,
_getHourCount(),
Expand All @@ -246,7 +248,7 @@ class _TimePickerSpinnerState extends State<TimePickerSpinner> {
spacer(),
SizedBox(
width: _getItemWidth(),
height: _getItemHeight()! * 3,
height: _getItemHeight()! * widget.numberOfVisibleNums,
child: spinner(
minuteController,
_getMinuteCount(),
Expand All @@ -266,7 +268,7 @@ class _TimePickerSpinnerState extends State<TimePickerSpinner> {
contents.add(spacer());
contents.add(SizedBox(
width: _getItemWidth(),
height: _getItemHeight()! * 3,
height: _getItemHeight()! * widget.numberOfVisibleNums,
child: spinner(
secondController,
_getSecondCount(),
Expand All @@ -286,7 +288,7 @@ class _TimePickerSpinnerState extends State<TimePickerSpinner> {
contents.add(spacer());
contents.add(SizedBox(
width: _getItemWidth()! * 1.2,
height: _getItemHeight()! * 3,
height: _getItemHeight()! * widget.numberOfVisibleNums,
child: apSpinner(),
));
}
Expand All @@ -301,12 +303,13 @@ class _TimePickerSpinnerState extends State<TimePickerSpinner> {
Widget spacer() {
return Container(
width: _getSpacing(),
height: _getItemHeight()! * 3,
height: _getItemHeight()! * widget.numberOfVisibleNums,
);
}

Widget spinner(
ScrollController controller,
/// Max int value of spinner number.
int max,
int selectedIndex,
bool isScrolling,
Expand All @@ -329,7 +332,7 @@ class _TimePickerSpinnerState extends State<TimePickerSpinner> {
onUpdateSelectedIndex(selectedIndex + max);
controller
.jumpTo(controller.offset + (max * _getItemHeight()!));
} else if (segment == 2) {
} else if (segment == widget.numberOfVisibleNums -1) {
onUpdateSelectedIndex(selectedIndex - max);
controller
.jumpTo(controller.offset - (max * _getItemHeight()!));
Expand All @@ -345,7 +348,7 @@ class _TimePickerSpinnerState extends State<TimePickerSpinner> {
} else if (scrollNotification is ScrollUpdateNotification) {
setState(() {
onUpdateSelectedIndex(
(controller.offset / _getItemHeight()!).round() + 1);
(controller.offset / _getItemHeight()!).round() + offset);
});
}
return true;
Expand Down Expand Up @@ -378,7 +381,7 @@ class _TimePickerSpinnerState extends State<TimePickerSpinner> {
);
},
controller: controller,
itemCount: isLoop(max) ? max * 3 : max + 2,
itemCount: isLoop(max) ? max * widget.numberOfVisibleNums : max + 2,
physics: ItemScrollPhysics(itemHeight: _getItemHeight()),
padding: EdgeInsets.zero,
),
Expand Down Expand Up @@ -432,7 +435,7 @@ class _TimePickerSpinnerState extends State<TimePickerSpinner> {
);
},
controller: apController,
itemCount: 4,
itemCount: widget.numberOfVisibleNums +1,
physics: ItemScrollPhysics(
itemHeight: _getItemHeight(),
targetPixelsLimit: 1,
Expand Down
Loading