Giter Site home page Giter Site logo

davidanaya / flutter-circular-slider Goto Github PK

View Code? Open in Web Editor NEW
202.0 4.0 71.0 2.01 MB

A customizable circular slider for Flutter.

Home Page: https://pub.dartlang.org/packages/flutter_circular_slider

License: MIT License

Java 2.60% Objective-C 2.12% Dart 94.19% Shell 1.08%
flutter flutter-widget slider

flutter-circular-slider's Introduction

flutter_circular_slider

Build Status License: MIT Pub

A customizable circular slider for Flutter.

Getting Started

Installation

Add

flutter_circular_slider : ^lastest_version

to your pubspec.yaml, and run

flutter packages get

in your project's root directory.

Basic Usage

Create a new project with command

flutter create myapp

Edit lib/main.dart like this:

import 'package:flutter/material.dart';

import 'package:flutter_circular_slider/flutter_circular_slider.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.blueGrey,
        body: Center(
          child: Container(child: DoubleCircularSlider(100, 0, 20)),
        ));
  }
}

There are two different options:

  • SingleCircularSlider: has only one handler and can be moved either dragging the handler or just by clicking on different parts of the slider.
  • DoubleCircularSlider: has two handlers and both have to be moved by dragging them.

Basic Slider

Constructor

Parameter Default Description
divisions The number of sections in which the circle will be divided for selection.
init (Only for DoubleCircularSlider) The initial value in the selection. Has to be bigger than 0 and smaller than divisions.
end (Only for DoubleCircularSlider) The end value in the selection. Has to be bigger than 0 and smaller than divisions.
position (Only for SingleCircularSlider) The selection. Has to be bigger than 0 and smaller than divisions.
height 220.0 Height of the canvas where the slider is rendered.
width 220.0 Width of the canvas where the slider is rendered.
primarySectors 0 Number of sectors painted in the base circle. Painted in selectionColor.
secondarySectors 0 Number of secondary sectors painted in the base circle. Painted in baseColor.
child null Widget that will be inserted in the center of the circular slider.
onSelectionChange void onSelectionChange(int init, int end, int laps) Triggered every time the user interacts with the slider and changes the init and end values, and also laps.
onSelectionEnd void onSelectionEnd(int init, int end, int laps) Triggered every time the user finish one interaction with the slider.
baseColor Color.fromRGBO(255, 255, 255, 0.1) The color used for the base of the circle.
selectionColor Color.fromRGBO(255, 255, 255, 0.3) The color used for the selection in the circle.
handlerColor Colors.white The color used for the handlers.
handlerOutterRadius 12.0 The radius for the outter circle around the handler.
showRoundedCapInSelection false (Only for SingleCircularSlider) Shows a rounded cap at the edge of the selection slider with no handler.
showHandlerOutter true If true will display an extra ring around the handlers.
sliderStrokeWidth 12.0 The stroke width for the slider (thickness).
shouldCountLaps false If true, onSelectionChange will also return the updated number of laps.

Use Cases

Move complete selection

Move Selection

Single Handler

Sleep Single Slider

Laps

Sleep Single Slider Laps

Sleep Double Slider Laps

Sleep Time Selection

import 'dart:math';

import 'package:flutter/material.dart';

import 'package:flutter_circular_slider/flutter_circular_slider.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SafeArea(
      child: Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage('images/background_morning.png'),
              fit: BoxFit.cover,
            ),
          ),
          child: SleepPage()),
    ));
  }
}

class SleepPage extends StatefulWidget {
  @override
  _SleepPageState createState() => _SleepPageState();
}

class _SleepPageState extends State<SleepPage> {
  final baseColor = Color.fromRGBO(255, 255, 255, 0.3);

  int initTime;
  int endTime;

  int inBedTime;
  int outBedTime;

  @override
  void initState() {
    super.initState();
    _shuffle();
  }

  void _shuffle() {
    setState(() {
      initTime = _generateRandomTime();
      endTime = _generateRandomTime();
      inBedTime = initTime;
      outBedTime = endTime;
    });
  }

  void _updateLabels(int init, int end) {
    setState(() {
      inBedTime = init;
      outBedTime = end;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        Text(
          'How long did you stay in bed?',
          style: TextStyle(color: Colors.white),
        ),
        DoubleCircularSlider(
          288,
          initTime,
          endTime,
          height: 260.0,
          width: 260.0,
          primarySectors: 6,
          secondarySectors: 24,
          baseColor: Color.fromRGBO(255, 255, 255, 0.1),
          selectionColor: baseColor,
          handlerColor: Colors.white,
          handlerOutterRadius: 12.0,
          onSelectionChange: _updateLabels,
          sliderStrokeWidth: 12.0,
          child: Padding(
            padding: const EdgeInsets.all(42.0),
            child: Center(
                child: Text('${_formatIntervalTime(inBedTime, outBedTime)}',
                    style: TextStyle(fontSize: 36.0, color: Colors.white))),
          ),
        ),
        Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
          _formatBedTime('IN THE', inBedTime),
          _formatBedTime('OUT OF', outBedTime),
        ]),
        FlatButton(
          child: Text('S H U F F L E'),
          color: baseColor,
          textColor: Colors.white,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(50.0),
          ),
          onPressed: _shuffle,
        ),
      ],
    );
  }

  Widget _formatBedTime(String pre, int time) {
    return Column(
      children: [
        Text(pre, style: TextStyle(color: baseColor)),
        Text('BED AT', style: TextStyle(color: baseColor)),
        Text(
          '${_formatTime(time)}',
          style: TextStyle(color: Colors.white),
        )
      ],
    );
  }

  String _formatTime(int time) {
    if (time == 0 || time == null) {
      return '00:00';
    }
    var hours = time ~/ 12;
    var minutes = (time % 12) * 5;
    return '$hours:$minutes';
  }

  String _formatIntervalTime(int init, int end) {
    var sleepTime = end > init ? end - init : 288 - init + end;
    var hours = sleepTime ~/ 12;
    var minutes = (sleepTime % 12) * 5;
    return '${hours}h${minutes}m';
  }

  int _generateRandomTime() => Random().nextInt(288);
}

