日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

pythoni屏幕连点_【Flutter组件】仿抖音双击点赞弹出爱心效果(可连点)

發布時間:2023/12/2 python 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pythoni屏幕连点_【Flutter组件】仿抖音双击点赞弹出爱心效果(可连点) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

效果

簡介

仿抖音點贊手勢,單擊暫停,雙擊點贊,可連續點擊添加多個愛心,特點如下

全部效果為代碼繪制(愛心圖標來自Material Icon的圖標)

套上在目標Widget外即可使用

提供單擊與點贊的回調

建議復制代碼使用,動畫可按需修改

沒有flutter之外的依賴項,可復制使用(懶得發pub)

基本原理

一個罩在child上的stack層,雙擊后根據坐標添加目標愛心Widget,愛心Widget在出現時會播放動畫,用坐標作為key,動畫結束后移除已經消失的愛心。

全部代碼

什么都不依賴,可以直接復制使用。

import 'dart:async';

import 'dart:math';

import 'package:flutter/material.dart';

/// 視頻手勢封裝

/// 單擊:暫停

/// 雙擊:點贊,雙擊后再次單擊也是增加點贊愛心

class TikTokVideoGesture extends StatefulWidget {

const TikTokVideoGesture({

Key key,

@required this.child,

this.onAddFavorite,

this.onSingleTap,

}) : super(key: key);

final Function onAddFavorite;

final Function onSingleTap;

final Widget child;

@override

_TikTokVideoGestureState createState() => _TikTokVideoGestureState();

}

class _TikTokVideoGestureState extends State {

GlobalKey _key = GlobalKey();

// 內部轉換坐標點

Offset _p(Offset p) {

RenderBox getBox = _key.currentContext.findRenderObject();

return getBox.globalToLocal(p);

}

List icons = [];

bool canAddFavorite = false;

bool justAddFavorite = false;

Timer timer;

@override

Widget build(BuildContext context) {

var iconStack = Stack(

children: icons

.map(

(p) => TikTokFavoriteAnimationIcon(

key: Key(p.toString()),

position: p,

onAnimationComplete: () {

icons.remove(p);

},

),

)

.toList(),

);

return GestureDetector(

key: _key,

onTapDown: (detail) {

setState(() {

if (canAddFavorite) {

print('添加愛心,當前愛心數量:${icons.length}');

icons.add(_p(detail.globalPosition));

widget.onAddFavorite?.call();

justAddFavorite = true;

} else {

justAddFavorite = false;

}

});

},

onTapUp: (detail) {

timer?.cancel();

var delay = canAddFavorite ? 1200 : 600;

timer = Timer(Duration(milliseconds: delay), () {

canAddFavorite = false;

timer = null;

if (!justAddFavorite) {

widget.onSingleTap?.call();

}

});

canAddFavorite = true;

},

onTapCancel: () {

print('onTapCancel');

},

child: Stack(

children: [

widget.child,

iconStack,

],

),

);

}

}

class TikTokFavoriteAnimationIcon extends StatefulWidget {

final Offset position;

final double size;

final Function onAnimationComplete;

const TikTokFavoriteAnimationIcon({

Key key,

this.onAnimationComplete,

this.position,

this.size: 100,

}) : super(key: key);

@override

_TikTokFavoriteAnimationIconState createState() =>

_TikTokFavoriteAnimationIconState();

}

class _TikTokFavoriteAnimationIconState

extends State with TickerProviderStateMixin {

AnimationController _animationController;

@override

void dispose() {

_animationController?.dispose();

super.dispose();

}

@override

void didChangeDependencies() {

print('didChangeDependencies');

super.didChangeDependencies();

}

@override

void initState() {

_animationController = AnimationController(

lowerBound: 0,

upperBound: 1,

duration: Duration(milliseconds: 1600),

vsync: this,

);

_animationController.addListener(() {

setState(() {});

});

startAnimation();

super.initState();

}

startAnimation() async {

await _animationController.forward();

widget.onAnimationComplete?.call();

}

double rotate = pi / 10.0 * (2 * Random().nextDouble() - 1);

double get value => _animationController?.value;

double appearDuration = 0.1;

double dismissDuration = 0.8;

double get opa {

if (value < appearDuration) {

return 0.99 / appearDuration * value;

}

if (value < dismissDuration) {

return 0.99;

}

var res = 0.99 - (value - dismissDuration) / (1 - dismissDuration);

return res < 0 ? 0 : res;

}

double get scale {

if (value < appearDuration) {

return 1 + appearDuration - value;

}

if (value < dismissDuration) {

return 1;

}

return (value - dismissDuration) / (1 - dismissDuration) + 1;

}

@override

Widget build(BuildContext context) {

Widget content = Icon(

Icons.favorite,

size: widget.size,

color: Colors.redAccent,

);

content = ShaderMask(

child: content,

blendMode: BlendMode.srcATop,

shaderCallback: (Rect bounds) => RadialGradient(

center: Alignment.topLeft.add(Alignment(0.66, 0.66)),

colors: [

Color(0xffEF6F6F),

Color(0xffF03E3E),

],

).createShader(bounds),

);

Widget body = Transform.rotate(

angle: rotate,

child: Opacity(

opacity: opa,

child: Transform.scale(

alignment: Alignment.bottomCenter,

scale: scale,

child: content,

),

),

);

return widget.position == null

? Container()

: Positioned(

left: widget.position.dx - widget.size / 2,

top: widget.position.dy - widget.size / 2,

child: body,

);

}

}

結語

作者:馬嘉倫

日期:2020/01/31

平臺:Segmentfault獨家,勿轉載

總結

以上是生活随笔為你收集整理的pythoni屏幕连点_【Flutter组件】仿抖音双击点赞弹出爱心效果(可连点)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。