Wheel In Flutter

Wheel In Flutter :

Screenshot :

Wheel In Flutter

Model Package :

model.dart

import 'dart:ui';

class Luck {
  final String image;
  final Color color;
  final String point;

  Luck(this.image, this.color, this.point);

  String get asset => "assets/images/$image.png";
}

widget Package :

arrow_view.dart

import 'dart:math';

import 'package:flutter/material.dart';

class ArrowView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Align(
        alignment: Alignment.center,
        child: Transform.rotate(
            angle: pi,
            child: Padding(
                padding: EdgeInsets.only(top: 80),
                child: ClipPath(
                    clipper: _ArrowClipper(),
                    child: Container(
                        decoration: BoxDecoration(
                            gradient: LinearGradient(
                                begin: Alignment.topCenter,
                                end: Alignment.bottomCenter,
                                colors: [Colors.black12, Colors.black])),
                        height: 40,
                        width: 40)))));
  }
}

class _ArrowClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    Path _path = Path();
    Offset _center = size.center(Offset.zero);
    _path.lineTo(_center.dx, size.height);
    _path.lineTo(size.width, 0);
    _path.lineTo(_center.dx, _center.dy);
    _path.close();
    print("bitti");
    return _path;
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) {
    return false;
  }
}

board_view.dart

import 'dart:math';
import 'package:flutter/material.dart';
import 'package:wheel/model/model.dart';
import 'package:wheel/widget/arrow_view.dart';

class BoardView extends StatefulWidget {
  final double angle;
  final double current;
  final List<Luck> items;

  const BoardView({Key key, this.angle, this.current, this.items})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _BoardViewState();
  }
}

class _BoardViewState extends State<BoardView> {
  Size get size => Size(MediaQuery.of(context).size.width * 0.8,
      MediaQuery.of(context).size.width * 0.8);

  double _rotote(int index) => (index / widget.items.length) * 2 * pi;

  @override
  Widget build(BuildContext context) {
    return Stack(alignment: Alignment.center, children: <Widget>[
      //shadow
      Container(
          height: size.height,
          width: size.width,
          decoration: BoxDecoration(
              shape: BoxShape.circle,
              boxShadow: [BoxShadow(blurRadius: 20, color: Colors.black38)])),
      Transform.rotate(
          angle: -(widget.current + widget.angle) * 2 * pi,
          child: Stack(alignment: Alignment.center, children: <Widget>[
            for (var luck in widget.items) ...[_buildCard(luck)],
            for (var luck in widget.items) ...[_buildImage(luck)]
          ])),
      Container(height: size.height, width: size.width, child: ArrowView())
    ]);
  }

  _buildCard(Luck luck) {
    var _rotate = _rotote(widget.items.indexOf(luck));
    var _angle = 2 * pi / widget.items.length;
    return Transform.rotate(
        angle: _rotate,
        child: ClipPath(
            clipper: _LuckPath(_angle),
            child: Container(
                height: size.height,
                width: size.width,
                decoration: BoxDecoration(
                    gradient: LinearGradient(
                        begin: Alignment.topCenter,
                        end: Alignment.bottomCenter,
                        colors: [luck.color, luck.color.withOpacity(0)])))));
  }

  _buildImage(Luck luck) {
    var _rotate = _rotote(widget.items.indexOf(luck));
    return Transform.rotate(
        angle: _rotate,
        child: Container(
            height: size.height,
            width: size.width,
            alignment: Alignment.topCenter,
            child: ConstrainedBox(
                constraints:
                    BoxConstraints.expand(height: size.height / 3, width: 44),
                child: Padding(
                    padding: const EdgeInsets.only(top: 24.0),
                    child: Column(children: <Widget>[
                      Icon(Icons.attach_money, color: Colors.amberAccent),
                      SizedBox(height: 4),
                      Text(luck.point.toString(),
                          style: TextStyle(
                              fontSize: 16,
                              color: Colors.white,
                              fontWeight: FontWeight.w600))
                    ])))));
  }
}

class _LuckPath extends CustomClipper<Path> {
  final double angle;

  _LuckPath(this.angle);

