最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Flutter投票組件使用方法詳解

 更新時(shí)間:2022年08月24日 09:09:37   作者:懷君  
這篇文章主要為大家詳細(xì)介紹了Flutter投票組件的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Flutter投票組件的使用方法,供大家參考,具體內(nèi)容如下

前景

基于公司項(xiàng)目需求,仿照微博實(shí)現(xiàn)投票功能。

開發(fā)遇到的問題

1.選項(xiàng)列表的高度,自適應(yīng)的問題;
2.進(jìn)度條動(dòng)畫的問題;
3.列表回收機(jī)制,導(dǎo)致進(jìn)度條動(dòng)畫重復(fù);
4.自定義進(jìn)度條四周圓角;

如何解決問題

  • 拿到數(shù)組列表最長的數(shù)據(jù),然后根據(jù)屏幕寬度計(jì)算,超出一行則設(shè)定兩行高度,否則使用一行的高度;
_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) {
? ? //設(shè)置item選項(xiàng)的高度
? ? ? _itemHeight = 100.w;
? ? }
? }
  • Widget控件初始化(initState)方法時(shí),使用AnimationController動(dòng)畫,并實(shí)現(xiàn)SingleTickerProviderStateMixin,在build方法當(dāng)中調(diào)用 _controller.animateTo()
AnimationController _controller;
? ? _controller = AnimationController(
? ? ? vsync: this,
? ? ? duration: const Duration(seconds: 1),
? ? )..addListener(() {
? ? ? ? setState(() {});
? ? ? });
//觸發(fā)動(dòng)畫,執(zhí)行的位置
_controller.animateTo()
  • 在列表數(shù)據(jù)當(dāng)中給動(dòng)畫標(biāo)記字段,讓其是否執(zhí)行動(dòng)畫;當(dāng)用戶投票成功,改變狀態(tài)執(zhí)行進(jìn)度條動(dòng)畫。用戶滑動(dòng)列表之后,將標(biāo)記改為false。關(guān)閉動(dòng)畫效果。
  • 針對修改部分源碼,設(shè)置進(jìn)度條圓角控件;
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) {
? ? ? ///當(dāng)進(jìn)度條為1時(shí),設(shè)置四周圓角
? ? ? ? rRect = RRect.fromRectAndRadius(
? ? ? ? ? ? Offset(0.0, 0.0) & Size(width, size.height), radius);
? ? ? } else {
? ? ? ? ///小于1時(shí),設(shè)置左側(cè)圓角
? ? ? ? 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,
? ? );
? }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Kotlin中的反射機(jī)制深入講解

    Kotlin中的反射機(jī)制深入講解

    反射,簡單來說,是一種在運(yùn)行時(shí)動(dòng)態(tài)地訪問對象屬性和方法的方式,而不需要事先確定這些屬性是什么。下面這篇文章主要給大家介紹了關(guān)于Kotlin中反射機(jī)制的相關(guān)資料,需要的朋友可以參考下
    2018-11-11
  • Android 中ListView點(diǎn)擊Item無響應(yīng)問題的解決辦法

    Android 中ListView點(diǎn)擊Item無響應(yīng)問題的解決辦法

    如果listitem里面包括button或者checkbox等控件,默認(rèn)情況下listitem會(huì)失去焦點(diǎn),導(dǎo)致無法響應(yīng)item的事件,怎么解決呢?下面小編給大家分享下listview點(diǎn)擊item無響應(yīng)的解決辦法
    2016-12-12
  • Android架構(gòu)組件LiveData使用詳解

    Android架構(gòu)組件LiveData使用詳解

    這篇文章主要介紹了Android架構(gòu)組件LiveData使用詳解的相關(guān)資料,需要的朋友可以參考下
    2023-07-07
  • 深入解析android5.1 healthd

    深入解析android5.1 healthd

    這篇文章主要為大家詳細(xì)介紹了android5.1 healthd的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Android編程記錄ListView標(biāo)記行狀態(tài)的方法

    Android編程記錄ListView標(biāo)記行狀態(tài)的方法

    這篇文章主要介紹了Android編程記錄ListView標(biāo)記行狀態(tài)的方法,結(jié)合實(shí)例分析了ListView標(biāo)記的相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • Android實(shí)現(xiàn)鬧鐘小程序

    Android實(shí)現(xiàn)鬧鐘小程序

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)鬧鐘小程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Android BroadcastReceiver廣播注冊方式總結(jié)

    Android BroadcastReceiver廣播注冊方式總結(jié)

    這篇文章主要介紹了Android BroadcastReceiver廣播注冊方式總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • 詳解 WebView 與 JS 交互傳值問題

    詳解 WebView 與 JS 交互傳值問題

    這篇文章主要介紹了詳解 WebView 與 JS 交互傳值問題的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • flutter項(xiàng)目引入iconfont阿里巴巴圖標(biāo)

    flutter項(xiàng)目引入iconfont阿里巴巴圖標(biāo)

    這篇文章主要為大家介紹了flutter項(xiàng)目引入iconfont阿里巴巴圖標(biāo)的過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • 超酷炫的Android碎紙機(jī)效果推薦

    超酷炫的Android碎紙機(jī)效果推薦

    這篇文章運(yùn)用xml和java實(shí)現(xiàn)了Android版的碎紙機(jī)動(dòng)畫,效果非常好,推薦給有需要的小伙伴們使用。
    2016-07-07

最新評論

乐东| 沂南县| 介休市| 桂阳县| 浏阳市| 锡林郭勒盟| 灵台县| 夏津县| 若尔盖县| 乌拉特后旗| 通道| 文成县| 南充市| 石河子市| 蓬莱市| 巩留县| 昔阳县| 江华| 杭锦后旗| 西峡县| 治县。| 瑞金市| 宁津县| 邢台县| 云阳县| 武宣县| 浠水县| 卫辉市| 山东省| 玉树县| 门源| 通化县| 台江县| 仁化县| 公安县| 郓城县| 白水县| 白银市| 宝鸡市| 浦县| 微山县|