Created
September 1, 2018 08:32
-
-
Save IshanFx/7773a4e25e853196116c45c61a2054c4 to your computer and use it in GitHub Desktop.
Swipe to dismiss for flutter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
debugShowCheckedModeBanner: false, | |
title: 'Flutter Swipe delete', | |
theme: new ThemeData( | |
primarySwatch: Colors.red, | |
), | |
home: new MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
// TODO: implement build | |
return Scaffold( | |
appBar: AppBar( | |
title: Text("Swipe Delete"), | |
), | |
body: Center(child: SwipeList())); | |
} | |
} | |
class SwipeList extends StatefulWidget { | |
@override | |
State<StatefulWidget> createState() { | |
// TODO: implement createState | |
return CircleWidgets(); | |
} | |
} | |
class CircleWidgets extends State<SwipeList> { | |
List items = getDummyList(); | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
child: ListView.builder( | |
itemCount: items.length, | |
itemBuilder: (context, index) { | |
return Dismissible( | |
direction: DismissDirection.endToStart, | |
key: Key(items[index]), | |
background: Container( | |
alignment: AlignmentDirectional.centerEnd, | |
color: Colors.red, | |
child: Padding( | |
padding: EdgeInsets.fromLTRB(0.0, 0.0, 10.0, 0.0), | |
child: Icon(Icons.delete, | |
color: Colors.white, | |
), | |
), | |
), | |
onDismissed: (direction) { | |
setState(() { | |
items.removeAt(index); | |
}); | |
}, | |
child: Container( | |
height: 50.0, | |
decoration: BoxDecoration(border: Border.all(width: 1.0)), | |
padding: EdgeInsets.all(5.0), | |
child: Row( | |
children: <Widget>[ | |
Text( | |
items[index], | |
style: TextStyle( | |
color: Colors.black, | |
fontSize: 20.0, | |
), | |
) | |
], | |
), | |
), | |
); | |
}, | |
)); | |
} | |
static List getDummyList(){ | |
List list = List.generate(10, (i) { | |
return "Item ${i +1 }"; | |
}); | |
return list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment