您可以复制下面的完整代码并运行。
步骤 1:您可以使用suffixIconConstraints
来减少填充 https://github.com/flutter/flutter/pull/50058
步骤 2:使用InkWell
包装Icon
并设置Icon
大小。
TextFormField(
autocorrect: false,
decoration: InputDecoration(
prefixIcon: Icon(Icons.add),
prefixIconConstraints: BoxConstraints(
minWidth: 5,
minHeight: 5,
),
isDense: true,
suffixIconConstraints: BoxConstraints(
minWidth: 2,
minHeight: 2,
),
suffixIcon: InkWell(
child: Icon(Icons.clear, size: 14), onTap: () {})),
controller: _controller1,
maxLines: null,
),
演示
![在此输入图像描述](https://istack.dev59.com/S9PMZ.gif)
完整代码
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,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
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> {
int _counter = 0;
TextEditingController myController1 = TextEditingController();
TextEditingController _controller1 = TextEditingController();
TextEditingController _controller2 = TextEditingController();
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: TextFormField(
autocorrect: false,
decoration: InputDecoration(
prefixIcon: Icon(Icons.add),
prefixIconConstraints: BoxConstraints(
minWidth: 5,
minHeight: 5,
),
isDense: true,
suffixIconConstraints: BoxConstraints(
minWidth: 2,
minHeight: 2,
),
suffixIcon: InkWell(
child: Icon(Icons.clear, size: 14), onTap: () {})),
controller: _controller1,
maxLines: null,
),
),
Expanded(
child: TextFormField(
autocorrect: false,
decoration: InputDecoration(
prefixIcon: Icon(Icons.add),
prefixIconConstraints: BoxConstraints(
minWidth: 5,
minHeight: 5,
),
isDense: true,
suffixIconConstraints: BoxConstraints(
minWidth: 2,
minHeight: 2,
),
suffixIcon: InkWell(
child: Icon(Icons.clear, size: 14), onTap: () {})),
controller: _controller2,
maxLines: null,
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}