gpt4 book ai didi

Flutter - 我想通过 onLongPress 选择卡片?

转载 作者:IT王子 更新时间:2023-10-29 06:37:45 26 4
gpt4 key购买 nike

我想通过 onLongPress 从 flutter 中选择卡片。但是当我选择单张卡时,所有其他卡都被选中了吗?我想根据自己的意愿选择卡片..然后执行一些操作...

enter image description here enter image description here

最佳答案

试试这个!

demo

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'NonStopIO',
theme: new ThemeData(
primarySwatch: Colors.red,
),
home: new MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
bool longPressFlag = false;
List<int> indexList = new List();

void longPress() {
setState(() {
if (indexList.isEmpty) {
longPressFlag = false;
} else {
longPressFlag = true;
}
});
}

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Selected ${indexList.length} ' + indexList.toString()),
),
body: new ListView.builder(
itemCount: 3,
itemBuilder: (context, index) {
return new CustomWidget(
index: index,
longPressEnabled: longPressFlag,
callback: () {
if (indexList.contains(index)) {
indexList.remove(index);
} else {
indexList.add(index);
}

longPress();
},
);
},
),
floatingActionButton: new FloatingActionButton(
onPressed: () {},
tooltip: 'Increment',
child: new Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}

class CustomWidget extends StatefulWidget {
final int index;
final bool longPressEnabled;
final VoidCallback callback;

const CustomWidget({Key key, this.index, this.longPressEnabled, this.callback}) : super(key: key);

@override
_CustomWidgetState createState() => new _CustomWidgetState();
}

class _CustomWidgetState extends State<CustomWidget> {
bool selected = false;

@override
Widget build(BuildContext context) {
return new GestureDetector(
onLongPress: () {
setState(() {
selected = !selected;
});
widget.callback();
},
onTap: () {
if (widget.longPressEnabled) {
setState(() {
selected = !selected;
});
widget.callback();
}
},
child: new Container(
margin: new EdgeInsets.all(5.0),
child: new ListTile(
title: new Text("Title ${widget.index}"),
subtitle: new Text("Description ${widget.index}"),
),
decoration: selected
? new BoxDecoration(color: Colors.black38, border: new Border.all(color: Colors.black))
: new BoxDecoration(),
),
);
}
}

关于Flutter - 我想通过 onLongPress 选择卡片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50462281/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com