Flutter 我可以用背景自定义文本字段吗?
我希望文本字段搜索图标(后缀)有一个绿色背景,然后我的文本字段使用阴影而不是边框。你能帮我做这个吗?或者这个不能在颤振中实现 这是我的代码:Flutter 我可以用背景自定义文本字段吗?,flutter,flutter-layout,Flutter,Flutter Layout,我希望文本字段搜索图标(后缀)有一个绿色背景,然后我的文本字段使用阴影而不是边框。你能帮我做这个吗?或者这个不能在颤振中实现 这是我的代码: TextField( decoration: InputDecoration( border: OutlineInputBorder( borderRadius: BorderRadius.circular(15),
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(15),
borderSide: BorderSide(
width: 0,
style: BorderStyle.solid,
),
),
contentPadding: EdgeInsets.only(left: 25),
hintText: "Bandung, Jawa Barat",
hintStyle: TextStyle(fontSize: 15, color: Colors.black45),
filled: true,
fillColor: Colors.white,
prefixIcon: Image.asset('assets/img/map.png', height: 15),
suffixIcon: Image.asset('assets/img/search.png', color: Colors.green),
suffixIconConstraints: BoxConstraints(
minWidth: 50,
)
),
)
到
要为
文本字段
小部件获取阴影,一个选项是材质
小部件。用材质
小部件包装文本字段
,该小部件具有高程
、阴影颜色
、边界半径
等属性。它是比具有属性装饰的容器
小部件更干净的阴影选项
输出
复制粘贴以下代码以查看效果:
class CustomTextField extends StatefulWidget {
@override
_CustomTextField State createState() => _CustomTextField State();
}
class _CustomTextField State extends State<CustomTextField > {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(height: 25),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: Material(
elevation: 5.0,
shadowColor: Colors.green,
borderRadius: BorderRadius.circular(15.0),
child: TextFormField(
obscureText: true,
autofocus: false,
decoration: InputDecoration(
hintText: 'Search here',
hintStyle:
TextStyle(fontSize: 16, color: Colors.black45),
fillColor: Colors.white,
filled: true,
prefixIcon: Icon(Icons.person, color: Colors.green),
suffixIcon: Material(
elevation: 5.0,
color: Colors.green,
shadowColor: Colors.green,
borderRadius: BorderRadius.only(
topRight: Radius.circular(15.0),
bottomRight: Radius.circular(15.0),
),
child: Icon(Icons.search, color: Colors.white),
),
contentPadding:
EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(15.0),
borderSide:
BorderSide(color: Colors.white, width: 3.0))),
),
),
),
],
),
),
),
),
);
}
}
类CustomTextField扩展StatefulWidget{
@凌驾
_CustomTextField状态createState()=>\u CustomTextField状态();
}
类_CustomTextField状态扩展状态{
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
正文:安全区(
子:SingleChildScrollView(
子:列(
mainAxisSize:mainAxisSize.min,
儿童:[
尺寸箱(高度:25),
填充物(
填充:常量边集。对称(水平:10.0),
儿童:材料(
标高:5.0,
阴影颜色:颜色。绿色,
边界半径:边界半径。圆形(15.0),
子项:TextFormField(
蒙昧文字:对,
自动对焦:错误,
装饰:输入装饰(
hintText:'在此处搜索',
辛茨风格:
TextStyle(字体大小:16,颜色:Colors.black45),
fillColor:Colors.white,
是的,
前缀:图标(Icons.person,颜色:Colors.green),
足够:材料(
标高:5.0,
颜色:颜色。绿色,
阴影颜色:颜色。绿色,
borderRadius:仅限borderRadius(
右上角:半径。圆形(15.0),
右下角:半径。圆形(15.0),
),
子项:图标(Icons.search,颜色:Colors.white),
),
内容填充:
LTRB(20.0,10.0,20.0,10.0)中的边缘集,
enabledBorder:OutlineInputBorder(
边界半径:边界半径。圆形(15.0),
边界:
边框(颜色:Colors.white,宽度:3.0)),
),
),
),
],
),
),
),
),
);
}
}
要为文本字段
小部件获取阴影,一个选项是材质
小部件。用材质
小部件包装文本字段
,该小部件具有高程
、阴影颜色
、边界半径
等属性。它是比具有属性装饰的容器
小部件更干净的阴影选项
输出
复制粘贴以下代码以查看效果:
class CustomTextField extends StatefulWidget {
@override
_CustomTextField State createState() => _CustomTextField State();
}
class _CustomTextField State extends State<CustomTextField > {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(height: 25),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: Material(
elevation: 5.0,
shadowColor: Colors.green,
borderRadius: BorderRadius.circular(15.0),
child: TextFormField(
obscureText: true,
autofocus: false,
decoration: InputDecoration(
hintText: 'Search here',
hintStyle:
TextStyle(fontSize: 16, color: Colors.black45),
fillColor: Colors.white,
filled: true,
prefixIcon: Icon(Icons.person, color: Colors.green),
suffixIcon: Material(
elevation: 5.0,
color: Colors.green,
shadowColor: Colors.green,
borderRadius: BorderRadius.only(
topRight: Radius.circular(15.0),
bottomRight: Radius.circular(15.0),
),
child: Icon(Icons.search, color: Colors.white),
),
contentPadding:
EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(15.0),
borderSide:
BorderSide(color: Colors.white, width: 3.0))),
),
),
),
],
),
),
),
),
);
}
}
类CustomTextField扩展StatefulWidget{
@凌驾
_CustomTextField状态createState()=>\u CustomTextField状态();
}
类_CustomTextField状态扩展状态{
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
正文:安全区(
子:SingleChildScrollView(
子:列(
mainAxisSize:mainAxisSize.min,
儿童:[
尺寸箱(高度:25),
填充物(
填充:常量边集。对称(水平:10.0),
儿童:材料(
标高:5.0,
阴影颜色:颜色。绿色,
边界半径:边界半径。圆形(15.0),
子项:TextFormField(
蒙昧文字:对,
自动对焦:错误,
装饰:输入装饰(
hintText:'在此处搜索',
辛茨风格:
TextStyle(字体大小:16,颜色:Colors.black45),
fillColor:Colors.white,
是的,
前缀:图标(Icons.person,颜色:Colors.green),
足够:材料(
标高:5.0,
颜色:颜色。绿色,
阴影颜色:颜色。绿色,
borderRadius:仅限borderRadius(
右上角:半径。圆形(15.0),
右下角:半径。圆形(15.0),
),
子项:图标(Icons.search,颜色:Colors.white),
),
内容填充:
LTRB(20.0,10.0,20.0,10.0)中的边缘集,
enabledBorder:OutlineInputBorder(
边界半径:边界半径。圆形(15.0),
边界:
边框(颜色:Colors.white,宽度:3.0)),
),
),
),
],
),
),
),
),
);
}
}
是否可以将背景色添加到后缀(搜索图标)?@RLovanto Yeah。我想是吧<代码>