basic_selector 0.1.2

SDKflutter
Platformandroidioswindowslinuxmacosweb

Basic Selector is a Flutter package that creates a widget to select a value from a wheeled like picker.

Basic selector

pub package

With BasicSelector

Basic with numbersStyling items with numbersStyling container with numbers
Basic with textBasic loop with text

With BasicTimeSelector

Basic with timeBasic time selectorBasic time selector on dialog

How to use

The BasicSelector widget has three constructors. The BasicSelector() will use the default style. Also BasicSelector.items() which just uses the items list as formal parameter and BasicSelector.loop() which enables loop by default and just scrolls infinitely through the items provided.

Add this to your package's pubspec.yaml file, use the latest version Pub:

dependencies:
  basic_selector: ^latest_version
import 'package:basic_selector/basic_selector.dart';

// Generate a list of numbers with BuildSelectorHelpers
static final List<int> _numbers = BasicSelectorHelpers.generateNumbers(end: 10);
int _selectedNumber = 0;

// Then build this widget
BasicSelector<int>(
    items: _numbers,
    value: _selectedNumber,
    textFormatter: (item) { // Example of conditional formatting
        if (item == 1) {
          return '1 item';
        } else {
          return '$item items';
        }
    },
    onChanged: (item) {
        setState(() {
          _selectedNumber = item;
        });
    },
),

BasicSelector uses <T> for setting the type for the selected value, list items and onChange function.

Some examples are:

  • BasicSelector<int>
  • BasicSelector<double>
  • BasicSelector<String>
  • BasicSelector<DateTime>
  • BasicSelector<TimeOfDay>
  • And some other Types

Other functionalities

basic_selector also provides another widget built with BasicSelector, one for selecting times (TimeOfDay): BasicTimeSelector.
As well as some static helpers on BasicSelectorHelpers and some extensions on TimeOfDay (See below).

Usage for BasicTimeSelector

See Properties for more details.

import 'package:basic_selector/basic_selector.dart';

// Generate a list of numbers with BuildSelectorHelpers
TimeOfDay _selectedTime = const TimeOfDay(hour: 0, minute: 0);

// Then build this widget
BasicTimeSelector(
    time: _selectedTime,
    currentTime: const TimeOfDay(hour: 13, minute: 30),
    config: const BasicTimeSelectorConfig(showOnlyToCurrentType: true),
    textFormatter: (item) {
        return item.formatted();
    },
    onChanged: (item) {
        setState(() {
            _selectedTime = item;
        });
    },
),

Usage for BasicTimeSelector.showModalDialog()

This method shows a basic dialog for selecting a TimeOfDay

import 'package:basic_selector/basic_selector.dart';

// Generate a list of numbers with BuildSelectorHelpers
TimeOfDay _selectedTime = const TimeOfDay(hour: 0, minute: 0);

// Then build this widget
FilledButton(
    onPressed: () => BasicTimeSelector.showModalDialog(
        context,
        time: _selectedTime,
    ).then((value) {
        if (value != null) {
            setState(() {
                _selectedTime = value;
        });

        ScaffoldMessenger.of(context)
          ..hideCurrentSnackBar()
          ..showSnackBar(SnackBar(
            content: Text(
              'Date selected: ${_selectedTime.formatted()}',
            ),
            behavior: SnackBarBehavior.floating,
          ));
      }
    }),
    child: const Text('Open dialog'),
),

Helper methods (BasicSelectorHelpers)

This package provides some static methods for generating some very basic values.

List<T> generateNumbers<T extends num>()

Generates a list of <T> that extends num, so it must be a number, typically of type int or double.

static final List<int> _numbers = BasicSelectorHelpers.generateNumbers(start: 25, end: 50, step: 5);
// Returns a list of type int
// [25, 30, 35, 40, 45, 50]
PropertyTypeDescriptionDefault ValueRequired
startnumThe start number of the generated listnull ?? 0
endnumThe end number of the generated list✅
stepnumThe interval of number of the generated list1

List<String> generateAlphabet()

Generates a list of <String> with the letters of the alphabet.

static final List<String> _alphabet = BasicSelectorHelpers.generateAlphabet();
// Returns a list of type String
// ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

No properties needed for this method

List<TimeOfDay> generateTimes()

Generates a list of <TimeOfDay> with all times from 00:00 to 23:59 if no properties used.

static final List<TimeOfDay> _times = BasicSelectorHelpers.generateTimes(minuteStep: 30);
// Returns a list of type TimeOfDay with a step of 30 mins
// [TimeOfDay(0, 0), TimeOfDay(0, 30), TimeOfDay(1, 0), TimeOfDay(1, 30), ..., TimeOfDay(23, 30)]
PropertyTypeDescriptionDefault ValueRequired
fromTimeOfDayFrom time to generate listnull ?? const TimeOfDay(hour: 00, minute: 00)
toTimeOfDayTo time to generate listnull ?? const TimeOfDay(hour: 23, minute: 59)
minuteStepintThe interval of minutes to generate list1

Examples

If you want to see some usage examples, clone the example app with usages like:

Properties

BasicSelector

