text
stringlengths
1
474
final List<Item> items;
String get formattedTotalItemPrice {
final totalPriceCents =
items.fold<int>(0, (prev, item) => prev + item.totalPriceCents);
return '\$${(totalPriceCents / 100.0).toStringAsFixed(2)}';
}
}<code_end>
<topic_end>
<topic_start>Add Material touch ripples
Widgets that follow the Material Design guidelines display
a ripple animation when tapped.Flutter provides the InkWell
widget to perform this effect.
Create a ripple effect using the following steps:
<code_start>// The InkWell wraps the custom flat button widget.
InkWell(
// When the user taps the button, show a snackbar.
onTap: () {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('Tap'),
));
},
child: const Padding(
padding: EdgeInsets.all(12),
child: Text('Flat Button'),
),
)<code_end>
<topic_end>
<topic_start>
Interactive example
<code_start>import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
const title = 'InkWell Demo';
return const MaterialApp(
title: title,
home: MyHomePage(title: title),
);
}
}
class MyHomePage extends StatelessWidget {
final String title;
const MyHomePage({super.key, required this.title});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: const Center(
child: MyButton(),
),
);
}
}
class MyButton extends StatelessWidget {
const MyButton({super.key});
@override
Widget build(BuildContext context) {
// The InkWell wraps the custom flat button widget.
return InkWell(
// When the user taps the button, show a snackbar.
onTap: () {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('Tap'),
));
},
child: const Padding(
padding: EdgeInsets.all(12),
child: Text('Flat Button'),
),
);
}
}<code_end>
<topic_end>
<topic_start>Implement swipe to dismiss
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.