Flutter 绘制一个登录界面

我们在使用一个软件或者APP时,肯定会接触登录界面。只有我们登录系统成功后才能使用软件的功能,所以这期我们来实现一个登录界面。

这个项目中用到的容器、组件有 Material风格的基本布局结构Scaffold()、单容器控件Container()、水平容器控件Row()、竖直容器控件Column()、Padding组件Padding()、居中控件Center()、 弹性控件Expanded()、分隔控件Divider()、圆形头像控件CircleAvatar()、按钮MaterialButton()、基本文本控件Text()、输入框控件TextField()。

Scaffold控件

const Scaffold({
Key? key,
this.appBar,
this.body,
this.floatingActionButton,
this.floatingActionButtonLocation,
this.floatingActionButtonAnimator,
this.persistentFooterButtons,
this.drawer,
this.onDrawerChanged,
this.endDrawer,
this.onEndDrawerChanged,
this.bottomNavigationBar,
this.bottomSheet,
this.backgroundColor,
this.resizeToAvoidBottomInset,
this.primary = true,
this.drawerDragStartBehavior = DragStartBehavior.start,
this.extendBody = false,
this.extendBodyBehindAppBar = false,
this.drawerScrimColor,
this.drawerEdgeDragWidth,
this.drawerEnableOpenDragGesture = true,
this.endDrawerEnableOpenDragGesture = true,
this.restorationId,
})

Container控件

Container({
Key? key,
this.alignment,
this.padding,
this.color,
this.decoration,
this.foregroundDecoration,
double? width,
double? height,
BoxConstraints? constraints,
this.margin,
this.transform,
this.transformAlignment,
this.child,
this.clipBehavior = Clip.none,
})

Row控件

Row({
Key? key,
MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
MainAxisSize mainAxisSize = MainAxisSize.max,
CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
TextDirection? textDirection,
VerticalDirection verticalDirection = VerticalDirection.down,
TextBaseline? textBaseline, // NO DEFAULT: we don't know what the text's baseline should be
List children = const [],
})

Column控件

Column({
Key? key,
MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
MainAxisSize mainAxisSize = MainAxisSize.max,
CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
TextDirection? textDirection,
VerticalDirection verticalDirection = VerticalDirection.down,
TextBaseline? textBaseline,
List children = const [],
})

Padding控件

const Padding({
Key? key,
required this.padding,
Widget? child,
})

Center控件

const Center({ Key? key, double? widthFactor, double? heightFactor, Widget? child })

Expanded控件

const Expanded({
Key? key,
int flex = 1,
required Widget child,
})

Divider控件

const Divider({
Key? key,
this.height,
this.thickness,
this.indent,
this.endIndent,
this.color,
})

CircleAvatar控件

const CircleAvatar({
Key? key,
this.child,
this.backgroundColor,
this.backgroundImage,
this.foregroundImage,
this.onBackgroundImageError,
this.onForegroundImageError,
this.foregroundColor,
this.radius,
this.minRadius,
this.maxRadius,
})

MaterialButton控件

const MaterialButton({
Key? key,
required this.onPressed,
this.onLongPress,
this.onHighlightChanged,
this.mouseCursor,
this.textTheme,
this.textColor,
this.disabledTextColor,
this.color,
this.disabledColor,
this.focusColor,
this.hoverColor,
this.highlightColor,
this.splashColor,
this.colorBrightness,
this.elevation,
this.focusElevation,
this.hoverElevation,
this.highlightElevation,
this.disabledElevation,
this.padding,
this.visualDensity,
this.shape,
this.clipBehavior = Clip.none,
this.focusNode,
this.autofocus = false,
this.materialTapTargetSize,
this.animationDuration,
this.minWidth,
this.height,
this.enableFeedback = true,
this.child,
})

Text控件

const Text(
String this.data, {
Key? key,
this.style,
this.strutStyle,
this.textAlign,
this.textDirection,
this.locale,
this.softWrap,
this.overflow,
this.textScaleFactor,
this.maxLines,
this.semanticsLabel,
this.textWidthBasis,
this.textHeightBehavior,
})

