Flutter投票组件使用方法详解
作者:怀君 发布时间:2022-05-25 19:07:05
标签:Flutter,投票组件
本文实例为大家分享了Flutter投票组件的使用方法,供大家参考,具体内容如下
前景
基于公司项目需求,仿照微博实现投票功能。
开发遇到的问题
1.选项列表的高度,自适应的问题;
2.进度条动画的问题;
3.列表回收机制,导致进度条动画重复;
4.自定义进度条四周圆角;
如何解决问题
拿到数组列表最长的数据,然后根据屏幕宽度计算,超出一行则设定两行高度,否则使用一行的高度;
_didExceedOneMoreLines(String text, double width, TextStyle style) {
final span = TextSpan(text: text, style: style);
final tp =
TextPainter(text: span, maxLines: 1, textDirection: TextDirection.ltr);
tp.layout(maxWidth: width);
if (tp.didExceedMaxLines) {
//设置item选项的高度
_itemHeight = 100.w;
}
}
Widget控件初始化(initState)方法时,使用AnimationController动画,并实现SingleTickerProviderStateMixin,在build方法当中调用 _controller.animateTo()
AnimationController _controller;
_controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
)..addListener(() {
setState(() {});
});
//触发动画,执行的位置
_controller.animateTo()
在列表数据当中给动画标记字段,让其是否执行动画;当用户投票成功,改变状态执行进度条动画。用户滑动列表之后,将标记改为false。关闭动画效果。
针对修改部分源码,设置进度条圆角控件;
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
class RoundLinearProgressPainter extends ProgressIndicator {
const RoundLinearProgressPainter({
Key key,
double value,
Color backgroundColor,
Color color,
Animation<Color> valueColor,
this.minHeight,
String semanticsLabel,
String semanticsValue,
}) : assert(minHeight == null || minHeight > 0),
super(
key: key,
value: value,
backgroundColor: backgroundColor,
color: color,
valueColor: valueColor,
semanticsLabel: semanticsLabel,
semanticsValue: semanticsValue,
);
final double minHeight;
@override
_RoundLinearProgressPainterState createState() =>
_RoundLinearProgressPainterState();
}
class _RoundLinearProgressPainterState extends State<RoundLinearProgressPainter>
with SingleTickerProviderStateMixin {
AnimationController _controller;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(milliseconds: 1),
vsync: this,
)..addListener(() {
setState(() {});
});
if (widget.value != null) _controller.forward();
}
@override
Widget build(BuildContext context) {
return widget._buildSemanticsWrapper(
context: context,
child: Container(
constraints: BoxConstraints(
minWidth: double.infinity,
minHeight: widget.minHeight ?? 4.0,
),
child: CustomPaint(
painter: _LinearProgressIndicatorPainter(
backgroundColor: widget._getBackgroundColor(context),
valueColor: widget._getValueColor(context),
value: widget.value,
animationValue: _controller.value,
),
),
),
);
}
@override
void didUpdateWidget(RoundLinearProgressPainter oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.value == null && !_controller.isAnimating)
_controller.repeat();
else if (widget.value != null && _controller.isAnimating)
_controller.stop();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
}
class _LinearProgressIndicatorPainter extends CustomPainter {
const _LinearProgressIndicatorPainter({
this.backgroundColor,
this.valueColor,
this.value,
this.animationValue,
});
final Color backgroundColor;
final Color valueColor;
final double value;
final double animationValue;
@override
void paint(Canvas canvas, Size size) {
final Paint paint = Paint()
..color = backgroundColor
..isAntiAlias = true
..style = PaintingStyle.fill;
canvas.drawRect(Offset.zero & size, paint);
paint.color = valueColor;
void drawBar(double x, double width) {
if (width <= 0.0) return;
RRect rRect;
///圆角的宽度
var radius = Radius.circular(8.w);
if (value == 1.0) {
///当进度条为1时,设置四周圆角
rRect = RRect.fromRectAndRadius(
Offset(0.0, 0.0) & Size(width, size.height), radius);
} else {
///小于1时,设置左侧圆角
rRect = RRect.fromRectAndCorners(
Offset(0.0, 0.0) & Size(width, size.height),
topLeft: radius,
bottomLeft: radius);
}
canvas.drawRRect(rRect, paint);
}
if (value != null) {
drawBar(0.0, value.clamp(0.0, 1.0) * size.width);
}
}
@override
bool shouldRepaint(_LinearProgressIndicatorPainter oldPainter) {
return oldPainter.backgroundColor != backgroundColor ||
oldPainter.valueColor != valueColor ||
oldPainter.value != value ||
oldPainter.animationValue != animationValue;
}
}
abstract class ProgressIndicator extends StatefulWidget {
const ProgressIndicator({
Key key,
this.value,
this.backgroundColor,
this.color,
this.valueColor,
this.semanticsLabel,
this.semanticsValue,
}) : super(key: key);
final double value;
final Color backgroundColor;
final Color color;
final Animation<Color> valueColor;
final String semanticsLabel;
final String semanticsValue;
Color _getBackgroundColor(BuildContext context) =>
backgroundColor ?? Theme.of(context).colorScheme.background;
Color _getValueColor(BuildContext context) =>
valueColor?.value ?? color ?? Theme.of(context).colorScheme.primary;
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(PercentProperty('value', value,
showName: false, ifNull: '<indeterminate>'));
}
Widget _buildSemanticsWrapper({
BuildContext context,
Widget child,
}) {
String expandedSemanticsValue = semanticsValue;
if (value != null) {
expandedSemanticsValue ??= '${(value * 100).round()}%';
}
return Semantics(
label: semanticsLabel,
value: expandedSemanticsValue,
child: child,
);
}
}
来源:https://blog.csdn.net/u013290250/article/details/120484975


