flutter写一个漂亮的登录界面,Android面试基础知识

  • 创建登录界面的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,它可以对输入框进行分组,
  • 然后进行一些统一操作,如输入内容校验、输入框重置以及输入内容保存。
    */
    child: new Form(
    key: _SignInFormKey,
    //开启自动检验输入内容,最好还是自己手动检验,不然每次修改子孩子的TextFormField的时候,其他TextFormField也会被检验,感觉不是很好
    // autovalidate: true,
    child: new Column(
    mainAxisSize: MainAxisSize.min,
    children: [
    Flexible(
    child: Padding(
    padding: const EdgeInsets.only(
    left: 25, right: 25, top: 20, bottom: 20),
    child: new TextFormField(
    //关联焦点
    focusNode: emailFocusNode,
    onEditingComplete: () {
    if (foc
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

浏览器打开:qq.cn.hn/FTe 开源分享

usScopeNode == 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的实现

PageController _pageController;
PageView _pageView;
int _currentPage = 0;

@override
void initState() {
super.initState();
_pageController = new PageController();
_pageView = new PageView(
controller: _pageController,
children: [
new SignInPage(),
new SignUpPage(),
],
onPageChanged: (index) {
setState(() {
_currentPage = index;
});
},
);
}

3. InheritedWidget的使用

例如,我想在任意一个地方的子控件,想获得用户的数据User,就可以利用InheritedWidget来实现。比如我们平时用的Theme.of(context)在任何地方来获取应用的主题,或者用MediaQuery.of(context)来获取应用的屏幕数据,其实本质上都是用了InheritedWidget来实现数据的共享。

具体的用法,后面再写一篇文章来解释吧(最近刚弄懂)

/**

  • 利用InheritedWidget用于子节点向祖先节点获取数据
    当依赖的InheritedWidget rebuild,会触发子控件的didChangeDependencies()接口
    */
    class UserProvider extends InheritedWidget {
    final Widget child;
    final User user;//在子树*享的数据

UserProvider({this.user, this.child}) : super(child: child);

/**

  • 该回调决定当数据发生变化时,是否通知子树中依赖数据的Widget
    */
    @override
    bool updateShouldNotify(InheritedWidget oldWidget) {
    return true;
    }
    }

/**

  • 需要一个StatefulWidget作为外层的组件,
    将我们的继承于InheritateWidget的组件build出去
    */
    class UserContainer extends StatefulWidget {
    //给子控件分享的数据
    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 {
@override
Widget build(BuildContext context) {
return new UserProvider(user: widget.user, child: widget.child);
}
}

4.Key的使用

在Flutter中,每个Widget的构建方法都会有一个key的参数可选,如果没有传key,那么应用会自动帮忙生成一个key值。这个key值在整个应用程序里面是唯一的,并且一个key唯一对应一个widget,所以你可以利用key来获取到widget的state,进而对widget进行控制。

例如,利用key来获取Form的状态FormState,当我点击登录按钮的时候,利用key来获取widget的状态FormState,再利用FormState对Form的子孙FromField进行统一的操作。

//定义一个key,并关联到对应的widget
GlobalKey _SignInFormKey = new GlobalKey();
new Form(
key: _SignInFormKey,
child: …)

/**利用key来获取widget的状态FormState
可以用过FormState对Form的子孙FromField进行统一的操作
*/
if (_SignInFormKey.currentState.validate()) {
//如果输入都检验通过,则进行登录操作
Scaffold.of(context).showSnackBar(
new SnackBar(content: new Text(“执行登录操作”)));
//调用所有自孩子的save回调,保存表单内容
_SignInFormKey.currentState.save();
}

遇到的问题

上一篇:JS BOM和DOM


下一篇:flutter集成测试