  @override
  Path getClip(Size size) {
    Path _path = Path();
    Offset _center = size.center(Offset.zero);
    Rect _rect = Rect.fromCircle(center: _center, radius: size.width / 2);
    _path.moveTo(_center.dx, _center.dy);
    _path.arcTo(_rect, -pi / 2 - angle / 2, angle, false);
    _path.close();

    return _path;
  }

  @override
  bool shouldReclip(_LuckPath oldClipper) {
    return angle != oldClipper.angle;
  }
}

main.dart

import 'package:flutter/material.dart';
import 'package:wheel/whell.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity),
        home: MyHomePage());
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(body: WhellFortune());
  }
}

whell.dart

import 'dart:math';
import 'package:flutter/material.dart';
import 'package:wheel/model/model.dart';
import 'package:wheel/widget/board_view.dart';

class WhellFortune extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _WhellFortuneState();
  }
}

class _WhellFortuneState extends State<WhellFortune>
    with SingleTickerProviderStateMixin {
  double _angle = 0;
  double _current = 0;
  AnimationController _ctrl;
  Animation _ani;

  List<Luck> _items = [
    Luck("apple", Color(0xFF9F6083), "10"),
    Luck("raspberry", Color(0xFFFDB78B), "30"),
    Luck("grapes", Color(0xFF57CFE2), "45"),
    Luck("fruit", Color(0xFF606B7E), "75"),
    Luck("milk", Color(0xFF24ACE9), "150"),
    Luck("salad", Color(0xFFFB7C7A), "250"),
    Luck("cheese", Color(0xFF1BD3AC), "500"),
    Luck("carrot", Color(0xFFa73737), "1000")
  ];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    var _duration = Duration(milliseconds: 5000);
    _ctrl = AnimationController(vsync: this, duration: _duration);
    _ani = CurvedAnimation(parent: _ctrl, curve: Curves.fastLinearToSlowEaseIn);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
            decoration: BoxDecoration(
                gradient: LinearGradient(
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    colors: [Color(0xCCf857a6), Color(0xCCff5858)])),
            child: AnimatedBuilder(
                animation: _ani,
                builder: (context, child) {
                  final _value = _ani.value;
                  final _angle = _value * this._angle;
                  return Stack(alignment: Alignment.center, children: <Widget>[
                    Align(
                        alignment: Alignment.topCenter,
                        child: Padding(
                            padding: const EdgeInsets.only(top: 36.0),
                            child: Text("Quick opportunity",
                                style: TextStyle(
                                    fontSize: 18, color: Colors.white)))),
                    BoardView(items: _items, current: _current, angle: _angle),
                    _buildGo(),
                    _buildResult(_value)
                  ]);
                })));
  }

  _buildGo() {
    return Material(
        color: Colors.white,
        shape: CircleBorder(),
        child: InkWell(
          customBorder: CircleBorder(),
          child: Container(
              alignment: Alignment.center,
              height: 84,
              width: 84,
              child: Text("START",
                  style:
                      TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold))),
          onTap: _animation,
        ));
  }

  _animation() {
    if (!_ctrl.isAnimating) {
      var _random = Random().nextDouble();
      _angle = 20 + Random().nextInt(5) + _random;
      _ctrl.forward(from: 0.0).then((_) {
        _current = (_current + _random);
        _current = _current - _current ~/ 1;

        print('Dialog or navigation');

        _ctrl.reset();
      });
    }
  }

  int _calIndex(value) {
    var _base = (2 * pi / _items.length / 2) / (2 * pi);
    return (((_base + value) % 1) * _items.length).floor();
  }

  _buildResult(_value) {
    var _index = _calIndex(_value * _angle + _current);

    return Padding(
        padding: EdgeInsets.symmetric(vertical: 48.0),
        child: Align(
            alignment: Alignment.bottomCenter,
            child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Icon(Icons.attach_money, color: Colors.amber),
                  SizedBox(width: 4),
                  Text(_items[_index].point,
                      style: TextStyle(
                          fontSize: 22,
                          color: Colors.amber,
                          fontWeight: FontWeight.w600))
                ])));
  }
}

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit www.fluttertutorial.in