【问题标题】:How to make custom loading widget only using flutter如何仅使用颤振制作自定义加载小部件
【发布时间】:2021-09-01 01:07:04
【问题描述】:

我正在尝试制作一个像这样的自定义动画进度指示器

但我不知道从哪里开始..

感谢您的帮助!

编辑:仅在可能的情况下使用颤振

【问题讨论】:

  • 我更喜欢河。或者 customPainter 可以在这种情况下提供帮助。

标签: flutter flutter-animation


【解决方案1】:

您可以像这样使用 ClipPath 和 CustomPaint:

import 'package:flutter/material.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Home(),
    );
  }
}

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

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

class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
  late AnimationController controller;

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

    controller = AnimationController(duration: const Duration(milliseconds: 1000), vsync: this);
    controller.addStatusListener(statusListener);
    controller.forward();
  }

  statusListener(AnimationStatus status) {
    if (status == AnimationStatus.completed) {
      controller.value = 0.0;
      controller.forward();
    }
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
          child: Container(
        width: 200,
        height: 200,
        color: Colors.blue,
        child: Center(
          child: AnimatedBuilder(
              animation: controller.view,
              builder: (context, snapshot) {
                return ClipPath(
                  clipper: MyClipper(),
                  child: Container(
                    width: 100,
                    height: 100,
                    color: Colors.white,
                    child: CustomPaint(
                      painter: MyCustomPainter(controller.value),
                    ),
                  ),
                );
              }),
        ),
      )),
    );
  }
}

class MyClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    double w = size.width;
    double h = size.height;

    Path path = Path();
    path.moveTo(w / 2, 0);
    path.lineTo(w / 2 - 20, 55);
    path.lineTo(w / 2, 55);
    path.lineTo(w / 2 - 20, h);
    path.lineTo(w / 2 + 20, 45);
    path.lineTo(w / 2, 45);
    path.lineTo(w / 2 + 20, 0);
    path.close();
    return path;
  }

  @override
  bool shouldReclip(covariant CustomClipper oldClipper) {
    return false;
  }
}

class MyCustomPainter extends CustomPainter {
  final double percentage;

  MyCustomPainter(this.percentage);
  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..color = Colors.amber
      ..style = PaintingStyle.fill;

    Rect rect = Rect.fromLTWH(0, 0, size.width, size.height * percentage);

    canvas.drawRect(rect, paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}

【讨论】:

  • 嗨@Develocode 777 你能告诉我如何循环这个值吗?因为我只是一个不断移动的旋转器谢谢!!
  • 嗨,我编辑了我的答案以进行加载循环。
  • 非常感谢!!将对此进行研究:)
  • 很高兴能帮上忙!
【解决方案2】:

我不知道你可以控制lotti资产和包。
https://pub.dev/packages/lottie

使用lotti资产,您可以制作如下所示的加载指示页面。

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lottie/lottie.dart';

class LoadingIndicatorPage extends StatelessWidget {
  final Color backgroundColor;
  LoadingIndicatorPage({this.backgroundColor});

  @override
  Widget build(BuildContext context) => Container(
        color: backgroundColor ?? Colors.transparent,
        child: Center(
          child: Lottie.asset(
            'assets/loading_indicator3.zip',
            width: ScreenUtil().setWidth(64),
            height: ScreenUtil().setWidth(64),
          ),
        ),
      );
}

【讨论】:

    猜你喜欢
    • 2020-07-27
    • 2019-05-10
    • 2021-08-19
    • 2020-09-07
    • 2020-10-13
    • 2018-09-13
    • 1970-01-01
    • 1970-01-01
    • 2019-12-20
    相关资源
    最近更新 更多