/*
 * @Descripttion: 
 * @version: 
 * @Author: guanxiaoxin
 * @Date: 2023-10-24 16:56:11
 * @LastEditors: guanxiaoxin
 * @LastEditTime: 2023-10-25 14:04:47
 * @FilePath: \VNoteApp\lib\components\floating_window\index.dart
 */
import 'dart:math';

import 'package:flutter/material.dart';

class DemoFlowMenu extends StatefulWidget {
  const DemoFlowMenu({super.key});

  @override
  _DemoFlowMenuState createState() => _DemoFlowMenuState();
}

class _DemoFlowMenuState extends State<DemoFlowMenu>
    with TickerProviderStateMixin {
  //动画须要这个类来混合
  //动画变量,以及初始化和销毁
  late AnimationController _ctrlAnimationCircle;

  @override
  void initState() {
    super.initState();
    _ctrlAnimationCircle = AnimationController(
        //初始化动画变量
        lowerBound: 0,
        upperBound: 80,
        duration: const Duration(milliseconds: 300),
        vsync: this);
    _ctrlAnimationCircle.addListener(() => setState(() {}));
  }

  @override
  void dispose() {
    _ctrlAnimationCircle.dispose(); //销毁变量,释放资源
    super.dispose();
  }

  //生成Flow的数据
  List<Widget> _buildFlowChildren() {
    return List.generate(
      5,
      (index) => Container(
        child: Icon(
          index.isEven ? Icons.timer : Icons.ac_unit,
          color: Colors.primaries[index % Colors.primaries.length],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        width: 100,
        height: 100,
        decoration: const BoxDecoration(
          borderRadius: BorderRadius.all(
            Radius.circular(
              50,
            ),
          ),
        ),
        child: Material(
          borderRadius: const BorderRadius.all(
            Radius.circular(
              50,
            ),
          ),
          color: Colors.black.withOpacity(0.5),
          child: Center(
            child: Stack(
              children: [
                ClipOval(
                  child: Opacity(
                    opacity: 0.7,
                    child: Image.asset(
                      'assets/images/avatar_man.png',
                      fit: BoxFit.fitWidth,
                    ),
                  ),
                ),
                const Align(
                  alignment: Alignment.bottomCenter,
                  child: Text(
                    '白金浩',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 24,
                    ),
                  ),
                )
              ],
            ),
          ),
        )
        // Stack(
        //   children: <Widget>[
        //     Positioned.fill(
        //       child: Flow(
        //         delegate: FlowAnimatedCircle(_ctrlAnimationCircle.value),
        //         children: _buildFlowChildren(),
        //       ),
        //     ),
        //     Positioned.fill(
        //       child: IconButton(
        //         icon: const Icon(Icons.menu),
        //         onPressed: () {
        //           setState(() {
        //             //点击后让动画可前行或回退
        //             _ctrlAnimationCircle.status == AnimationStatus.completed
        //                 ? _ctrlAnimationCircle.reverse()
        //                 : _ctrlAnimationCircle.forward();
        //           });
        //         },
        //       ),
        //     ),
        //   ],
        // ),
        );
  }
}

class FlowAnimatedCircle extends FlowDelegate {
  final double radius; //绑定半径,让圆动起来
  FlowAnimatedCircle(this.radius);

  @override
  void paintChildren(FlowPaintingContext context) {
    if (radius == 0) {
      return;
    }
    double x = 0; //开始(0,0)在父组件的中心
    double y = 0;
    for (int i = 0; i < context.childCount; i++) {
      x = radius * sin(i * pi / (context.childCount - 1)); //根据数学得出坐标
      y = radius * cos(i * pi / (context.childCount - 1)); //根据数学得出坐标
      context.paintChild(i, transform: Matrix4.translationValues(-x, y, 0));
    } //使用Matrix定位每一个子组件
  }

  @override
  bool shouldRepaint(FlowDelegate oldDelegate) => true;
}
