flutter編寫精美的登錄頁面
本文實例為大家分享了flutter編寫精美的登錄頁面的具體代碼,供大家參考,具體內(nèi)容如下
先看效果圖;
源代碼已上傳到github
我們先看一下頁面 , 首先這個頁面,我們并沒有用到AppBar,當(dāng)然也就沒有自帶返回功能.
然后下面有個Login的文字以及一條橫線.
屏幕中上方是填寫帳號以及密碼的2個輸入框,密碼輸入框有隱藏和顯示密碼的按鈕.
下方是登錄按鈕 以及其他登錄方式.
看一下主體布局:
return Scaffold( body: Form( key: _formKey, child: ListView( padding: EdgeInsets.symmetric(horizontal: 22.0), children: <Widget>[ SizedBox( height: kToolbarHeight, ), buildTitle(), buildTitleLine(), SizedBox(height: 70.0), buildEmailTextField(), SizedBox(height: 30.0), buildPasswordTextField(context), buildForgetPasswordText(context), SizedBox(height: 60.0), buildLoginButton(context), SizedBox(height: 30.0), buildOtherLoginText(), buildOtherMethod(context), buildRegisterText(context), ], )));
頁面在一個Scaffold中包裹著, 然后整體布局是縱向的,于是我們用ListView來做外層控件,因為是有輸入框,所以我們又用了Form來包裹住整體.
標(biāo)題部分
buildTitle(), buildTitleLine(),
分別實現(xiàn)了Login的文字組件和下方的一個橫線組件.
Login:
Padding( padding: EdgeInsets.all(8.0), child: Text( 'Login', style: TextStyle(fontSize: 42.0), ), );
橫線:
Padding( padding: EdgeInsets.only(left: 12.0, top: 4.0), child: Align( alignment: Alignment.bottomLeft, child: Container( color: Colors.black, width: 40.0, height: 2.0, ), ), );
可以看到,都是用Padding做外層組件,前者包裹了一個Text,后者包裹了一個Container.
輸入框
TextFormField buildPasswordTextField(BuildContext context) { return TextFormField( onSaved: (String value) => _password = value, obscureText: _isObscure, validator: (String value) { if (value.isEmpty) { return '請輸入密碼'; } }, decoration: InputDecoration( labelText: 'Password', suffixIcon: IconButton( icon: Icon( Icons.remove_red_eye, color: _eyeColor, ), onPressed: () { setState(() { _isObscure = !_isObscure; _eyeColor = _isObscure ? Colors.grey : Theme.of(context).iconTheme.color; }); })), ); } TextFormField buildEmailTextField() { return TextFormField( decoration: InputDecoration( labelText: 'Emall Address', ), validator: (String value) { var emailReg = RegExp( r"[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?"); if (!emailReg.hasMatch(value)) { return '請輸入正確的郵箱地址'; } }, onSaved: (String value) => _email = value, ); }
用TextFormField 來實現(xiàn)輸入框, 帳號我們規(guī)定是郵箱,所以用了正則表達式來驗證:
var emailReg = RegExp( r"[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?");
如果不符合,在提交的時候會給出相應(yīng)的提示.
密碼輸入那里使用了判空的方法,多了一個顯示/隱藏密碼的按鈕:
decoration: InputDecoration( labelText: 'Password', suffixIcon: IconButton( icon: Icon( Icons.remove_red_eye, color: _eyeColor, ), onPressed: () { setState(() { _isObscure = !_isObscure; _eyeColor = _isObscure ? Colors.grey : Theme.of(context).iconTheme.color; }); })),
可以看到在decotation中設(shè)置,suffixIcon是在后面加一個圖標(biāo),這里給它一個點擊方法是改變是否顯示密碼的,并更改圖標(biāo)的顏色.
登錄
Align buildLoginButton(BuildContext context) { return Align( child: SizedBox( height: 45.0, width: 270.0, child: RaisedButton( child: Text( 'Login', style: Theme.of(context).primaryTextTheme.headline, ), color: Colors.black, onPressed: () { if (_formKey.currentState.validate()) { ///只有輸入的內(nèi)容符合要求通過才會到達此處 _formKey.currentState.save(); //TODO 執(zhí)行登錄方法 print('email:$_email , assword:$_password'); } }, shape: StadiumBorder(side: BorderSide()), ), ), ); }
登錄按鈕,是一個RaiseButton,點擊的時候,我們判斷輸入框內(nèi)容,符合條件會執(zhí)行登錄方法.
其他帳號登錄
ButtonBar buildOtherMethod(BuildContext context) { return ButtonBar( alignment: MainAxisAlignment.center, children: _loginMethod .map((item) => Builder( builder: (context) { return IconButton( icon: Icon(item['icon'], color: Theme.of(context).iconTheme.color), onPressed: () { //TODO : 第三方登錄方法 Scaffold.of(context).showSnackBar(new SnackBar( content: new Text("${item['title']}登錄"), action: new SnackBarAction( label: "取消", onPressed: () {}, ), )); }); }, )) .toList(), ); }
其他帳號登錄,這里我以facebook,twitter和google為例來實現(xiàn)的
ButtonBar是一個按鈕的組合,我們放了3個IconButton, 并在list中定義了支持的登錄方式. 點擊圖標(biāo)實現(xiàn)對應(yīng)的登錄方法.
其他都是些text使用,跟login大致相同,不再介紹了,想了解請看源碼.github
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Android的網(wǎng)絡(luò)數(shù)據(jù)存儲
LeanCloud是一種簡單高效的數(shù)據(jù)和文件存儲服務(wù),本文主要介紹了利用LeanCloud來進行網(wǎng)絡(luò)數(shù)據(jù)的存儲的實現(xiàn)方法。具有很好的參考價值,需要的朋友一起來看下吧2016-12-12RxJava+Retrofit實現(xiàn)網(wǎng)絡(luò)請求封裝的方法
Retrofit是當(dāng)前應(yīng)用非常廣泛的網(wǎng)絡(luò)請求框架,通常結(jié)合RxJava來進行網(wǎng)絡(luò)請求,本文將展示一個采用RxJava+Retrofit的網(wǎng)絡(luò)請求demo,感興趣的可以了解一下2019-04-04Kotlin之自定義 Live Templates詳解(模板代碼)
這篇文章主要介紹了Kotlin之自定義 Live Templates詳解(模板代碼),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android自定義view實現(xiàn)標(biāo)簽欄功能(只支持固定兩個標(biāo)簽)
這篇文章主要介紹了Android自定義view實現(xiàn)標(biāo)簽欄(只支持固定兩個標(biāo)簽),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06Android+OpenCV4.2.0環(huán)境配置詳解(Android studio)
這篇文章主要介紹了Android+OpenCV4.2.0環(huán)境配置詳解(Android studio),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10android4.0混淆XmlPullParser報錯原因分析解決
今天,用android4.0在proguard-project.txt中加入 -libraryjars libs/ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar這句話后,混淆時報上面的錯誤,下面與大家分享下具體的解決方法2013-06-06Flutter 首頁必用組件NestedScrollView的示例詳解
今天介紹的組件是NestedScrollView,大部分的App首頁都會用到這個組件。對Flutter 首頁必用組件NestedScrollView的相關(guān)知識感興趣的一起看看吧2020-05-05Android EditText 實現(xiàn)監(jiān)聽實例
本文主要介紹Android EditText 組件 實現(xiàn)監(jiān)聽事件,并附有代碼實例,在Android開發(fā)過程中如果能用到可以參考下2016-07-07