gpt4 book ai didi

android - 用于选择行和/或元素的 Flutter longPress 事件功能

转载 作者:行者123 更新时间:2023-12-04 16:02:22 25 4
gpt4 key购买 nike

我需要在适用于 Android 和 iOS 的 Flutter 行元素上实现长按选择。有什么帮助吗?

到目前为止我的代码:

class ListElement extends StatelessWidget {
ListElement({this.text, this.name, this.mId, this.animationController});

final String text;
final String name;
final String mId;
final AnimationController animationController;

@override
Widget build(BuildContext context) {
return new GestureDetector(
onTap: () {
Navigator.of(context).push(
new MaterialPageRoute(builder: (BuildContext context) => new DrugProfile(drugmId))
);
},
onLongPress: () {
//HERE I NEED TO SELECT MULTIPLE ROWS IF IT FIRES
},

child: new SizeTransition(
sizeFactor: new CurvedAnimation(
parent: animationController, curve: Curves.easeOut),
axisAlignment: 0.0,
child: new Container(
margin: const EdgeInsets.symmetric(vertical: 10.0),
child: new Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Container(
margin: const EdgeInsets.only(right: 16.0),
child: new CircleAvatar(child: new Text(name[0].toUpperCase())),
),
new Expanded(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Text(name, style: Theme.of(context).textTheme.subhead),
new Container(
margin: const EdgeInsets.only(top: 5.0),
child: new Text(
text,
textAlign: TextAlign.left,
style: new TextStyle(
fontSize: 13.0,),
),
),
],
),
),
],
),
)
)
);
}
}

也许我从 onLongPress 事件开始就错了,但我需要执行以下操作:如果在行元素上长按,则提供选择多行的能力。然后在选择之后对行执行自定义操作(这不是问题的一部分:))在选择之后,我想象了一个索引数组,我可以将它们传递给函数以进行进一步处理。我只需要有关元素的多项选择的帮助。

最佳答案

您可以使用 HashMap 来跟踪列表中的选定项并根据您喜欢的手势更新其值。

这是您可以尝试的示例。在此示例中,多选模式将在长按某个项目时启动。当没有剩余的选择项时,多选模式将停止。

import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@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> {
var selectMode = false;

Map<String, bool> listItemSelected = {
'List 1': false,
'List 2': false,
'List 3': false,
'List 4': false,
'List 5': false,
'List 6': false,
'List 7': false,
'List 8': false,
'List 9': false,
};

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: ListView(
children: listItemSelected.keys.map((key) {
return Card(
child: GestureDetector(
onTap: () {
// if multi-select mode is true, tap should select List item
if (selectMode && listItemSelected.containsValue(true)) {
debugPrint('onTap on $key');
setState(() {
listItemSelected[key] = !listItemSelected[key];
});
} else {
// Stop multi-select mode when there's no more selected List item
debugPrint('selectMode STOP');
selectMode = false;
}
},
// Start List multi-select mode on long press
onLongPress: () {
debugPrint('onLongPress on $key');
if (!selectMode) {
debugPrint('selectMode START');
selectMode = true;
}
setState(() {
listItemSelected[key] = !listItemSelected[key];
});
},
child: Container(
// Change List item color if selected
color: (listItemSelected[key])
? Colors.lightBlueAccent
: Colors.white,
padding: EdgeInsets.all(16.0),
child: Text(key),
),
),
);
}).toList(),
),
),
);
}
}

演示

demo

关于android - 用于选择行和/或元素的 Flutter longPress 事件功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50156231/

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