text
stringlengths
1
372
}
class MyAppState extends State<MyApp> {
final items = List<String>.generate(20, (i) => 'item ${i + 1}');
@override
widget build(BuildContext context) {
const title = 'dismissing items';
return MaterialApp(
title: title,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
),
home: scaffold(
appBar: AppBar(
title: const text(title),
),
body: ListView.builder(
itemCount: items.length,
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')));
},
// show a red background as the item is swiped away.
background: container(color: colors.red),
child: ListTile(
title: text(item),
),
);
},
),
),
);
}
}
<code_end>
<topic_end>
<topic_start>
input & forms
<topic_end>
<topic_start>
topics
<topic_end>
<topic_start>
create and style a text field
text fields allow users to type text into an app.
they are used to build forms,
send messages, create search experiences, and more.
in this recipe, explore how to create and style text fields.
flutter provides two text fields:
TextField and TextFormField.
<topic_end>
<topic_start>
TextField
TextField is the most commonly used text input widget.
by default, a TextField is decorated with an underline.
you can add a label, icon, inline hint text, and error text by supplying an
InputDecoration as the decoration
property of the TextField.
to remove the decoration entirely (including the
underline and the space reserved for the label),
set the decoration to null.
<code_start>
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'enter a search term',
),
),
<code_end>
to retrieve the value when it changes,
see the handle changes to a text field recipe.
<topic_end>
<topic_start>
TextFormField
TextFormField wraps a TextField and integrates it
with the enclosing form.
this provides additional functionality,
such as validation and integration with other
FormField widgets.
<code_start>
TextFormField(
decoration: const InputDecoration(
border: UnderlineInputBorder(),
labelText: 'enter your username',
),
),
<code_end>
<topic_end>