介紹
最近學(xué)習(xí)了Flutter的一些控件使用矗钟,然后在Github上面看見了一個挺漂亮的登錄界面,于是就用Flutter自己模仿地實現(xiàn)了一下。原作者做得比較好看妒牙,不過只是單純實現(xiàn)界面吐绵。所以自己加了些東西迹淌,比如Key的使用和InheritedWidget的使用。
下面是一些總結(jié)己单,如果有說錯的地方唉窃,還請各位指出來,謝謝纹笼。
最終的展示界面
代碼結(jié)構(gòu)
每個類的名字纹份,相信大家一看就知道對應(yīng)的作用類。每個類的作用廷痘,我在代碼里面都有注釋蔓涧,可以去看下代碼。
主要用到的控件
- 利用Row牍疏,Column沿水平方向或者垂直方向排列子布局
- 利用Stack實現(xiàn)布局層疊蠢笋,利用Positioned控件實現(xiàn)絕對定位
- 利用Container實現(xiàn)裝飾效果
- 利用TextFormField實現(xiàn)文本輸入,利用Form來管理這些TextFormField
- 利用Key來獲取widget的狀態(tài)
- 利用InheritedWidget可以把數(shù)據(jù)傳遞給子控件
- 利用PageView和PageController實現(xiàn)頁面滑動切換
在pubspec.yaml中添加依賴
- font_awesome_flutter鳞陨,這個一個Flutter的圖標(biāo)庫
- 添加一張登錄界面的頂部圖片昨寞,并聲明資源路徑。下面的這種寫法厦滤,會之間把整個文件夾下面的資源都導(dǎo)入應(yīng)用程序援岩,可以不用一個一個資源地進(jìn)行聲明。
- random_pk掏导。這里說一下這個庫的作用享怀,一方面是為容器提供隨機顏色。另一方面趟咆,我覺得可以這個有顏色的容器進(jìn)行調(diào)試布局添瓷,這個RandomContainer的用法是跟Container一樣的,只需包裹child就可以了值纱。然后你可以通過容器的背景的大小鳞贷,來判斷是否所繪制布局的大小是不是對的,看起來比較直觀一些虐唠,而且自己可以不用手動的去寫container+color的那種寫法搀愧。當(dāng)你不需要用的時候,再把這一層RandomContainer給去掉就可以了。我自己在平時用的時候咱筛,發(fā)現(xiàn)確實挺有作用的搓幌,大家可以拿去試試。
random_pk: any
font_awesome_flutter: any
//省略部分代碼
assets:
- assets/
代碼實現(xiàn)
1.利用代碼模板生成代碼迅箩,新建一個空頁面(如果手動打出一段stateful的代碼是真的麻煩)
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
@override
Widget build(BuildContext context) {
return Container();
}
}
2.根據(jù)布局編寫代碼
這部分沒什么好說的溉愁,主要是要熟悉一些控件的使用,根據(jù)UI稿一步一步寫出布局就可以了饲趋。
例如叉钥,輸入賬號和密碼的TextForm的實現(xiàn)
/**
* 創(chuàng)建登錄界面的TextForm
*/
Widget buildSignInTextForm() {
return new Container(
decoration:
new BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(8))
, color: Colors.white
),
width: 300,
height: 190,
/**
* Flutter提供了一個Form widget,它可以對輸入框進(jìn)行分組篙贸,
* 然后進(jìn)行一些統(tǒng)一操作,如輸入內(nèi)容校驗枫疆、輸入框重置以及輸入內(nèi)容保存爵川。
*/
child: new Form(
key: _SignInFormKey,
//開啟自動檢驗輸入內(nèi)容,最好還是自己手動檢驗息楔,不然每次修改子孩子的TextFormField的時候寝贡,其他TextFormField也會被檢驗,感覺不是很好
// autovalidate: true,
child: new Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Flexible(
child: Padding(
padding: const EdgeInsets.only(
left: 25, right: 25, top: 20, bottom: 20),
child: new TextFormField(
//關(guān)聯(lián)焦點
focusNode: emailFocusNode,
onEditingComplete: () {
if (focusScopeNode == null) {
focusScopeNode = FocusScope.of(context);
}
focusScopeNode.requestFocus(passwordFocusNode);
},
decoration: new InputDecoration(
icon: new Icon(Icons.email, color: Colors.black,),
hintText: "Email Address",
border: InputBorder.none
),
style: new TextStyle(fontSize: 16, color: Colors.black),
//驗證
validator: (value) {
if (value.isEmpty) {
return "Email can not be empty!";
}
},
onSaved: (value) {
},
),
),
),
new Container(
height: 1,
width: 250,
color: Colors.grey[400],
),
Flexible(
child: Padding(
padding: const EdgeInsets.only(
left: 25, right: 25, top: 20),
child: new TextFormField(
focusNode: passwordFocusNode,
decoration: new InputDecoration(
icon: new Icon(Icons.lock, color: Colors.black,),
hintText: "Password",
border: InputBorder.none,
suffixIcon: new IconButton(icon: new Icon(
Icons.remove_red_eye, color: Colors.black,),
onPressed: showPassWord)
),
//輸入密碼值依,需要用*****顯示
obscureText: !isShowPassWord,
style: new TextStyle(fontSize: 16, color: Colors.black),
validator: (value) {
if (value == null || value.isEmpty || value.length < 6) {
return "Password'length must longer than 6!";
}
},
onSaved: (value) {
},
),
),
),
],
),),
);
}
例如圃泡,PageView的實現(xiàn)
PageController _pageController;
PageView _pageView;
int _currentPage = 0;
@override
void initState() {
super.initState();
_pageController = new PageController();
_pageView = new PageView(
controller: _pageController,
children: <Widget>[
new SignInPage(),
new SignUpPage(),
],
onPageChanged: (index) {
setState(() {
_currentPage = index;
});
},
);
}
3. InheritedWidget的使用
例如,我想在任意一個地方的子控件愿险,想獲得用戶的數(shù)據(jù)User颇蜡,就可以利用InheritedWidget來實現(xiàn)。比如我們平時用的Theme.of(context)在任何地方來獲取應(yīng)用的主題辆亏,或者用MediaQuery.of(context)來獲取應(yīng)用的屏幕數(shù)據(jù)风秤,其實本質(zhì)上都是用了InheritedWidget來實現(xiàn)數(shù)據(jù)的共享。
具體的用法扮叨,后面再寫一篇文章來解釋吧(最近剛弄懂)
/**
* 利用InheritedWidget用于子節(jié)點向祖先節(jié)點獲取數(shù)據(jù)
當(dāng)依賴的InheritedWidget rebuild,會觸發(fā)子控件的didChangeDependencies()接口
*/
class UserProvider extends InheritedWidget {
final Widget child;
final User user;//在子樹中共享的數(shù)據(jù)
UserProvider({this.user, this.child}) : super(child: child);
/**
* 該回調(diào)決定當(dāng)數(shù)據(jù)發(fā)生變化時缤弦,是否通知子樹中依賴數(shù)據(jù)的Widget
*/
@override
bool updateShouldNotify(InheritedWidget oldWidget) {
return true;
}
}
/**
* 需要一個StatefulWidget作為外層的組件,
將我們的繼承于InheritateWidget的組件build出去
*/
class UserContainer extends StatefulWidget {
//給子控件分享的數(shù)據(jù)
final User user;
final Widget child;
UserContainer({Key key, this.user, this.child}) : super(key: key);
@override
_UserContainerState createState() => _UserContainerState();
static UserProvider of(BuildContext context) {
return context.inheritFromWidgetOfExactType(UserProvider);
}
}
class _UserContainerState extends State<UserContainer> {
@override
Widget build(BuildContext context) {
return new UserProvider(user: widget.user, child: widget.child);
}
}
4.Key的使用
在Flutter中彻磁,每個Widget的構(gòu)建方法都會有一個key的參數(shù)可選碍沐,如果沒有傳key,那么應(yīng)用會自動幫忙生成一個key值衷蜓。這個key值在整個應(yīng)用程序里面是唯一的累提,并且一個key唯一對應(yīng)一個widget,所以你可以利用key來獲取到widget的state恍箭,進(jìn)而對widget進(jìn)行控制刻恭。
例如,利用key來獲取Form的狀態(tài)FormState,當(dāng)我點擊登錄按鈕的時候,利用key來獲取widget的狀態(tài)FormState鳍贾,再利用FormState對Form的子孫FromField進(jìn)行統(tǒng)一的操作鞍匾。
//定義一個key,并關(guān)聯(lián)到對應(yīng)的widget
GlobalKey<FormState> _SignInFormKey = new GlobalKey();
new Form(
key: _SignInFormKey,
child: .........)
/**利用key來獲取widget的狀態(tài)FormState
可以用過FormState對Form的子孫FromField進(jìn)行統(tǒng)一的操作
*/
if (_SignInFormKey.currentState.validate()) {
//如果輸入都檢驗通過骑科,則進(jìn)行登錄操作
Scaffold.of(context).showSnackBar(
new SnackBar(content: new Text("執(zhí)行登錄操作")));
//調(diào)用所有自孩子的save回調(diào)橡淑,保存表單內(nèi)容
_SignInFormKey.currentState.save();
}
遇到的問題
1.一些布局方面的坑 利用SafeArea可以讓內(nèi)容顯示在安全的可見區(qū)域。 利用SingleChildScrollView可以避免彈出鍵盤的時候咆爽,出現(xiàn)overFlow的現(xiàn)象梁棠。
2.理解context,state斗埂,key,widget的一些概念和之間的關(guān)系呛凶,這篇文章我覺得寫得很不錯: https://user-gold-cdn.xitu.io/2018/12/8/1678bb83bcdb5f6d
下一步計劃
繼續(xù)整理自己學(xué)習(xí)Flutter中的收獲和遇到的一些問題
Demo地址(代碼我都加了挺多注釋的地方)
https://github.com/LXD312569496/flutter-learing/tree/master/login_demo
歡迎大家關(guān)注我的公眾號,會推送關(guān)于Flutter和Android學(xué)習(xí)的一些文章