Sleep Slider

flutter-circular-slider's People

Contributors

davidanaya avatar kirkeaton avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

flutter-circular-slider's Issues

Bug have to be fixed

So simply the bug happen when the start is at 0 and stays in 0 and you drag the end to 0 the slider disappear

「onSelectionEnd」 actually holds three parameters, what does each parameter mean?

Question

Nice to meet you. I am new to Dart and want to make good use of this library.

There is one thing I want to ask.

Although onSelectionEnd is commented that its definition takes a function with a parameter of (int init, int end) as an argument, it is actually a function that takes three parameters.

I would like you to teach me the role of each parameter. Thank you.

My usage

Center(
  child: SingleCircularSlider(
    12,
    3,
    handlerColor: Colors.black,
    onSelectionEnd: (a, b, c) {
         // I do not figure out what a, b, c parameters mean.
    },
    baseColor: Colors.grey.withOpacity(0.5),
    selectionColor: Colors.deepPurple,
  ),
),

スクリーンショット 2019-12-30 15 08 50

Multiple instances of circular silder

Hello,

I am trying to use circular slider with many instances. I am creating different objects but every time the position does not change even though I am setting it with a different value.

Is it possible to use your widget with multiple instances?

Thanks.

A question rather than an issue.

Is there s way to customize sectors width, height and color?
Also - is there a way change appearance of handlers, like using your own widgets?

base_painter.dart needs more customization

The section lines can be more customizable and I wish for that right now

https://github.com/davidanaya/flutter-circular-slider/blob/master/lib/src/base_painter.dart#L37-L43

    if (primarySectors > 0) {
      _paintSectors(primarySectors, 8.0, selectionColor, canvas);
    }

    if (secondarySectors > 0) {
      _paintSectors(secondarySectors, 6.0, baseColor, canvas);
    }

the radiusPadding can be made customizable and the secondarySector should not use the baseColor, but rather allow a custom one and default to baseColor

Limiting the range

Any tip on how would you implement constraints like:

  • defining MIN and MAX allowed values (at the beginning and at the end of a circle)
  • constraining the range so that 'init' value is always less than 'end' value
  • constraining all range values not to exceed [MIN, MAX] range (meaning disabling 'laps')

For example, if a use case would be selecting time range in a single day, there is no obvious way how to achieve this.

Thanks!

Null safety

It's a must-have for any modern flutter library

base_painter.dart needs more customization

The section lines can be more customizable and I wish for that right now

https://github.com/davidanaya/flutter-circular-slider/blob/master/lib/src/base_painter.dart#L37-L43

    if (primarySectors > 0) {
      _paintSectors(primarySectors, 8.0, selectionColor, canvas);
    }

    if (secondarySectors > 0) {
      _paintSectors(secondarySectors, 6.0, baseColor, canvas);
    }```

the `radiusPadding` can be made customizable and the secondarySector should not use the baseColor, but rather allow a custom one and default to baseColor

What if I want one side drag ?

I want that the drag happens from one side so always the init is 0 and you can't change it and only one circle to drag ?

onSelectionChange is not providing updated values for initTime and endTime

2019-08-22_13-51-31

DoubleCircularSlider(
  48,
  0,
  10,
  height: 230.0,
  width: 230.0,
  primarySectors: 4,
  secondarySectors: 12,
  baseColor: Color.fromRGBO(255, 255, 255, 0.1),
  selectionColor: Theme.of(context).primaryColor,
  handlerColor: Color(0xFFEEEEEE),
  onSelectionChange: (int initTime, int endTime, int laps) {
    setState(() {
      this.initTime = initTime;
      this.endTime = endTime;
    });
  },
  child: Padding(
    padding: const EdgeInsets.all(42.0),
    child: Center(
        child: Text('${_formatIntervalTime(initTime, endTime)}',
            style: TextStyle(fontSize: 36.0, color: Colors.white))),
  ),
),

Hot reloading properly updates the state (updates the time)

Click instead of dragging the slider

Hi,
Great work on the sliders. Just a quick query, lets say there is only one handler, then instead of dragging, is it possible to click somewhere on the circle and the handler to move there. This is similar to how linear slider in flutter would work. Thanks!

Feature request: Set initial value of laps

Hi David, great package! Can you please add the ability to set an initial lap value? I am using your slider to create values, lets say in increments of 200, and saving both the total value and the laps. If the value is 500, I would have laps = 2, and the slider would be at 50%. When I initialize the slider at another time, I want the slider to know that the lap count is 2...not 0. So when I decrement the slider it allows me to reset the value from 500 back to 0. Without a lap initialized, I cannot decrement back to 0. Thank you!

Is there any way to create half circle?

I want to make a design like speedOmeter with the handle and same dragging event like yours. Can you please help me to build that? Or suggest the way to build half circle?

how to set limits/control values?

Hi, would be great to be able to use onSelectionChange to control/validate the values (thus enabling user logic like max interval size or similar...).

Impossible to change slider position from code

I need to change slider position not only by user interaction, but from code as well. My build() function returns this widget:

FractionallySizedBox(
      widthFactor: 0.5,
      child: Container(
        color: Colors.redAccent,
        child: SingleCircularSlider(
            255,
            entity.brightness ?? 0
         ),
      ),
    )

Changing the value of entity.brightness and rebuilding the widget didn't change it's position.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.