這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)flutter中怎么實(shí)現(xiàn)發(fā)送驗(yàn)證碼功能,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)提供
網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、網(wǎng)頁設(shè)計(jì),
成都品牌網(wǎng)站建設(shè),
廣告投放等致力于企業(yè)網(wǎng)站建設(shè)與公司網(wǎng)站制作,十余年的網(wǎng)站開發(fā)和建站經(jīng)驗(yàn),助力企業(yè)信息化建設(shè),成功案例突破1000+,是您實(shí)現(xiàn)網(wǎng)站建設(shè)的好選擇.
一個(gè)發(fā)送驗(yàn)證碼的需求:包括限制文本框輸入長度和只允許輸入數(shù)字
class MyBody extends StatefulWidget { @override _MyBodyState createState() => _MyBodyState();} class _MyBodyState extends State { bool isButtonEnable=true; //按鈕狀態(tài) 是否可點(diǎn)擊 String buttonText='發(fā)送驗(yàn)證碼'; //初始文本 int count=60; //初始倒計(jì)時(shí)時(shí)間 Timer timer; //倒計(jì)時(shí)的計(jì)時(shí)器 TextEditingController mController=TextEditingController(); void _buttonClickListen(){ setState(() { if(isButtonEnable){ //當(dāng)按鈕可點(diǎn)擊時(shí) isButtonEnable=false; //按鈕狀態(tài)標(biāo)記 _initTimer(); return null; //返回null按鈕禁止點(diǎn)擊 }else{ //當(dāng)按鈕不可點(diǎn)擊時(shí)// debugPrint('false'); return null; //返回null按鈕禁止點(diǎn)擊 } }); } void _initTimer(){ timer = new Timer.periodic(Duration(seconds: 1), (Timer timer) { count--; setState(() { if(count==0){ timer.cancel(); //倒計(jì)時(shí)結(jié)束取消定時(shí)器 isButtonEnable=true; //按鈕可點(diǎn)擊 count=60; //重置時(shí)間 buttonText='發(fā)送驗(yàn)證碼'; //重置按鈕文本 }else{ buttonText='重新發(fā)送($count)'; //更新文本內(nèi)容 } }); }); } @override void dispose() { timer?.cancel(); //銷毀計(jì)時(shí)器 timer=null; super.dispose(); } @override Widget build(BuildContext context) { return Container( child: Column(// mainAxisAlignment: MainAxisAlignment.center, children: [ 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: [ Text('驗(yàn)證碼',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: ('填寫驗(yàn)證碼'), 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), //按鈕禁用時(shí)的顏色 disabledTextColor: Colors.white, //按鈕禁用時(shí)的文本顏色 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), ), ), ), ], ), ); }}
上述就是小編為大家分享的flutter中怎么實(shí)現(xiàn)發(fā)送驗(yàn)證碼功能了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前標(biāo)題:flutter中怎么實(shí)現(xiàn)發(fā)送驗(yàn)證碼功能-創(chuàng)新互聯(lián)
鏈接分享:
http://weahome.cn/article/idded.html