介紹
最近學(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的圖標庫
添加一張登錄界面的頂部圖片,并聲明資源路徑赊瞬。下面的這種寫法先煎,會之間把整個文件夾下面的資源都導(dǎo)入應(yīng)用程序,可以不用一個一個資源地進行聲明森逮。
random_pk榨婆。這里說一下這個庫的作用磁携,一方面是為容器提供隨機顏色褒侧。另一方面,我覺得可以這個有顏色的容器進行調(diào)試布局谊迄,這個RandomContainer的用法是跟Container一樣的闷供,只需包裹child就可以了。然后你可以通過容器的背景的大小统诺,來判斷是否所繪制布局的大小是不是對的歪脏,看起來比較直觀一些,而且自己可以不用手動的去寫container+color的那種寫法粮呢。當你不需要用的時候婿失,再把這一層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次伶,它可以對輸入框進行分組,
* 然后進行一些統(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ù)
當依賴的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)決定當數(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匀奏,進而對widget進行控制。
例如学搜,利用key來獲取Form的狀態(tài)FormState,當我點擊登錄按鈕的時候娃善,利用key來獲取widget的狀態(tài)FormState论衍,再利用FormState對Form的子孫FromField進行統(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進行統(tǒng)一的操作
*/
if (_SignInFormKey.currentState.validate()) {
//如果輸入都檢驗通過饲齐,則進行登錄操作
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中的收獲和遇到的一些問題