12-flutter Textfield的使用

获取用户的输入用

TextField 或者TextFormField 的实现,通过控制器来实现获取用户的输入。

1 TextField 的属性
const TextField({
  Key key,
  this.controller,
  this.focusNode,// 这个属性可以用来监听输入框是否获取
  this.decoration = const InputDecoration(),
  TextInputType keyboardType,
  this.textInputAction,
  this.textCapitalization = TextCapitalization.none,
  this.style,
  this.textAlign = TextAlign.start,   //类似Text组件
  this.textDirection,   //类似Text组件
  this.autofocus = false,
  this.obscureText = false,// 密文/明文显示
  this.autocorrect = true,
  this.maxLines = 1,
  this.maxLength,
  this.maxLengthEnforced = true,
  this.onChanged,
  this.onEditingComplete,
  this.onSubmitted,
  this.inputFormatters,
  this.enabled,
  this.cursorWidth = 2.0,
  this.cursorRadius,
  this.cursorColor,
  this.keyboardAppearance,
  this.scrollPadding = const EdgeInsets.all(20.0),
  this.enableInteractiveSelection = true,
  this.onTap,
})
2 输入框的样式 InputDecoration
const InputDecoration({
  this.icon,  //输入框前面的图片(在下划线外面)
  this.labelText,  //顶部提示文字(获取焦点之后会移动到输入框上方)
  this.labelStyle,
  this.helperText,  //底部提示文字(不会移动)
  this.helperStyle,
  this.hintText,  //占位文字
  this.hintStyle,
  this.errorText,  //类似helperText
  this.errorStyle,
  this.errorMaxLines,
  this.hasFloatingPlaceholder = true,
  this.isDense,
  this.contentPadding,  //输入内容的边距,默认有一个边距,可以手动设置
  this.prefixIcon, //输入框前面的图片(在下划线里面)
  this.prefix,
  this.prefixText,
  this.prefixStyle,
  this.suffixIcon,  //输入框后面的图片(在下划线里面)
  this.suffix,
  this.suffixText,
  this.suffixStyle,
  this.counterText,
  this.counterStyle,
  this.filled,
  this.fillColor,
  this.errorBorder,
  this.focusedBorder,
  this.focusedErrorBorder,
  this.disabledBorder,
  this.enabledBorder,
  this.border,   //输入框边框线(默认是下划线,也可以是none,也可以是一个框)
  this.enabled = true,
  this.semanticCounterText,
})
3 获取回调
  • 如何获取回调的值 第一种方式就是通过onchanged 方式获取,
// 每一次的输入都会调用次方法               
onChanged: (text){
print(text);
}, 
  • 第二种就是通过设置控制器来获取。在iOS中就是相当于代理吧。

可以获取输入的值 清空输入的值,设置输入的值

import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

import 'dart:convert';
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyForm(),
      );

  }
}

class MyForm extends StatefulWidget{
    MyForm({Key key}) : super(key:key);
    @override
    _MyFormState createState() =>  _MyFormState();
    
}

class _MyFormState extends State<MyForm>{

    final myVC = TextEditingController();

    // show log
    void showTip(BuildContext context){        
        Scaffold.of(context).showSnackBar(new SnackBar(content: new Text(myVC.text),));
    }

    @override
    void dispose(){
      // 销毁组件的时候 销毁控制器
      myVC.dispose();
      super.dispose();
    }

    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(title: Text("Hello World"),),
        body: Padding(
          padding: const EdgeInsets.all(20),
          child: Column(children: <Widget>[
                TextField(
              // 设置vc
              controller: myVC,
              // 提示
              decoration: InputDecoration(
                prefixIcon: Icon(Icons.lock),
                labelText: "手机号码",
                // 提示字体的颜色
                hintStyle: TextStyle(color: Colors.green),
                hintText: "请输入手机号码"), 

              // 每一次的输入都会调用次方法               
              onChanged: (text){
                print(text);
              },  
            ),

            RaisedButton(
              child: Text("我输入了什么东西???"),
              onPressed: (){                                              
                print(myVC.text);
                // 清空字体
                myVC.clear();
                // 设置字体
                myVC.text = "OKTOGO";
              },
            ),
            
          ],)
        ),
      );

    }

}




在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值