text stringlengths 1 372 |
|---|
the “swipe to dismiss” pattern is common in many mobile apps. |
for example, when writing an email app, |
you might want to allow a user to swipe away |
email messages to delete them from a list. |
flutter makes this task easy by providing the |
dismissible widget. |
learn how to implement swipe to dismiss with the following steps: |
<topic_end> |
<topic_start> |
1. create a list of items |
first, create a list of items. for detailed |
instructions on how to create a list, |
follow the working with long lists recipe. |
<topic_end> |
<topic_start> |
create a data source |
in this example, |
you want 20 sample items to work with. |
to keep it simple, generate a list of strings. |
<code_start> |
final items = List<String>.generate(20, (i) => 'item ${i + 1}'); |
<code_end> |
<topic_end> |
<topic_start> |
convert the data source into a list |
display each item in the list on screen. users won’t |
be able to swipe these items away just yet. |
<code_start> |
ListView.builder( |
itemCount: items.length, |
itemBuilder: (context, index) { |
return ListTile( |
title: text(items[index]), |
); |
}, |
) |
<code_end> |
<topic_end> |
<topic_start> |
2. wrap each item in a dismissible widget |
in this step, |
give users the ability to swipe an item off the list by using the |
dismissible widget. |
after the user has swiped away the item, |
remove the item from the list and display a snackbar. |
in a real app, you might need to perform more complex logic, |
such as removing the item from a web service or database. |
update the itemBuilder() function to return a dismissible widget: |
<code_start> |
itemBuilder: (context, index) { |
final item = items[index]; |
return dismissible( |
// each dismissible must contain a key. keys allow flutter to |
// uniquely identify widgets. |
key: key(item), |
// provide a function that tells the app |
// what to do after an item has been swiped away. |
onDismissed: (direction) { |
// remove the item from the data source. |
setState(() { |
items.removeAt(index); |
}); |
// then show a snackbar. |
ScaffoldMessenger.of(context) |
.showsnackbar(snackbar(content: text('$item dismissed'))); |
}, |
child: ListTile( |
title: text(item), |
), |
); |
}, |
<code_end> |
<topic_end> |
<topic_start> |
3. provide “leave behind” indicators |
as it stands, |
the app allows users to swipe items off the list, but it doesn’t |
give a visual indication of what happens when they do. |
to provide a cue that items are removed, |
display a “leave behind” indicator as they |
swipe the item off the screen. in this case, |
the indicator is a red background. |
to add the indicator, |
provide a background parameter to the dismissible. |
<topic_end> |
<topic_start> |
interactive example |
<code_start> |
import 'package:flutter/material.dart'; |
void main() { |
runApp(const MyApp()); |
} |
// MyApp is a StatefulWidget. this allows updating the state of the |
// widget when an item is removed. |
class MyApp extends StatefulWidget { |
const MyApp({super.key}); |
@override |
MyAppState createState() { |
return MyAppState(); |
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.