Skip to content

Instantly share code, notes, and snippets.

@manthri-mohan-sai
Created August 20, 2021 05:11
Show Gist options
  • Save manthri-mohan-sai/3e095153b028d988c9342a51bd367cf7 to your computer and use it in GitHub Desktop.
Save manthri-mohan-sai/3e095153b028d988c9342a51bd367cf7 to your computer and use it in GitHub Desktop.
Testing swipe to dismiss functionality.
import 'package:flutter/material.dart';
import 'dart:math' as math;
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
// This widget is the root of your application.
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHome(),
);
}
}
class MyHome extends StatefulWidget {
@override
createState() => _MyHomeState();
}
class _MyHomeState extends State<MyHome> {
final List<String> items =
new List<String>.generate(30, (i) => "Items ${i + 1}");
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Swipe To Dismiss"),
centerTitle: true,
),
body: ListView.builder(
itemCount: items.length,
itemBuilder: (context, int index) {
return Dismissible(
key: Key(items[index]),
onDismissed: (direction) {
items.removeAt(index);
Scaffold.of(context).showSnackBar(new SnackBar(
content: Text("ITEM IS SUCCESSFULLY REMOVED")));
setState((){});
},
background: Container(
color: Color((math.Random().nextDouble() * 0xFFFFFF).toInt())
.withOpacity(1.0),
),
child: ListTile(
title: Text("${items[index]}"),
),
);
}),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment