線上書籍

Home

Dart 及 flutter 初體驗

class _SignInState extends State<SignIn> { final GlobalKey<FormState> _formKey = GlobalKey<FormState>(); String email; String passwd; InputDecoration myInputDecoration = InputDecoration( fillColor: Colors.white, filled: true, focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.lime, width: 2.0), ), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.black12, width: 2.0), ), ); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('登入我的待辦清單'), ), body: Container( child: Form( key: _formKey, child: Column( children: [ TextFormField( onChanged: (val) { setState(() { email = val; }); }, validator: (String val) => (val.isEmpty) ? '請輸入電子郵件' : null, decoration: myInputDecoration.copyWith(hintText: '請輸入電子郵件'), ), TextFormField( onChanged: (val) { setState(() { passwd = val; }); }, obscureText: true, validator: (String val) => (val.length < 6) ? '密碼至少 6 個字' : null, decoration: myInputDecoration.copyWith(hintText: '請輸入登入密碼'), ), ElevatedButton( onPressed: () { if (_formKey.currentState.validate()) { print('電子郵件:$email'); print('密碼:$passwd'); } }, child: Text( '登入', style: TextStyle( color: Colors.white, fontSize: 20.0, ), ), ), ], ), ), ), ); } }