TextField控件

const TextField({
Key? key,
this.controller,
this.focusNode,
this.decoration = const InputDecoration(),
TextInputType? keyboardType,
this.textInputAction,
this.textCapitalization = TextCapitalization.none,
this.style,
this.strutStyle,
this.textAlign = TextAlign.start,
this.textAlignVertical,
this.textDirection,
this.readOnly = false,
ToolbarOptions? toolbarOptions,
this.showCursor,
this.autofocus = false,
this.obscuringCharacter = '•',
this.obscureText = false,
this.autocorrect = true,
SmartDashesType? smartDashesType,
SmartQuotesType? smartQuotesType,
this.enableSuggestions = true,
this.maxLines = 1,
this.minLines,
this.expands = false,
this.maxLength,
@Deprecated(
'Use maxLengthEnforcement parameter which provides more specific '
'behavior related to the maxLength limit. '
'This feature was deprecated after v1.25.0-5.0.pre.',)
this.maxLengthEnforced = true,
this.maxLengthEnforcement,
this.onChanged,
this.onEditingComplete,
this.onSubmitted,
this.onAppPrivateCommand,
this.inputFormatters,
this.enabled,
this.cursorWidth = 2.0,
this.cursorHeight,
this.cursorRadius,
this.cursorColor,
this.selectionHeightStyle = ui.BoxHeightStyle.tight,
this.selectionWidthStyle = ui.BoxWidthStyle.tight,
this.keyboardAppearance,
this.scrollPadding = const EdgeInsets.all(20.0),
this.dragStartBehavior = DragStartBehavior.start,
this.enableInteractiveSelection = true,
this.selectionControls,
this.onTap,
this.mouseCursor,
this.buildCounter,
this.scrollController,
this.scrollPhysics,
this.autofillHints,
this.restorationId,
})

1.新建一个 Flutter 项目,项目命名为: LoginUI_demo 。

2.在新建的项目文件夹中找到【lib】文件夹,然后在里面开始编写代码。

自定义组件:

