首先來看一下效果圖

注意事項(xiàng):
? ? ? ? 動(dòng)態(tài)改變Widget,需要使用StatefulWidget,在更新Widget時(shí),使用setState就行。廢話不說,上代碼:
import 'package:flutter/material.dart';
import 'dart:io';
import 'package:flutter/services.dart';
var usernameController = new TextEditingController();
var passworkController = new TextEditingController();
String userName;
String passwork;
bool checkboxSelected;
void main() {
? runApp(MyApp());
? if (Platform.isAndroid) {
? ? // 以下兩行 設(shè)置android狀態(tài)欄為透明的沉浸。寫在組件渲染之后,是為了在渲染后進(jìn)行set賦值,覆蓋狀態(tài)欄,寫在渲染之前MaterialApp組件會(huì)覆蓋掉這個(gè)值。
? ? SystemUiOverlayStyle systemUiOverlayStyle =
? ? ? ? SystemUiOverlayStyle(statusBarColor: Colors.transparent);
? ? SystemChrome.setSystemUIOverlayStyle(
? ? ? ? systemUiOverlayStyle.copyWith(statusBarBrightness: Brightness.light));
? ? // SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light
? ? //? ? .copyWith(statusBarBrightness: Brightness.light));
? }
}
class MyApp extends StatelessWidget {
? @override
? Widget build(BuildContext context) {
? ? return MaterialApp(
? ? ? home: Scaffold(
? ? ? ? body: SingleChildScrollView(
? ? ? ? ? child: Column(
? ? ? ? ? ? //MainAxisSize在主軸方向占有空間的值,默認(rèn)是max。還有一個(gè)min
? ? ? ? ? ? mainAxisSize: MainAxisSize.max,
? ? ? ? ? ? //MainAxisAlignment:主軸方向上的對(duì)齊方式,會(huì)對(duì)child的位置起作用,默認(rèn)是start。
? ? ? ? ? ? mainAxisAlignment: MainAxisAlignment.start,
? ? ? ? ? ? children: <Widget>[
? ? ? ? ? ? ? iconImg(),
? ? ? ? ? ? ? editWidget(),
? ? ? ? ? ? ? bt_Widget(),
? ? ? ? ? ? ? radioBt_Widget(),
? ? ? ? ? ? ],
? ? ? ? ? ),
? ? ? ? ),
? ? ? ),
? ? );
? }
}
/**
* icon界面
*/
class iconImg extends StatelessWidget {
? @override
? Widget build(BuildContext context) {
? ? // TODO: implement build
? ? return Container(
? ? ? alignment: Alignment.topCenter,
? ? ? child: Container(
? ? ? ? decoration: BoxDecoration(
? ? ? ? ? ? borderRadius: new BorderRadius.circular((20.0)), // 圓角度
? ? ? ? ? ? boxShadow: [
? ? ? ? ? ? ? ///陰影顏色/位置/大小等
? ? ? ? ? ? ? BoxShadow(
? ? ? ? ? ? ? ? color: Colors.grey[300],
? ? ? ? ? ? ? ? offset: Offset(1, 1),
? ? ? ? ? ? ? ? ///模糊陰影半徑
? ? ? ? ? ? ? ? blurRadius: 10,
? ? ? ? ? ? ? ),
? ? ? ? ? ? ? BoxShadow(
? ? ? ? ? ? ? ? ? color: Colors.grey[300],
? ? ? ? ? ? ? ? ? offset: Offset(-1, -1),
? ? ? ? ? ? ? ? ? blurRadius: 5),
? ? ? ? ? ? ? BoxShadow(
? ? ? ? ? ? ? ? ? color: Colors.grey[200],
? ? ? ? ? ? ? ? ? offset: Offset(1, -1),
? ? ? ? ? ? ? ? ? blurRadius: 10),
? ? ? ? ? ? ? BoxShadow(
? ? ? ? ? ? ? ? ? color: Colors.grey[100],
? ? ? ? ? ? ? ? ? offset: Offset(-1, 1),
? ? ? ? ? ? ? ? ? blurRadius: 10)
? ? ? ? ? ? ],
? ? ? ? ? ? border: Border.all(
? ? ? ? ? ? ? width: 1.0,
? ? ? ? ? ? ? color: Color(0xFFE0E0E0),
? ? ? ? ? ? )),
? ? ? ? margin: EdgeInsets.only(top: 60.0),
? ? ? ? child: Container(
? ? ? ? ? decoration: BoxDecoration(
? ? ? ? ? ? borderRadius: new BorderRadius.circular((20.0)), // 圓角度
? ? ? ? ? ? color: Colors.white,
? ? ? ? ? ),
? ? ? ? ? child: Column(
? ? ? ? ? ? mainAxisSize: MainAxisSize.max,
? ? ? ? ? ? mainAxisAlignment: MainAxisAlignment.center,
? ? ? ? ? ? children: <Widget>[
? ? ? ? ? ? ? new Padding(
? ? ? ? ? ? ? ? padding: EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 10.0),
? ? ? ? ? ? ? ? child: Container(
? ? ? ? ? ? ? ? ? width: 70.0,
? ? ? ? ? ? ? ? ? height: 70.0,
? ? ? ? ? ? ? ? ? child: Image.asset(
? ? ? ? ? ? ? ? ? ? 'images/icon.png',
? ? ? ? ? ? ? ? ? ? scale: 2.85,
? ? ? ? ? ? ? ? ? ? alignment: Alignment.center,
? ? ? ? ? ? ? ? ? ),
? ? ? ? ? ? ? ? ),
? ? ? ? ? ? ? ),
? ? ? ? ? ? ],
? ? ? ? ? ),
? ? ? ? ),
? ? ? ),
? ? );
? }
}
class editWidget extends StatefulWidget {
? editWidget({Key key}) : super(key: key);
? @override
? _editWidget createState() => new _editWidget();
}
/**
* 輸入框界面
*/
class _editWidget extends State<editWidget> {
? var userClearFlag = false;
? var pswClearFlag = false;
? @override
? Widget build(BuildContext context) {
? ? // TODO: implement build
? ? return Container(
? ? ? padding: EdgeInsets.fromLTRB(30.0, 5.0, 10.0, 10.0),
? ? ? child: Column(
? ? ? ? children: <Widget>[
? ? ? ? ? Container(
? ? ? ? ? ? alignment: Alignment.centerLeft,
? ? ? ? ? ? margin: EdgeInsets.only(bottom: 30.0, top: 100.0),
? ? ? ? ? ? child: Text(
? ? ? ? ? ? ? '用戶登錄',
? ? ? ? ? ? ? textAlign: TextAlign.left,
? ? ? ? ? ? ? style: TextStyle(fontSize: 22.0, color: Color(0xff333333)),
? ? ? ? ? ? ),
? ? ? ? ? ),
? ? ? ? ? Row(
? ? ? ? ? ? children: <Widget>[
? ? ? ? ? ? ? Image.asset(
? ? ? ? ? ? ? ? 'images/user.png',
? ? ? ? ? ? ? ? width: 18.0,
? ? ? ? ? ? ? ),
? ? ? ? ? ? ? Expanded(
? ? ? ? ? ? ? ? child: Container(
? ? ? ? ? ? ? ? ? margin: EdgeInsets.only(left: 10.0, right: 5.0),
? ? ? ? ? ? ? ? ? child: TextField(
? ? ? ? ? ? ? ? ? ? controller: usernameController,
? ? ? ? ? ? ? ? ? ? maxLines: 1,
? ? ? ? ? ? ? ? ? ? //是否自動(dòng)更正
? ? ? ? ? ? ? ? ? ? autocorrect: true,
? ? ? ? ? ? ? ? ? ? //是否自動(dòng)對(duì)焦
? ? ? ? ? ? ? ? ? ? autofocus: false,
? ? ? ? ? ? ? ? ? ? textInputAction: TextInputAction.unspecified,
? ? ? ? ? ? ? ? ? ? decoration: InputDecoration(
? ? ? ? ? ? ? ? ? ? ? ? hintStyle:
? ? ? ? ? ? ? ? ? ? ? ? ? ? TextStyle(color: Color(0xffBFBFBF), fontSize: 16.0),
? ? ? ? ? ? ? ? ? ? ? ? hintText: '請(qǐng)輸入用戶名',
? ? ? ? ? ? ? ? ? ? ? ? border: InputBorder.none,
? ? ? ? ? ? ? ? ? ? ? ? prefixStyle: TextStyle(
? ? ? ? ? ? ? ? ? ? ? ? ? ? color: Color(0xff333333), fontSize: 16.0)),
? ? ? ? ? ? ? ? ? ? onChanged: (val) {
? ? ? ? ? ? ? ? ? ? ? setState(() {
? ? ? ? ? ? ? ? ? ? ? ? pswClearFlag = false;
? ? ? ? ? ? ? ? ? ? ? ? if (val.length > 0)
? ? ? ? ? ? ? ? ? ? ? ? ? userClearFlag = true;
? ? ? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? ? userClearFlag = false;
? ? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? ? print(val);
? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? onEditingComplete: () {
? ? ? ? ? ? ? ? ? ? ? print('點(diǎn)擊鍵盤');
? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? onSubmitted: (val) {
? ? ? ? ? ? ? ? ? ? ? print('完成${val}');
? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ),
? ? ? ? ? ? ? ? ),
? ? ? ? ? ? ? ),
? ? ? ? ? ? ? Offstage(
? ? ? ? ? ? ? ? offstage: !userClearFlag,
? ? ? ? ? ? ? ? child: GestureDetector(
? ? ? ? ? ? ? ? ? child: Container(
? ? ? ? ? ? ? ? ? ? margin: EdgeInsets.only(right: 10.0),
? ? ? ? ? ? ? ? ? ? child: Image.asset(
? ? ? ? ? ? ? ? ? ? ? 'images/clear.png',
? ? ? ? ? ? ? ? ? ? ? width: 15.0,
? ? ? ? ? ? ? ? ? ? ),
? ? ? ? ? ? ? ? ? ),
? ? ? ? ? ? ? ? ? onTap: () {
? ? ? ? ? ? ? ? ? ? print('清空');
? ? ? ? ? ? ? ? ? ? usernameController.clear();
? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ),
? ? ? ? ? ? ? ),
? ? ? ? ? ? ],
? ? ? ? ? ),
? ? ? ? ? Container(
? ? ? ? ? ? height: 1.0,
? ? ? ? ? ? margin: EdgeInsets.only(bottom: 40.0),
? ? ? ? ? ? decoration: BoxDecoration(
? ? ? ? ? ? ? border: Border.all(width: 1.0, color: Color(0xffD9D9D9)),
? ? ? ? ? ? ),
? ? ? ? ? ),
? ? ? ? ? Row(
? ? ? ? ? ? children: <Widget>[
? ? ? ? ? ? ? Image.asset(
? ? ? ? ? ? ? ? 'images/psw.png',
? ? ? ? ? ? ? ? width: 18.0,
? ? ? ? ? ? ? ),
? ? ? ? ? ? ? Expanded(
? ? ? ? ? ? ? ? child: Container(
? ? ? ? ? ? ? ? ? margin: EdgeInsets.only(left: 10.0, right: 5.0),
? ? ? ? ? ? ? ? ? child: TextField(
? ? ? ? ? ? ? ? ? ? controller: passworkController,
? ? ? ? ? ? ? ? ? ? maxLines: 1,
? ? ? ? ? ? ? ? ? ? //是否自動(dòng)更正
? ? ? ? ? ? ? ? ? ? autocorrect: true,
? ? ? ? ? ? ? ? ? ? //是否自動(dòng)對(duì)焦
? ? ? ? ? ? ? ? ? ? autofocus: false,
? ? ? ? ? ? ? ? ? ? // 是否是密碼
? ? ? ? ? ? ? ? ? ? obscureText: true,
? ? ? ? ? ? ? ? ? ? textInputAction: TextInputAction.unspecified,
? ? ? ? ? ? ? ? ? ? keyboardType: TextInputType.phone,
? ? ? ? ? ? ? ? ? ? decoration: InputDecoration(
? ? ? ? ? ? ? ? ? ? ? ? hintStyle:
? ? ? ? ? ? ? ? ? ? ? ? ? ? TextStyle(color: Color(0xffBFBFBF), fontSize: 16.0),
? ? ? ? ? ? ? ? ? ? ? ? prefixStyle:
? ? ? ? ? ? ? ? ? ? ? ? ? ? TextStyle(color: Color(0xff333333), fontSize: 16.0),
? ? ? ? ? ? ? ? ? ? ? ? hintText: '請(qǐng)輸入密碼',
? ? ? ? ? ? ? ? ? ? ? ? border: InputBorder.none),
? ? ? ? ? ? ? ? ? ? onChanged: (val) {
? ? ? ? ? ? ? ? ? ? ? setState(() {
? ? ? ? ? ? ? ? ? ? ? ? userClearFlag = false;
? ? ? ? ? ? ? ? ? ? ? ? if (val.length > 0)
? ? ? ? ? ? ? ? ? ? ? ? ? pswClearFlag = true;
? ? ? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? ? pswClearFlag = false;
? ? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? ? print(val);
? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ),
? ? ? ? ? ? ? ? ),
? ? ? ? ? ? ? ),
? ? ? ? ? ? ? Offstage(
? ? ? ? ? ? ? ? offstage: !pswClearFlag,
? ? ? ? ? ? ? ? child: GestureDetector(
? ? ? ? ? ? ? ? ? child: Container(
? ? ? ? ? ? ? ? ? ? margin: EdgeInsets.only(right: 10.0),
? ? ? ? ? ? ? ? ? ? child: Image.asset(
? ? ? ? ? ? ? ? ? ? ? 'images/clear.png',
? ? ? ? ? ? ? ? ? ? ? width: 15.0,
? ? ? ? ? ? ? ? ? ? ),
? ? ? ? ? ? ? ? ? ),
? ? ? ? ? ? ? ? ? onTap: () {
? ? ? ? ? ? ? ? ? ? passworkController.clear();
? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ),
? ? ? ? ? ? ? ),
? ? ? ? ? ? ],
? ? ? ? ? ),
? ? ? ? ? Container(
? ? ? ? ? ? height: 1.0,
? ? ? ? ? ? decoration: BoxDecoration(
? ? ? ? ? ? ? border: Border.all(width: 1.0, color: Color(0xffD9D9D9)),
? ? ? ? ? ? ),
? ? ? ? ? ),
? ? ? ? ],
? ? ? ),
? ? );
? }
}
/**
* 登錄按鈕
*/
class bt_Widget extends StatelessWidget {
? @override
? Widget build(BuildContext context) {
? ? // TODO: implement build
? ? return GestureDetector(
? ? ? child: Container(
? ? ? ? margin:
? ? ? ? ? ? EdgeInsets.only(left: 30.0, right: 30.0, top: 50.0, bottom: 10.0),
? ? ? ? padding: EdgeInsets.only(top: 10.0, bottom: 10.0),
? ? ? ? decoration: BoxDecoration(
? ? ? ? ? borderRadius: BorderRadius.circular(20.0),
? ? ? ? ? color: Color(0xff3896FF),
? ? ? ? ? boxShadow: [
? ? ? ? ? ? ///陰影顏色/位置/大小等
? ? ? ? ? ? BoxShadow(
? ? ? ? ? ? ? color: Colors.grey[200],
? ? ? ? ? ? ? offset: Offset(1, 1),
? ? ? ? ? ? ? ///模糊陰影半徑
? ? ? ? ? ? ? blurRadius: 10,
? ? ? ? ? ? ),
? ? ? ? ? ? BoxShadow(
? ? ? ? ? ? ? ? color: Colors.grey[200], offset: Offset(-1, -1), blurRadius: 5),
? ? ? ? ? ? BoxShadow(
? ? ? ? ? ? ? ? color: Colors.grey[200], offset: Offset(1, -1), blurRadius: 10),
? ? ? ? ? ? BoxShadow(
? ? ? ? ? ? ? ? color: Colors.grey[200], offset: Offset(-1, 1), blurRadius: 10)
? ? ? ? ? ],
? ? ? ? ),
? ? ? ? child: Row(
? ? ? ? ? mainAxisAlignment: MainAxisAlignment.center,
? ? ? ? ? children: <Widget>[
? ? ? ? ? ? Text(
? ? ? ? ? ? ? '登 錄',
? ? ? ? ? ? ? style: TextStyle(fontSize: 16.0, color: Color(0xffffffff)),
? ? ? ? ? ? ? textAlign: TextAlign.center,
? ? ? ? ? ? ),
? ? ? ? ? ],
? ? ? ? ),
? ? ? ),
? ? ? onTap: () {
? ? ? ? showAlertDialog(context);
? ? ? },
? ? );
? }
? void showAlertDialog(BuildContext context) {
? ? showDialog(
? ? ? ? context: context,
? ? ? ? builder: (_) => new AlertDialog(
? ? ? ? ? ? ? ? title: new Text("Dialog Title"),
? ? ? ? ? ? ? ? content: new Text("This is my content"),
? ? ? ? ? ? ? ? actions: <Widget>[
? ? ? ? ? ? ? ? ? new FlatButton(
? ? ? ? ? ? ? ? ? ? child: new Text("CANCEL"),
? ? ? ? ? ? ? ? ? ? onPressed: () {
? ? ? ? ? ? ? ? ? ? ? Navigator.of(context).pop();
? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ),
? ? ? ? ? ? ? ? ? new FlatButton(
? ? ? ? ? ? ? ? ? ? child: new Text("OK"),
? ? ? ? ? ? ? ? ? ? onPressed: () {
? ? ? ? ? ? ? ? ? ? ? Navigator.of(context).pop();
? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? )
? ? ? ? ? ? ? ? ]));
? }
}
class radioBt_Widget extends StatefulWidget {
? radioBt_Widget({Key key}) : super(key: key);
? @override
? _radioBt_Widget createState() => new _radioBt_Widget();
}
/**
* 記住密碼單選框
*/
class _radioBt_Widget extends State<radioBt_Widget> {
? bool _checkboxSelected = true;
? @override
? Widget build(BuildContext context) {
? ? // TODO: implement build
? ? return Container(
? ? ? margin: EdgeInsets.only(left: 25.0, top: 15.0),
? ? ? child: GestureDetector(
? ? ? ? child: Row(
? ? ? ? ? mainAxisAlignment: MainAxisAlignment.start,
? ? ? ? ? children: <Widget>[
? ? ? ? ? ? Container(
? ? ? ? ? ? ? margin: EdgeInsets.only(right: 5.0),
? ? ? ? ? ? ? padding: EdgeInsets.only(top: 5.0, bottom: 5.0, left: 5.0),
? ? ? ? ? ? ? child: Image.asset(
? ? ? ? ? ? ? ? _checkboxSelected ? 'images/wu.png' : 'images/gou.png',
? ? ? ? ? ? ? ? width: 15.0,
? ? ? ? ? ? ? ),
? ? ? ? ? ? ),
? ? ? ? ? ? Text(
? ? ? ? ? ? ? '記住密碼',
? ? ? ? ? ? ? style: TextStyle(fontSize: 14.0, color: Color(0xff333333)),
? ? ? ? ? ? )
? ? ? ? ? ],
? ? ? ? ),
? ? ? ? onTap: () {
? ? ? ? ? setState(() {
? ? ? ? ? ? _checkboxSelected = !_checkboxSelected;
? ? ? ? ? });
? ? ? ? },
? ? ? ),
? ? );
? }
}