Flutter Can';t选择列表中的第一个元素
我用过这个dep。 为了创建可重排序列表,我做了它并对其进行了样式化。我没有使用带有复选框的文本输入,而是生成了10个列表,其中9个是可点击的,但不是第一个Flutter Can';t选择列表中的第一个元素,flutter,dart,drag-and-drop,reorderable-list,Flutter,Dart,Drag And Drop,Reorderable List,我用过这个dep。 为了创建可重排序列表,我做了它并对其进行了样式化。我没有使用带有复选框的文本输入,而是生成了10个列表,其中9个是可点击的,但不是第一个 类DragAndDropSubTasks扩展StatefulWidget{ DragAndDropSubTasks({Key-Key,this.title}):超级(Key:Key); 最后的字符串标题; @凌驾 _DragandDropSubTaskState createState()=>\u DragandDropSubTaskS
类DragAndDropSubTasks扩展StatefulWidget{
DragAndDropSubTasks({Key-Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_DragandDropSubTaskState createState()=>\u DragandDropSubTaskState();
}
类ItemData{
ItemData(this.title,this.key);
最后的字符串标题;
//可重排序列表中的每个项目都需要稳定且唯一的密钥
最后一键;
}
枚举拖动模式{
网间网操作系统,
安卓
}
类_DragandDropSubTaskState扩展状态{
列出项目;
_DragandDropSubTaskState(){
_项目=列表();
对于(int i=0;i<1;i++){
String label=“列表项$i”;
_添加(ItemData(标签、ValueKey(i));
}
}
//返回具有给定键的项的索引
int_indexOfKey(键){
返回_items.indexWhere((ItemData)=>d.key==key);
}
bool\u reorderCallback(键项,键newPosition){
int draggingIndex=_indexOfKey(项目);
int newPositionIndex=_indexOfKey(newPosition);
//取消注释以仅允许偶数目标重新订购
//如果(newPositionIndex%2==1)
//返回false;
最终draggedItem=_项[draggingIndex];
设置状态(){
debugPrint(“重新排序$item->$newPosition”);
_项目移除(拖动索引);
_项目。插入(newPositionIndex,draggedItem);
});
返回true;
}
作废(关键项目){
最终数据=_items[_indexOfKey(item)];
debugPrint(“为${draggedItem.title}}重新排序完成”);
}
//
//重新排序通过在层次结构中具有ReorderableList小部件来工作
//包含ReorderableItems小部件
//
拖动模式_DraggingMode=DraggingMode.iOS;
小部件构建(构建上下文){
返回脚手架(
正文:可重排序列表(
onReorder:这个,
onreordone:这个,
子:自定义滚动视图(
//范围:3000,
条子:[
填缝料(
填充:仅限边缘设置(
底部:MediaQuery.of(context.padding.bottom),
银条:银条列表(
代表:SliverChildBuilderDelegate(
(BuildContext上下文,int索引){
返回容器(
儿童:项目(
数据:_项[索引],
//第一个和最后一个属性会影响拖动期间绘制的边界
isFirst:index==0,
isLast:index==\u items.length-1,
拖动模式:_拖动模式,
),
);
},
childCount:_items.length,
),
),
),
],
),
),
);
}
}
类项扩展了无状态小部件{
项目({
这个数据,,
这是第一次,
这是最后一次,
这是draggingMode,
});
最终项目数据;
最终目标是第一;
最终布尔伊斯兰特;
最终拖动模式拖动模式;
小部件_buildChild(BuildContext上下文,ReorderableItemState状态){
var device=MediaQuery.of(context).size;
盒子装饰;
if(state==ReorderableItemState.dragProxy||
状态==ReorderableItemState.dragProxyFinished){
//略微透明的背景白色拖动(就像在iOS上一样)
}否则{
bool placeholder=state==ReorderableItemState.placeholder;
装饰(
边界:边界(
顶部:isFirst&!占位符
?分隔符。createBorderSide(上下文)//
:BorderSide.none,
底部:isLast和占位符(&P)
?无//
:Divider.createBorderSide(上下文)),
颜色:占位符?空:颜色。白色);
}
//对于iOS拖动mdoe,右侧将有一个拖动手柄触发
//重新排序;对于android模式,它将只是一个空容器
Widget dragHandle=draggingMode==draggingMode.iOS
?可重设可重设的侦听器(
子:容器(
高度:设备高度*39/812,
填充:仅限边设置(右:18.0,左:18.0),
页边距:仅限边集(顶部:7),
装饰:盒子装饰(
borderRadius:仅限新的borderRadius(
右上角:常数半径。圆形(8.0),
右下角:常数半径。圆形(8.0),
),
颜色:color.fromRGBO(249248255,1),
),
child:Center(child:BuildSvg('assets/svg/dragandropicon.svg'),
),
)
:容器();
控件内容=容器(
装饰:装饰,,
儿童:安全区(
上图:错,
底部:错误,
子对象:不透明度(
//隐藏占位符的内容
不透明度:状态==ReorderableItemState.placeholder?0.0:1.0,
孩子:内在的(
孩子:排(
crossAxisAlignment:crossAxisAlignment.stretch,
儿童:[
容器(
对齐:alignment.centerLeft,
填充:仅限边缘设置(左:7),
宽度:设备宽度*270/375,
页边距:仅限边集(顶部:7),
装饰:盒子装饰(
borderRadius:仅限新的borderRadius(
左上角:常数半径。圆形(8.0),
左下角:常数半径。圆形(8.0),
),
颜色:color.fromRGBO(249248255,1),
),
子:堆栈(
class DragAndDropSubTasks extends StatefulWidget {
DragAndDropSubTasks({Key key, this.title}) : super(key: key);
final String title;
@override
_DragAndDropSubTasksState createState() => _DragAndDropSubTasksState();
}
class ItemData {
ItemData(this.title, this.key);
final String title;
// Each item in reorderable list needs stable and unique key
final Key key;
}
enum DraggingMode {
iOS,
Android,
}
class _DragAndDropSubTasksState extends State<DragAndDropSubTasks> {
List<ItemData> _items;
_DragAndDropSubTasksState() {
_items = List();
for (int i = 0; i < 1; i++) {
String label = "List item $i";
_items.add(ItemData(label, ValueKey(i)));
}
}
// Returns index of item with given key
int _indexOfKey(Key key) {
return _items.indexWhere((ItemData d) => d.key == key);
}
bool _reorderCallback(Key item, Key newPosition) {
int draggingIndex = _indexOfKey(item);
int newPositionIndex = _indexOfKey(newPosition);
// Uncomment to allow only even target reorder possition
// if (newPositionIndex % 2 == 1)
// return false;
final draggedItem = _items[draggingIndex];
setState(() {
debugPrint("Reordering $item -> $newPosition");
_items.removeAt(draggingIndex);
_items.insert(newPositionIndex, draggedItem);
});
return true;
}
void _reorderDone(Key item) {
final draggedItem = _items[_indexOfKey(item)];
debugPrint("Reordering finished for ${draggedItem.title}}");
}
//
// Reordering works by having ReorderableList widget in hierarchy
// containing ReorderableItems widgets
//
DraggingMode _draggingMode = DraggingMode.iOS;
Widget build(BuildContext context) {
return Scaffold(
body: ReorderableList(
onReorder: this._reorderCallback,
onReorderDone: this._reorderDone,
child: CustomScrollView(
// cacheExtent: 3000,
slivers: <Widget>[
SliverPadding(
padding: EdgeInsets.only(
bottom: MediaQuery.of(context).padding.bottom),
sliver: SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return Container(
child: Item(
data: _items[index],
// first and last attributes affect border drawn during dragging
isFirst: index == 0,
isLast: index == _items.length - 1,
draggingMode: _draggingMode,
),
);
},
childCount: _items.length,
),
),
),
],
),
),
);
}
}
class Item extends StatelessWidget {
Item({
this.data,
this.isFirst,
this.isLast,
this.draggingMode,
});
final ItemData data;
final bool isFirst;
final bool isLast;
final DraggingMode draggingMode;
Widget _buildChild(BuildContext context, ReorderableItemState state) {
var device = MediaQuery.of(context).size;
BoxDecoration decoration;
if (state == ReorderableItemState.dragProxy ||
state == ReorderableItemState.dragProxyFinished) {
// slightly transparent background white dragging (just like on iOS)
} else {
bool placeholder = state == ReorderableItemState.placeholder;
decoration = BoxDecoration(
border: Border(
top: isFirst && !placeholder
? Divider.createBorderSide(context) //
: BorderSide.none,
bottom: isLast && placeholder
? BorderSide.none //
: Divider.createBorderSide(context)),
color: placeholder ? null : Colors.white);
}
// For iOS dragging mdoe, there will be drag handle on the right that triggers
// reordering; For android mode it will be just an empty container
Widget dragHandle = draggingMode == DraggingMode.iOS
? ReorderableListener(
child: Container(
height: device.height * 39 / 812,
padding: EdgeInsets.only(right: 18.0, left: 18.0),
margin: EdgeInsets.only(top: 7),
decoration: BoxDecoration(
borderRadius: new BorderRadius.only(
topRight: const Radius.circular(8.0),
bottomRight: const Radius.circular(8.0),
),
color: Color.fromRGBO(249, 248, 255, 1),
),
child: Center(child: BuildSvg('assets/svg/DragAndDropIcon.svg')),
),
)
: Container();
Widget content = Container(
decoration: decoration,
child: SafeArea(
top: false,
bottom: false,
child: Opacity(
// hide content for placeholder
opacity: state == ReorderableItemState.placeholder ? 0.0 : 1.0,
child: IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
alignment: Alignment.centerLeft,
padding: EdgeInsets.only(left: 7),
width: device.width * 270 / 375,
margin: EdgeInsets.only(top: 7),
decoration: BoxDecoration(
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(8.0),
bottomLeft: const Radius.circular(8.0),
),
color: Color.fromRGBO(249, 248, 255, 1),
),
child: Stack(
children: <Widget>[
CheckBox('', false, Colors.white),
Container(
margin: EdgeInsets.only(left: 30),
child: TextFormField(
decoration: new InputDecoration(
border: InputBorder.none,
focusedBorder: InputBorder.none,
),
keyboardType: TextInputType.text,
),
),
],
)),
// Triggers the reordering
dragHandle,
],
),
),
)),
);
// For android dragging mode, wrap the entire content in DelayedReorderableListener
if (draggingMode == DraggingMode.Android) {
content = DelayedReorderableListener(
child: content,
);
}
return content;
}
@override
Widget build(BuildContext context) {
return ReorderableItem(
key: data.key, //
childBuilder: _buildChild,
);
}
}