From 8dbd7e780ff03afc35d91b5fe232d525438b709d Mon Sep 17 00:00:00 2001 From: lifegpc Date: Sat, 20 Jan 2024 21:56:17 +0800 Subject: [PATCH] Fix bug --- lib/components/string_list_field.dart | 62 ++++++++++++++++----------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/lib/components/string_list_field.dart b/lib/components/string_list_field.dart index 338cd99..b44d85a 100644 --- a/lib/components/string_list_field.dart +++ b/lib/components/string_list_field.dart @@ -47,20 +47,7 @@ class _StringListFormField extends State { } Widget _buildItem(BuildContext context, int index) { - final isLast = index == value.length; - if (isLast) { - return IconButton( - key: lastKey, - onPressed: () { - setState(() { - value.add(""); - keys.add(ValueKey("${parentKey}_${value.length}")); - }); - widget.onChanged?.call(value); - }, - icon: const Icon(Icons.add)); - } - return Row( + Widget item = Row( key: keys[index], children: [ Expanded( @@ -94,6 +81,14 @@ class _StringListFormField extends State { index: index, child: const Icon(Icons.reorder)), ], ); + if (widget.padding != null) { + item = Padding( + key: item.key, + padding: widget.padding!, + child: item, + ); + } + return item; } void onReorder(int oldIndex, int newIndex) { @@ -111,18 +106,8 @@ class _StringListFormField extends State { Widget _buildList(BuildContext context) { Widget list = ReorderableList( - itemBuilder: (context, index) { - final item = _buildItem(context, index); - if (widget.padding == null) { - return item; - } - return Padding( - key: item.key, - padding: widget.padding!, - child: item, - ); - }, - itemCount: value.length + 1, + itemBuilder: _buildItem, + itemCount: value.length, onReorder: onReorder, proxyDecorator: proxyDecorator, shrinkWrap: true); @@ -176,6 +161,30 @@ class _StringListFormField extends State { ); } + Widget _buildAddButton(BuildContext context) { + Widget button = LayoutBuilder(builder: (context, box) { + return SizedBox( + width: box.maxWidth, + child: IconButton( + key: lastKey, + onPressed: () { + setState(() { + value.add(""); + keys.add(ValueKey("${parentKey}_${value.length}")); + }); + widget.onChanged?.call(value); + }, + icon: const Icon(Icons.add))); + }); + if (widget.padding != null) { + button = Padding( + padding: widget.padding!, + child: button, + ); + } + return button; + } + @override Widget build(BuildContext context) { if (value.length != keys.length) { @@ -190,6 +199,7 @@ class _StringListFormField extends State { children: [ _buildLabel(context), _buildList(context), + _buildAddButton(context), _buildHelper(context), ]); }