flutter - 如何在flutter中创建如下圆形动画

标签 flutter dart flutter-layout flutter-animation

  1. 黑暗部分持续循环
  2. 深色部分的大小与加载的数据量成正比。

示例:- 当加载 40% 数据时,深色循环部分为圆周的 40%。当加载 60% 数据时,暗循环部分为圆周的 60%。依此类推。

https://youtu.be/a4czRLK6ouE

enter image description here

最佳答案

这是我创建此动画的尝试:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: HomePage(),
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

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

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
  late final AnimationController controller;
  double percent = 0.0;

    // ** Addition in @mmcdon20 code - Start

  changePercent() {
    // 20 frame is enough to beat eye, that's why I used
    // 50 refresh/second to keep animation smooth
    Future.delayed(
      const Duration(milliseconds: 20), // Adjust accordingly.
      () {
        setState(() {
          percent += 0.005; // Adjust accordingly.
        });
        print('........................');
        if (percent < 1) {
          changePercent();
        }
      },
    );
  }
  // ** Addition in @mmcdon20 code - End

  @override
  void initState() {
    super.initState();
    controller = AnimationController(
      duration: const Duration(seconds: 2),
      vsync: this,
    )
      ..animateTo(1)
      ..repeat();

    // ** Addition in @mmcdon20 code - Start
    changePercent();
    // ** Addition in @mmcdon20 code - End

  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          CustomProgressIndicator(
            value: percent,
            color: Colors.orange,
            controller: controller,
            width: 200,
            height: 200,
            strokeWidth: 10,
            // curve: Curves.slowMiddle, // ** Eliminationated from @mmcdon20 code
          ),
    

          // ** Eliminationated from @mmcdon20 code - Start
          // Slider(
          //   value: percent,
          //   onChanged: (v) {
          //     setState(() {
          //       percent = v;
          //     });
          //   },
          // ),          
          // ** Eliminationated from @mmcdon20 code - End
        ],
      ),
    );
  }
}

class CustomProgressIndicator extends StatelessWidget {
  const CustomProgressIndicator({
    Key? key,
    required this.color,
    required this.value,
    required this.controller,
    required this.width,
    required this.height,
    required this.strokeWidth,
    this.curve = Curves.linear,
  }) : super(key: key);
  final Color color;
  final double value;
  final AnimationController controller;
  final double width;
  final double height;
  final double strokeWidth;
  final Curve curve;

  @override
  Widget build(BuildContext context) {
    return RotationTransition(
      turns: CurvedAnimation(
        parent: controller,
        curve: curve,
      ),
      child: SizedBox(
        width: width,
        height: height,
        child: CircularProgressIndicator(
          strokeWidth: strokeWidth,
          color: color,
          backgroundColor: color.withOpacity(.2),
          value: value,
        ),
      ),
    );
  }
}

关于flutter - 如何在flutter中创建如下圆形动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70484168/

相关文章:

flutter - 如何在点击检测中跳过 Flutter Widget?

Flutter googleMap onTap 长按

flutter - Flutter-如何在没有互联网的情况下从坐标获取国家/地区?

flutter - 在 Flutter 的平板电脑上制作固定的应用范围菜单而不是 Drawer

flutter - 如何在Flutter中编辑列表中的选定项目

firebase - 如何在 Flutter 中使用查询访问 Cloud Firestore 的数组索引?

ios - Flutter 运行 - 无法构建 iOS 应用程序 - 命令 PhaseScriptExecution 失败,退出代码非零

dart - 尝试从列表中删除对象时迭代期间的并发修改

dart - 有没有人尝试过在 Dart 中使用 Firebase javascript 库?

flutter - FadeInImage onFocusChange 重新生成