当前位置:网站首页>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.
边栏推荐
猜你喜欢
nacos-高可用seata之TC搭建(02)
Compilation et connexion de shader dans games202 - webgl (comprendre la direction)
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
图论的扩展
The IPO of mesk Electronics was terminated: Henan assets, which was once intended to raise 800 million yuan, was a shareholder
Flody的应用
Microblogging hot search stock selection strategy
从0到1建设智能灰度数据体系:以vivo游戏中心为例
ByteDance program yuan teaches you how to brush algorithm questions: I'm not afraid of the interviewer tearing the code
JS quick start (II)
随机推荐
饼干(考试版)
趋势前沿 | 达摩院语音 AI 最新技术大全
Introduction of several RS485 isolated communication schemes
Idea one key guide package
关于es8316的音频爆破音的解决
Upload nestjs configuration files, configure the use of middleware and pipelines
The web project imported the MySQL driver jar package but failed to load it into the driver
Leetcode 186 Flip the word II in the string (2022.07.05)
acwing周赛58
Quatre méthodes de redis pour dépanner les grandes clés sont nécessaires pour optimiser
[lgr-109] Luogu may race II & windy round 6
Basic knowledge and examples of binary tree
Fiddler installed the certificate, or prompted that the certificate is invalid
[noip2008 improvement group] stupid monkey
Biscuits (examination version)
Hyperledger Fabric2. Some basic concepts of X (1)
最高法院,离婚案件判决标准
Fuzzy -- basic application method of AFL
Please wait while Jenkins is getting ready to work
February 12 relativelayout