当前位置:网站首页>flutter 实现一个有加载动画的按钮(loadingButton)
flutter 实现一个有加载动画的按钮(loadingButton)
2022-07-06 05:01:00 【明似水】
前言
今天分享一个又加载动画的按钮,希望能帮助到你,替换加载图标就可以了。效果图如下:
一、loading组件代码
import "dart:math" as math;
import 'package:flutter/material.dart';
class HBLoadingButton extends StatefulWidget {
bool loading;
String title;
Function callback;
bool isActive; //是否活跃
// 按钮高度
double? height = 40.0;
HBLoadingButton(this.loading, this.title, this.callback, {this.height, this.isActive = true});
@override
_HBLoadingButtonState createState() => _HBLoadingButtonState();
}
class _HBLoadingButtonState extends State<HBLoadingButton> with SingleTickerProviderStateMixin {
AnimationController? _controller;
@override
void initState() {
_controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 100 * 15),
)..repeat();
}
@override
void dispose() {
_controller?.dispose();
super.dispose();
}
Widget _btn() {
return Container(
decoration: widget.isActive
? BoxDecoration(
color: Color(0xFF006CFF),
borderRadius: BorderRadius.circular(8.0),
)
: BoxDecoration(
color: Color(0x1F0C0C1C),
borderRadius: BorderRadius.circular(8.0),
),
height: widget.height,
child: TextButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all(Color(0xFF006CFF)),
),
child: Text(
widget.title,
style: widget.isActive
? const TextStyle(fontSize: 16.0, color: Color(0xFFFFFFFF))
: const TextStyle(fontSize: 16.0, color: Color(0x800C0C1C)),
),
onPressed: () {
widget.callback();
},
),
);
}
Widget _loadingBtn() {
Widget icon = Image.asset(
"lib/common/assets/ic_loading_more_white.png",
width: 16,
height: 16,
);
if (_controller != null) {
icon = AnimatedBuilder(
animation: _controller!,
builder: (_, Widget? child) {
return Transform.rotate(
angle: _controller!.value * 2 * math.pi,
child: child,
);
},
child: icon,
);
}
return Container(
decoration: BoxDecoration(
color: Color(0xFF006CFF),
borderRadius: BorderRadius.circular(8.0),
),
height: widget.height,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
"加载中。。。",
style: TextStyle(color: Color(0xFFFFFFFF), fontSize: 14.0),
),
Container(
margin: const EdgeInsets.only(left: 6.0),
child: icon,
),
],
),
);
}
@override
Widget build(BuildContext context) {
return widget.loading ? _loadingBtn() : _btn();
}
}
二、使用代码
import 'package:flutter/material.dart';
import 'package:flutter_hotsmeta/module/beginner_guide/page/hb_loading_button.dart';
class HBLoadingPage extends StatefulWidget {
const HBLoadingPage({Key? key}) : super(key: key);
@override
State<HBLoadingPage> createState() => _HBLoadingPageState();
}
class _HBLoadingPageState extends State<HBLoadingPage> {
bool requesting = false;
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
title: Text("测试加载按钮"),
),
body: _buildMainWidget(context),
);
}
_buildMainWidget(BuildContext context) {
return Center(
child: SizedBox(
width: 291.0,
height: 40.0,
child: HBLoadingButton(
requesting,
"确定",
_beforeConfirm,
),
),
);
}
_beforeConfirm() {
setState(() {
requesting = true;
});
Future.delayed(const Duration(milliseconds: 5000), () {
setState(() {
requesting = false;
});
});
}
}
END.
边栏推荐
- 麦斯克电子IPO被终止:曾拟募资8亿 河南资产是股东
- Microblogging hot search stock selection strategy
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- Postman前置脚本-全局变量和环境变量
- [FreeRTOS interrupt experiment]
- Summary of redis AOF and RDB knowledge points
- Compilation et connexion de shader dans games202 - webgl (comprendre la direction)
- Oracle deletes duplicate data, leaving only one
- Zynq learning notes (3) - partial reconfiguration
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
猜你喜欢
Basic knowledge and examples of binary tree
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
Leetcode dynamic planning day 16
RT thread analysis - object container implementation and function
Yyds dry inventory SSH Remote Connection introduction
CUDA11.1在线安装
几种RS485隔离通讯的方案介绍
F12 solve the problem that web pages cannot be copied
GAMES202-WebGL中shader的编译和连接(了解向)
yolov5 tensorrt加速
随机推荐
团队协作出了问题,项目经理怎么办?
F12 solve the problem that web pages cannot be copied
EditorUtility. The role and application of setdirty in untiy
Review of double pointer problems
Chip debugging of es8316 of imx8mp
比尔·盖茨晒18岁个人简历,48年前期望年薪1.2万美元
Pagoda configuration mongodb
MySQL if and ifnull use
驱动开发——HelloWDM驱动
[mathematical modeling] differential equation -- sustainable development of fishing industry
RT thread analysis - object container implementation and function
麥斯克電子IPO被終止:曾擬募資8億 河南資產是股東
Cve-2019-11043 (PHP Remote Code Execution Vulnerability)
[FreeRTOS interrupt experiment]
Sliding window problem review
Extension of graph theory
组播和广播的知识点梳理
Simple understanding of interpreters and compilers
Programmers' position in the Internet industry | daily anecdotes
树莓派3.5寸屏幕白屏显示连接