猜你喜欢
- 引言: 最近公司在做一个教育培训学习及在线考试的项目,本人主要从事网络课程模块,主要做课程分类,课程,课件的创建及
- 1.创建字符串的方法1.1构造方式一、直接构造String str = "fly";方式二 、调用构造方法进行构造对象S
- Spring数据源的配置数据源(连接池)的作用数据源(连接池)是提高程序性能如出现的事先实例化数据源,初始化部分连接资源使用连接资源时从数据
- 前言:OpenFeign 是 Spring 官方推出的一种声明式服务调用和负载均衡组件。它的出现就是为了替代已经进入停更维护状态的 Feig
- 本文实例为大家分享了Android实现简单点赞动画的具体代码,供大家参考,具体内容如下思路1、找到Activity中DecorView的Ro
- 本文实例为大家分享了Java从服务端下载Excel模板文件的具体实现代码,供大家参考,具体内容如下方法一 (2021年01月更新)生成exc
- Jedis简介实际开发中,我们需要用Redis的连接工具连接Redis然后操作Redis,对于主流语言,Redis都提供了对应的客户端;提供
- 现在越来越多的软件都开始使用沉浸式状态栏了,下面总结一下沉浸式状态栏的两种使用方法注意!沉浸式状态栏只支持安卓4.4及以上的版本状态栏:4.
- 一、项目简述功能包括: 仓库管理,出入库管理,仓库人员管理,基本信息管理, 供应商信息,系统管理等等。二、项目运行环境配置: Jdk1.8
- 本文实例讲述了C#使用linq语句查询数组中以特定字符开头元素的方法。分享给大家供大家参考。具体如下:下面的代码查询数组中以字母k开头的元素
- 1.申请测试号,并记录appID和appsecret2.关注测试号3.添加消息模板{{topic.DATA}} 用户名: {{user.DA
- 这个进度条可以反映真实进度,并且完成百分比的文字时随着进度增加而移动的,所在位置也恰好是真实完成的百分比位置,效果如下:思路如下:第一部分是
- JavaWeb项目部署到服务器详细步骤本地准备在eclipse中将项目打成war文件:鼠标右键要部署到服务器上的项目导出项目数据库文件MyS
- 本文实例为大家分享了Android Studio实现进度条效果的具体代码,供大家参考,具体内容如下实验作业 要求一个进度条,进度随机效果图x
- 将通用算法放入具体类(HeapSorter),并将通用算法必须调用的方法定义在接口(HeapSorterHandle)中,从这个接口派生出D
- 作为一个WPF控件开发者,我在工作中经常遇到如本文标题所示的问题。其实,这个问题并不是很难,只是在操作上有些繁琐。本文将尝试对这个问题进行解
- 概述不知道大家在各自项目中是如何写提供代码的commit message, 我们项目有的同事写的很简单,压根不知道提交了什么内容,是新功能还
- 本文实例讲述了Spring实战之Bean定义中的SpEL表达式语言支持操作。分享给大家供大家参考,具体如下:一 配置<?xml ver
- public string NextString(int charLowerBound, int charUpperBound, int l
- Hystrix 是一个帮助解决分布式系统交互时超时处理和容错的类库, 它同样拥有保护系统的能力。Netflix的众多开源项目之一。设计流程: