flutter 自定义输入框组件
一、组件分析
ui如下

根据UI分析我们需要提取哪些是动态的,可以通过传递参数得到不同的结果?
1.左侧icon
2.输入的文本
3.是否是密码框
4.输入框的控制器:如何时时得到输入框的值
二、快速创建自定义组件
vscode中使用快捷键stl快速生成一个无状态组件,

class CreateMyInput extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
);
}
}
为什么这里使用的是一个无状态组件(StatelessWidget)?,这里只是一个纯展示的,给定固定的参数就会返回固定的结果,而不是涉及自定义组件内部状态交互。因此选择无状态组件。
下面是建立好的无状态组件模板
import 'package:flutter/material.dart';
class CreateMyInput extends StatelessWidget {
final iconString;
final placeholder;
final isPassword;
final inputController;
//接收参数写法:1:
// const CreateMyInput(
// {Key key,
// this.iconString,
// this.placeholder,
// this.isPassword,
// @required this.inputController})
// : super(key: key);
//接收参数写法2:。。这种写法更简洁一点
CreateMyInput({this.iconString,this.placeholder,this.isPassword,this.inputController});
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.fromLTRB(15.0,5,15.0,5),
child: Row(
children: <Widget>[
Image.asset(iconString,width: 25,color:Color(0xff2D4ED1)),
Expanded(
child: Container(
margin: EdgeInsets.only(left: 15),
decoration:BoxDecoration(
border:Border(bottom: BorderSide(width: 0.8,color: Color(0xff2D4ED1))), //底部border
),
padding: EdgeInsets.fromLTRB(0, 0, 5, 0),
child: TextField(
controller: inputController,
decoration: InputDecoration(
hintText: placeholder,
contentPadding: EdgeInsets.fromLTRB(0, 17, 15, 15), //输入框内容部分设置padding,跳转跟icon的对其位置
border:InputBorder.none,
),
obscureText: isPassword, //是否是以星号*显示密码
),
),
)
],
),
);
}
}
三、外部使用自定义组
import '../base_widgit/create_my_input.dart'; //手机号的控制器 TextEditingController phoneController = TextEditingController(); //密码的控制器 TextEditingController passwordController = TextEditingController(); CreateMyInput(iconString:'images/login_icon_phone.png',placeholder:"请输入手机号",isPassword:false,inputController:phoneController),
注意使用的时候参数需要加类型
获取输入框的值
void _mylogin() async{
print({'手机号': phoneController, '密码': passwordController.text});
if(phoneController.text == ''){
showToast("请输入手机号码");
return;
}
if(passwordController.text == ''){
showToast("请输入密码");
return;
}
var data= {
'Identifier': phoneController.text,
'Credential': passwordController.text
};
SharedPreferences _prefs = await SharedPreferences.getInstance();
await post('Login/UserLogin',formData:params).then((val){
print('dddddd:>>>>>>>>>>>>>-----------------------------------$val');
_prefs.setString('mobile',phoneController.text);
});
}
注意一些创建布局的方法以及逻辑方法要与Widget build并列
Widget build(BuildContext context) {//..}
void _mylogin() async{//...}
Widget _createLogo() {//...}
flutter 自定义输入框组件的更多相关文章
- Flutter 中的常见的按钮组件 以及自定义按钮组件
Flutter 里有很多的 Button 组件很多,常见的按钮组件有:RaisedButton.FlatButton. IconButton.OutlineButton.ButtonBar.Float ...
- 移动端自定义输入框的vue组件 ----input
<style scoped lang="less"> .keyboard { font-family: -apple-system, BlinkMacSystemFon ...
- DateBox( 日期输入框) 组件
本节课重点了解 EasyUI 中 DateBox(日期输入框)组件的使用方法,这个组件依赖于 Combo(自定义下拉框)和 Calendar(日历). 一. 加载方式//class 加载方式<i ...
- 基于JQ的自定义弹窗组件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 基于Vue的数字输入框组件开发
1.概述 Vue组件开发的API:props.events和slots 2.组件代码 github地址:https://github.com/MengFangui/VueInputNumber 效果: ...
- 第二百一十五节,jQuery EasyUI,DateBox(日期输入框)组件
jQuery EasyUI,DateBox(日期输入框)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 DateBox(日期输入框)组件的使 ...
- 第二百一十三节,jQuery EasyUI,NumberBox(数值输入框)组件
jQuery EasyUI,NumberBox(数值输入框)组件 功能:只能输入数值,和各种数值的计算 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI ...
- vue自定义select组件
1.目的 看了很多element-ui的源码,决定自己实现一个简单的select组件,遇到的几个难点,便记录下来. 2.难点一 element-ui中的select组件通过v-model可以绑定数据, ...
- [Input-number]数字输入框组件
需求 加.减按钮 初始值 最大.最小值 数值改变时,触发一个自定义事件来通知父组件 目录文件 index.html 入口页 input-number.js 数字输入框组件 index.js 根实例 实 ...
随机推荐
- VMWare 鼠标无法点击 的问题
今日发现在VMWare虚拟机中点击鼠标没有反应,但是鼠标hover.键盘都是正常的. 还表现为,如果从外部环境激活鼠标,然后移至虚拟机区域,快速双击,则可以捕获为单击. 这个问题在网络上未见有明确解释 ...
- 【原创】大叔问题定位分享(13)HBase Region频繁下线
问题现象:hive执行sql报错 select count(*) from test_hive_table; 报错 Error: java.io.IOException: org.apache.had ...
- Mac OSX 系统搭建 Java 开发环境
转载:https://www.cnblogs.com/zjx2711904647/p/7735556.html 1. 安装JDK 双击jdk-9.0.1_osx-x64_bin.dmg文件进行安装 2 ...
- kali linux networking scanning Cookbok (第三章结尾笔记)
1.Zombie Scanning with Nmap Zombie scans can also be performed with an option in Namp , we can find ...
- Penettation testing with the bush Shell
1. Network Reconnaissance first we can use the command to gather the site information by whois eg : ...
- 详解 CAP 定理 Consistency(一致性)、 Availability(可用性)、Partition tolerance(分区容错性)
CAP原则又称CAP定理,指的是在一个分布式系统中,Consistency(一致性). Availability(可用性).Partition tolerance(分区容错性),三者不可得兼. 分布式 ...
- 关于Idea模块化部署web项目,Web Resource Directories作用
问题由来:接到某个所谓“将web工程中部分代码抽出打包,但待打包部分代码还需要在现场部署时能做微调”的需求. 解决方法:将待打包部分代码作为一个module,让工程依赖该模块,满足抽离打包与现场可调试 ...
- freemarker是什么东西?
前言 由于考虑到网站访问量,以及tocmat可能承受的最大访问压力,我们需要引进一些比较好的技术,来解决这个问题.所以在项目快要结束之际又收到消息,我们要考虑到这些问题然后对现在的项目进行改进,于是就 ...
- 4337: BJOI2015 树的同构
题解: 树的同构的判定 有根树从根开始进行树hash 先把儿子的f进行排序 $f[i]=\sum_{j=1}^{k} { f[j]*prime[j]} +num[i]$(我没有仔细想这样是不是树是唯一 ...
- 【转载】ImportFbx Errors
[转自http://blog.csdn.net/chenggong2dm/article/details/39580735] 问题: 在导入动作的时候出现一个错误: ImportFBX Errors: ...