[英]How to remove padding of suffixIcon in textfield flutter
I have a function to create a textField我有一个 function 来创建一个 textField
Widget _createNormalTextField(
String hintTxt,
TextEditingController controller,
TextInputType inputType,
String Function(String) validation,
Function(String) onTextChanged,
int length,
Widget suffixIcon) {
return Container(
margin: EdgeInsets.symmetric(vertical: 8),
child: new TextFormField(
controller: controller,
maxLength: length,
keyboardType: inputType,
validator: validation,
onChanged: onTextChanged,
decoration: InputDecoration(
contentPadding: EdgeInsets.only(bottom: -10.0),
suffixIcon: suffixIcon,
hintText: hintTxt,
)),
);
}
and i am creating few textfields like this:我正在创建几个这样的文本字段:
Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(flex: 2, child: Text("Discount")),
SizedBox(
width: 4,
),
Expanded(
flex: 1,
child: _createNormalTextField(
"", _discountPercentController, TextInputType.number, null,
(txt) {
setState(() {
calculateAmount(txt);
});
}, 2, Text("%")),
),
SizedBox(
width: 4,
),
Expanded(
flex: 2,
child: _createNormalTextField(
"Amount",
_discountAmountController,
TextInputType.number,
null,
(txt) {},
5,
null),
),
],
),
],
);
i used '%' Text as suffix icon.我使用 '%' 文本作为后缀图标。 but it creates '%' at middle and takes padding which i dont want.
但它会在中间创建 '%' 并采用我不想要的填充。 i have attached image of what i got and what i want:
我附上了我得到的和我想要的图片:
i got:我有:
i wanted suffix icon at the bottom right, like::我想要右下角的后缀图标,例如::
so does anyone knows workaround for this??那么有人知道解决方法吗?
You can copy paste run full code below您可以在下面复制粘贴运行完整代码
You can use suffixIcon
with suffixIconConstraints
您可以将
suffixIcon
与suffixIconConstraints
一起使用
code snippet代码片段
decoration: InputDecoration(
contentPadding: EdgeInsets.only(bottom: -10.0),
suffixIcon: suffixIcon,
suffixIconConstraints: BoxConstraints(maxHeight: 14),
hintText: hintTxt,
)),
working demo工作演示
full code完整代码
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
TextEditingController _discountPercentController = TextEditingController();
TextEditingController _discountAmountController = TextEditingController();
Widget _createNormalTextField(
String hintTxt,
TextEditingController controller,
TextInputType inputType,
String Function(String) validation,
Function(String) onTextChanged,
int length,
Widget suffixIcon) {
return Container(
margin: EdgeInsets.symmetric(vertical: 8),
child: TextFormField(
controller: controller,
maxLength: length,
keyboardType: inputType,
validator: validation,
onChanged: onTextChanged,
decoration: InputDecoration(
contentPadding: EdgeInsets.only(bottom: -10.0),
suffixIcon: suffixIcon,
suffixIconConstraints: BoxConstraints(maxHeight: 14),
hintText: hintTxt,
)),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(flex: 2, child: Text("Discount")),
SizedBox(
width: 4,
),
Expanded(
flex: 1,
child: _createNormalTextField("", _discountPercentController,
TextInputType.number, null, (txt) {
setState(() {
//calculateAmount(txt);
});
}, 2, Text("%")),
),
SizedBox(
width: 4,
),
Expanded(
flex: 2,
child: _createNormalTextField(
"Amount",
_discountAmountController,
TextInputType.number,
null,
(txt) {},
5,
null),
),
],
),
],
)));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.