颤抖 – 我想通过onLongPress选择卡?

颤抖 – 我想通过onLongPress选择卡?,第1张

概述我想通过onLongPress从颤动中选择卡片.但是当我选择单张卡时,所有其他卡都会被选中?我想根据自己的意愿选择卡片然后执行一些 *** 作…… 试试吧! import 'package:flutter/material.dart';void main() => runApp(new MyApp());class MyApp extends StatelessWidget { @overri 我想通过onLongPress从颤动中选择卡片.但是当我选择单张卡时,所有其他卡都会被选中?我想根据自己的意愿选择卡片然后执行一些 *** 作……


解决方法 试试吧!

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(),);  }}
总结

以上是内存溢出为你收集整理的颤抖 – 我想通过onLongPress选择卡?全部内容,希望文章能够帮你解决颤抖 – 我想通过onLongPress选择卡?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1004057.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存