flutter實現(xiàn)發(fā)送驗證碼功能
更新時間:2022年03月23日 17:08:45 作者:ulddfhv
這篇文章主要為大家詳細介紹了flutter發(fā)送驗證碼功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
一個發(fā)送驗證碼的需求:包括限制文本框輸入長度和只允許輸入數(shù)字
按慣例 先上圖:

class MyBody extends StatefulWidget {
@override
_MyBodyState createState() => _MyBodyState();
}
class _MyBodyState extends State<MyBody> {
bool isButtonEnable=true; //按鈕狀態(tài) 是否可點擊
String buttonText='發(fā)送驗證碼'; //初始文本
int count=60; //初始倒計時時間
Timer timer; //倒計時的計時器
TextEditingController mController=TextEditingController();
void _buttonClickListen(){
setState(() {
if(isButtonEnable){ //當(dāng)按鈕可點擊時
isButtonEnable=false; //按鈕狀態(tài)標(biāo)記
_initTimer();
return null; //返回null按鈕禁止點擊
}else{ //當(dāng)按鈕不可點擊時
// debugPrint('false');
return null; //返回null按鈕禁止點擊
}
});
}
void _initTimer(){
timer = new Timer.periodic(Duration(seconds: 1), (Timer timer) {
count--;
setState(() {
if(count==0){
timer.cancel(); //倒計時結(jié)束取消定時器
isButtonEnable=true; //按鈕可點擊
count=60; //重置時間
buttonText='發(fā)送驗證碼'; //重置按鈕文本
}else{
buttonText='重新發(fā)送($count)'; //更新文本內(nèi)容
}
});
});
}
@override
void dispose() {
timer?.cancel(); //銷毀計時器
timer=null;
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
child: Column(
// mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
color: Colors.white,
padding: EdgeInsets.only(left: 10,right: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
// crossAxisAlignment: CrossAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.ideographic,
children: <Widget>[
Text('驗證碼',style: TextStyle(fontSize: 13,color: Color(0xff333333)),),
Expanded(
child: Padding(padding: EdgeInsets.only(left: 15,right: 15,top: 15),
child: TextFormField(
maxLines: 1,
onSaved: (value) { },
controller: mController,
textAlign: TextAlign.left,
inputFormatters: [WhitelistingTextInputFormatter.digitsOnly,LengthLimitingTextInputFormatter(6)],
decoration: InputDecoration(
hintText: ('填寫驗證碼'),
contentPadding: EdgeInsets.only(top: -5,bottom: 0),
hintStyle: TextStyle(
color: Color(0xff999999),
fontSize: 13,
),
alignLabelWithHint: true,
border: OutlineInputBorder(borderSide: BorderSide.none),
),
),),
),
Container(
width: 120,
child: FlatButton(
disabledColor: Colors.grey.withOpacity(0.1), //按鈕禁用時的顏色
disabledTextColor: Colors.white, //按鈕禁用時的文本顏色
textColor:isButtonEnable?Colors.white:Colors.black.withOpacity(0.2), //文本顏色
color: isButtonEnable?Color(0xff44c5fe):Colors.grey.withOpacity(0.1), //按鈕的顏色
splashColor: isButtonEnable?Colors.white.withOpacity(0.1):Colors.transparent,
shape: StadiumBorder(side: BorderSide.none),
onPressed: (){ setState(() {
_buttonClickListen();
});},
// child: Text('重新發(fā)送 (${secondSy})'),
child: Text('$buttonText',style: TextStyle(fontSize: 13,),),
),
),
],
),
),
Container(
width: double.infinity,
height: 45,
margin: EdgeInsets.only(top: 50,left: 10,right: 10),
child: RaisedButton(
onPressed: () {
debugPrint('${mController.text}');
},
shape: StadiumBorder(side: BorderSide.none),
color: Color(0xff44c5fe),
child: Text(
'下一步',
style: TextStyle(color: Colors.white,fontSize: 15),
),
),
),
],
),
);
}
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
FrameLayout和Fragment處理Android應(yīng)用UI布局實例
這篇文章主要介紹了FrameLayout和Fragment處理Android應(yīng)用UI布局實例,安卓3.0以后Fragment的出現(xiàn)為多尺寸屏幕的適配帶來了方便,需要的朋友可以參考下2016-02-02
Android ContentProvider的實現(xiàn)及簡單實例代碼
這篇文章主要介紹了Android ContentProvider的實現(xiàn)及簡單實例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android中RecyclerView實現(xiàn)滑動刪除與拖拽功能
這篇文章主要使用了RecyclerView的ItemTouchHelper類實現(xiàn)了Item的拖動和刪除功能,ItemTouchHelper是v7包下的一個類,下面來看看詳細的介紹吧,需要的朋友可以參考學(xué)習(xí)。2017-02-02
Android開發(fā)之HTTP訪問網(wǎng)絡(luò)
這篇文章主要介紹了Android開發(fā)之HTTP訪問網(wǎng)絡(luò)的相關(guān)資料,需要的朋友可以參考下2016-07-07
android kotlin集成WorkManager實現(xiàn)定時獲取數(shù)據(jù)的步驟
在Android中使用Kotlin集成WorkManager來實現(xiàn)定時獲取數(shù)據(jù)是一個很常見的需求,下面給大家分享android kotlin集成WorkManager實現(xiàn)定時獲取數(shù)據(jù)的步驟,感興趣的朋友跟隨小編一起看看吧2024-08-08