PropertyTypeDescriptionDefault ValueRequired
itemsList<T>The list of items to be iterated on the selector✅
valueTThe selected initial value, must be a value from items✅
onChangedValueChanged<T>A void callback function that runs when the selector changes✅
textFormatterTextFormatter<T>A way to format the text shown on the selectornull
heightdoubleThe height of the selector200
stylesBasicSelectorStyleA way to style the BasicSelectorconst BasicSelectorStyle()
loopboolEnables infinite scroll for itemsfalse
reversedboolTo reverse the itemsfalse

BasicSelectorStyle

PropertyTypeDescriptionDefault ValueRequired
selectedValueTextStyleTextStyleStyle for the selected valueconst TextStyle(fontWeight: FontWeight.bold)
notSelectedValueTextStyleTextStyleStyle for the rest of the values that are not selectedconst TextStyle(fontWeight: FontWeight.normal)
selectedValueContainerDecorationBoxDecorationStyle for the container around the selected valuenull ?? BoxDecoration(color: Theme.of(context).colorScheme.surface,borderRadius: BorderRadius.circular(10),border: Border.all(color: Theme.of(context).colorScheme.primary,),)
bordersGradientBackgroundColorColorColor for gradient on the edges of the selectornull ?? Theme.of(context).scaffoldBackgroundColor

BasicTimeSelector

PropertyTypeDescriptionDefault ValueRequired
timeTimeOfDayThe selected time value✅
currentTimeTimeOfDayIn case is needed a different time from TimeOfDay.now()null ?? TimeOfDay.now()
onChangedValueChanged<TimeOfDay>A void callback function that runs when the selector changes✅
textFormatterString Function(T item)A way to format the text shown on the selectornull
heightdoubleThe height of the selector200
stylesBasicTimeSelectorStyleA way to style the BasicTimeSelectorconst BasicTimeSelectorStyle()
configBasicTimeSelectorConfigA way to configure the BasicTimeSelectorconst BasicTimeSelectorConfig()
loopboolEnables infinite scroll for itemsfalse

BasicTimeSelector.showModalDialog()

PropertyTypeDescriptionDefault ValueRequired
contextBuildContextCurrent context✅
timeTimeOfDayThe selected time value✅
currentTimeTimeOfDayIn case is needed a different time from TimeOfDay.now()null ?? TimeOfDay.now()
textFormatterString Function(T item)A way to format the text shown on the selector on the dialognull
heightdoubleThe height of the selector200
stylesBasicTimeSelectorStyleA way to style the BasicTimeSelector on the dialogconst BasicTimeSelectorStyle()
configBasicTimeSelectorConfigA way to configure the BasicTimeSelector on the dialogconst BasicTimeSelectorConfig()
dialogConfigBasicTimeSelectorDialogConfigA way to configure the BasicTimeSelector.showModalDialog() on the dialogconst BasicTimeSelectorDialogConfig()
loopboolEnables infinite scroll for itemsfalse

BasicTimeSelectorStyle

Extends all properties from BasicSelectorStyle and also

PropertyTypeDescriptionDefault ValueRequired
twoDotSeparatorStyleTextStyleStyle for ':' that is in the middle of the two selectorsconst TextStyle(fontWeight: FontWeight.bold,fontSize: 20,)

BasicTimeSelectorConfig

PropertyTypeDescriptionDefault ValueRequired
showOnlyFromCurrentTypeboolShows only the times from the selected currentTimefalse
showOnlyToCurrentTypeboolShows only the times to the selected currentTimefalse
minuteStepintInterval for the generated minutes (must be a divider of 60)1
startTimeTimeOfDayThe start date when generating timesnull
endTimeTimeOfDayThe end date when generating timesnull

BasicTimeSelectorDialogConfig

PropertyTypeDescriptionDefault ValueRequired
titleStringA title for the dialog'Select a time'
titleStyleTextStyleA way to style the title of the dialogconst TextStyle(fontWeight: FontWeight.bold,fontSize: 16,)
descriptionStringA description for the dialog, that goes right below the titlenull
titleStyleTextStyleA way to style the description of the dialognull ?? TextStyle(fontSize: 12, color: Colors.grey[600],)
backgroundColorColorThe background color for the dialognull
elevationdoubleThe elevation for the dialognull
shapeShapeBorderThe shape for the dialognull ?? RoundedRectangleBorder(borderRadius: BorderRadius.circular(10),)
confirmButtonBuilderWidget Function(TimeOfDay value)A builder for the confirm button, if not provided a default TextButton will be usednull
confirmButtonTextStringThe text used for the confirm button (ignored if confirmButtonBuilder is used)'Confirm'
cancelButtonBuilderWidget Function()A builder for the cancel button, if not provided a default TextButton will be usednull
cancelButtonTextStringThe text used for the cancel button (ignored if cancelButtonBuilder is used)'Cancel'
paddingEdgeInsetsThe padding for for the actions (the confirm and cancel buttons, or the builders) and for the contentnull
builderWidget Function(BasicTimeSelector basicTimeSelector, TimeOfDay? selectedTime)This is a Builder for the content of the dialog, the parameter value is the BasicTimeSelector you should use in the builder.null
extraContentList<Widget>Extra content that will go right below the BasicTimeSelector on the dialog (ignored if a builder is used)null



If you like this package, consider supporting it by giving it a star on GitHub and a like on pub.dev 💙

⌨️ with ❤️ by eljorgit🙃

You like the package? Buy me a coffee :)

ko-fi