颤抖 – 我想通过onLongPress选择卡?
发布时间:2020-12-14 14:58:11 所属栏目:百科 来源:网络整理
导读:我想通过onLongPress从颤动中选择卡片.但是当我选择单张卡时,所有其他卡都会被选中?我想根据自己的意愿选择卡片然后执行一些操作…… 解决方法 试试吧! import 'package:flutter/material.dart';void main() = runApp(new MyApp());class MyApp extends St
我想通过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(),); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |