zoukankan      html  css  js  c++  java
  • Flutter-TextField初始化值和選中取值

    import 'package:flutter/material.dart';
    
    class PersonalCenterInformationScreen extends StatefulWidget {
      @override
      _PersonalCenterInformationScreenState createState() => _PersonalCenterInformationScreenState();
    }
    
    class _PersonalCenterInformationScreenState extends State<PersonalCenterInformationScreen> {
      //初始化賦值
      var _username = new TextEditingController();
      var age = new TextEditingController();
      var _userName;
      @override
      void initState() {
        // TODO: implement initState
        super.initState();
        this._username.text = '李家長';
        this.age.text = '18';
      }
      @override
      Widget build(BuildContext context) {
        final width = MediaQuery.of(context).size.width;
        return Scaffold(
          appBar: AppBar(
            title:Text('學生個人信息')
          ),
          body: Padding(
            padding: EdgeInsets.all(10),
            child: Column(
              children: <Widget>[
                Row(
                  children: <Widget>[
                    Container(
                      child: Text(
                        '姓名:',
                        style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 15
                        ),
                      ),
                    ),
                    Container(
                       width*0.7,
                      child: TextField(
                        decoration: InputDecoration(
                          hintText: '請輸入名字',
                        ),
                        //利用控制器初始化
                        controller: this._username,
                        //發生改變事賦值
                        onChanged:(val){
                          this._userName = val;
                        } ,
                      ),
                    ),
                  ],
                ),
                Row(
                  children: <Widget>[
                    Container(
                      child: Text(
                        '年齡:',
                        style: TextStyle(
                            fontWeight: FontWeight.bold,
                            fontSize: 15
                        ),
                      ),
                    ),
                    Container(
                       width*0.7,
                      child: TextField(
                        decoration: InputDecoration(
                          hintText: '請輸入年齡',
                        ),
                        controller: this.age,
                      ),
                    ),
                  ],
                ),
                Row(
                  children: <Widget>[
                    Container(
                      child: Text(
                        '性別:',
                        style: TextStyle(
                            fontWeight: FontWeight.bold,
                            fontSize: 15
                        ),
                      ),
                    ),
                  ],
                ),
                Container(
                   double.infinity,
                  child: RaisedButton(
                    child: Text('修改'),
                    onPressed: (){
                      //輸出控制台
                      print(this._userName);
                    },
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }

    test

  • 相关阅读:
    策略梯度训练cartpole小游戏
    关于不执行整个大项目而是执行其中一部分独立文件夹的时候的python运行方法
    和textrank4ZH代码一模一样的算法详细解读
    K8s常用命令
    python标准库
    chrome通过devtools来查看Devtools Extension与浏览器内核实际通信的数据情况
    修改文件MD5值
    使用charles过滤网络请求
    git中working tree, index, commit
    Maven中settings.xml的配置项说明
  • 原文地址:https://www.cnblogs.com/ssjf/p/11805685.html
Copyright © 2011-2022 走看看