解决方法 试试吧!
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选择卡?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)