Widget TextBlock(){
    return Padding(
        padding: EdgeInsets.fromLTRB(0, 40, 0, 0),
        child: Text(
          'Sign in',
          style: TextStyle(
            fontSize: 32,
            fontWeight: FontWeight.bold,
          ),
        ),
    );
  }
  Widget TextBlock2(){
    return Center(
      child: Padding(
        padding: EdgeInsets.fromLTRB(0, 39, 0, 0),
        child: Text(
          '···',
          style: TextStyle(
            fontSize: 15,
            color: Colors.cyanAccent,
          ),
        ),
      ),
    );
  }
  Widget TextBlock3(){
    return Padding(
      padding: EdgeInsets.fromLTRB(30, 50, 0, 0),
      child: Text(
        '···',
        style: TextStyle(
          fontSize: 18,
          color: Colors.pinkAccent,
        ),
      ),
    );
  }
  Widget DividerText(){
    return Center(
      //padding: EdgeInsets.fromLTRB(0, 40, 0, 0),
      child: Text(
        '其他登录方式',
        style: TextStyle(
          color: Colors.blueGrey[500],
          fontSize: 15,
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }
  Widget CircleAvatarImage(){
    return Padding(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          Padding(
            padding: EdgeInsets.fromLTRB(18, 10, 0, 0),
            child: CircleAvatar(
              radius: 12,
              backgroundColor: Colors.white,
              backgroundImage: AssetImage('images/wechat.png'),
            ),
          ),
          Padding(
            padding: EdgeInsets.fromLTRB(18, 10, 0, 0),
            child: CircleAvatar(
              radius: 12,
              backgroundColor: Colors.white,
              backgroundImage: AssetImage('images/zhihu.png'),
            ),
          ),
          Padding(
            padding: EdgeInsets.fromLTRB(18, 10, 0, 0),
            child: CircleAvatar(
              radius: 12,
              backgroundColor: Colors.white,
              backgroundImage: AssetImage('images/QQ.png'),
            ),
          ),
          Padding(
            padding: EdgeInsets.fromLTRB(18, 10, 0, 0),
            child: CircleAvatar(
              radius: 12,
              backgroundColor: Colors.white,
              backgroundImage: AssetImage('images/baidu.png'),
            ),
          ),
          Padding(
            padding:  EdgeInsets.fromLTRB(20, 10, 0, 0),
            child: CircleAvatar(
              radius: 12,
              backgroundColor: Colors.white,//背景颜色设置为白色
              backgroundImage: AssetImage('images/github.png'),
            ),
          ),
        ],
      ),
    );
  }

两个按钮

      Row(
          children: [
            MaterialButton(
              textColor: Colors.white, //文本的颜色
              minWidth: 88, //最小的宽度,默认是 88
              height: 37, // Button 高度,默认是 36
              onPressed: () {},
              child: const Text("注册"), //显示的文字
            ),
            MaterialButton(
              color: CupertinoColors.link, //按钮颜色
              textColor: Colors.white, //文本的颜色
              minWidth: 108, //最小的宽度,默认是 88
              child: Text(
                "登录",
                style: TextStyle(
                  fontSize: 18,
                ),
              ), //显示的文字
            ),
          ],
        ),

两个输入框

    Column(
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.fromLTRB(0, 20, 0, 0), //上边部分填充
              child: TextField(
                style: TextStyle(fontSize: 12, color: Colors.black87), //文字大小、颜色
                decoration: InputDecoration(
                  filled: true, //必须设置为true,fillColor才有效
                  fillColor: Colors.grey[80], //背景颜色
                  hintText: "邮箱或手机号",
                  ///设置输入框可编辑时的边框样式
                  enabledBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(20.0), //输入框圆角
                      borderSide: BorderSide(), //输入框白色隐藏
                  ),
                ),
              ),
            ),
            Padding(
              //设置内边距
              padding: const EdgeInsets.fromLTRB(0, 20, 0, 0),
              child: TextField(
                obscureText: true,
                style: TextStyle(fontSize: 12, color: Colors.black87), //文字大小、颜色
                decoration: InputDecoration(
                  filled: true, //必须设置为true,fillColor才有效
                  fillColor: Colors.grey[80], //背景颜色
                  hintText: "密码",
                  ///设置输入框可编辑时的边框样式
                  enabledBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(20.0), //输入框圆角
                      borderSide: BorderSide() //输入框白色隐藏
                  ),
                ),
              ),
            ),
          ],
      ),

右侧布局代码

class RightWidget extends StatelessWidget {
  const RightWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    //填充布局
    return Padding(
      //四周填充10
      padding: EdgeInsets.fromLTRB(80, 10, 0,20),
      //垂直布局
      child: Column(
          children: <Widget>[
            TextBlock(),
            TextBlock2(),
            InputField(),
            ButtonBlock(),
            Padding(
              child: Divider(thickness: 1,indent: 2,color: Colors.blueGrey[300],),
            ),
            DividerText(),
            CircleAvatarImage(),
            TextBlock3(),
          ],
      ),
    );
  }
}

总体布局代码

   Scaffold(
      body: Center(
        //水平布局
        child: Row(
          //子组件
          children: [
            //Expanded 组件
            Expanded(
              //左侧空间占比
              flex: 3,
              //左侧布局
              child: LeftImage(),
            ),
            Expanded(
              //右侧空间占比
              flex: 5,
              //右侧布局
              child: RightWidget(),
            ),
          ],
        ),
      ),
    );

检查项目中程序代码是否有错误,有需要按照提示改正,没有直接配置主运行文件 main.dart 。

运行主文件 main.dart ,观察界面效果。

image
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,029评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,395评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,570评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,535评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,650评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,850评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,006评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,747评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,207评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,536评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,683评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,342评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,964评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,772评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,004评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,401评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,566评论 2 349

推荐阅读更多精彩内容