zoukankan      html  css  js  c++  java
  • flutter Form表单

    import 'package:flutter/material.dart';
    
    class FormDemo extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('FormDemo'),
            elevation: 0.0,
          ),
          body: Theme(
            data: Theme.of(context).copyWith(
              primaryColor: Colors.black,
            ),
            child: Container(
              padding: EdgeInsets.all(16.0),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  RegisterForm(),
                ],
              ),
            ),
          ),
        );
      }
    }
    
    class RegisterForm extends StatefulWidget {
      @override
      RegisterFormState createState() => RegisterFormState();
    }
    
    class RegisterFormState extends State<RegisterForm> {
      final registerFormKey = GlobalKey<FormState>();
      String username, password;
      bool autovalidate = false;
    
      void submitRegisterForm() {
        if (registerFormKey.currentState.validate()) {
          registerFormKey.currentState.save();
    
          debugPrint('username: $username');
          debugPrint('password: $password');
         
          Scaffold.of(context).showSnackBar(
            SnackBar(
              content: Text('Registering...'),
            )
          );
        } else {
          setState(() {
            autovalidate = true;        
          });
        }
      }
    
      String validateUsername(value) {
        if (value.isEmpty) {
          return 'Username is required.';
        }
    
        return null;
      }
    
      String validatePassword(value) {
        if (value.isEmpty) {
          return 'Password is required.';
        }
    
        return null;
      }
    
      @override
      Widget build(BuildContext context) {
        return Form(
          key: registerFormKey,
          child: Column(
            children: <Widget>[
              TextFormField(
                decoration: InputDecoration(
                  labelText: 'Username',
                  helperText: '',
                ),
                onSaved: (value) {
                  username = value;
                },
                validator: validateUsername,
                autovalidate: autovalidate,
              ),
              TextFormField(
                obscureText: true,
                decoration: InputDecoration(
                  labelText: 'Password',
                  helperText: '',
                ),
                onSaved: (value) {
                  password = value;
                },
                validator: validatePassword,
                autovalidate: autovalidate,
              ),
              SizedBox(height: 32.0,),
              Container(
                 double.infinity,
                child: RaisedButton(
                  color: Theme.of(context).accentColor,
                  child: Text('Register', style: TextStyle(color: Colors.white)),
                  elevation: 0.0,
                  onPressed: submitRegisterForm,              
                ),
              ),
            ],
          ),
        );
      }
    }
    
    class TextFieldDemo extends StatefulWidget {
      @override
      TextFieldDemoState createState() => TextFieldDemoState();
    }
    
    class TextFieldDemoState extends State<TextFieldDemo> {
      final textEditingController = TextEditingController();
    
      @override
      void dispose() {
        textEditingController.dispose();
        super.dispose();
      }
    
      @override
      void initState() {
        super.initState();
        // textEditingController.text = 'hi';
        textEditingController.addListener(
          () {
            debugPrint('input: ${textEditingController.text}');
          }
        );
      }
    
      @override
      Widget build(BuildContext context) {
        return TextField(
          controller: textEditingController,
          // onChanged: (value) {
          //   debugPrint('input: $value');
          // },
          onSubmitted: (value) {
            debugPrint('submit: $value');
          },
          decoration: InputDecoration(
            icon: Icon(Icons.subject),
            labelText: 'Title',
            hintText: 'Enter the post title.',
            // border: InputBorder.none,
            // border: OutlineInputBorder(),
            filled: true,
          ),
        );
      }
    }
    
    class ThemeDemo extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Container(
          color: Theme.of(context).accentColor,
        );
      }
    }

    效果:

  • 相关阅读:
    Eclipse设置、调优、使用
    eclipse安装插件的方式 三种:links、eclipse中使用插件安装向导安装、直接copy插件到对应的eclipse目录 MyEclipse10安装SVN插件
    eclipse 在Servers窗口创建一个Tomcat 6.0 Server失败
    小技巧:帮你批量删除代码前的行号
    editplus发布3.01 Build 446 Final版(附下载及中文版)
    eclipse 垃圾回收器,内存释放
    eclipse.ini的相关说明
    Eclipse 去掉JavaScript Validator
    DropBox 超实用的免费文件网络同步、备份、分享工具
    使用EditPlus技巧,提高工作效率(附英文版、自动完成文件、语法文件下载)
  • 原文地址:https://www.cnblogs.com/loaderman/p/11344678.html
Copyright © 2011-2022 走看看