Tip Calculator: Reset
Part of the Making It Interactive section of Coddy's Flutter journey. Lesson 34 of 34.
The last touch is a Reset button that starts over: empty field, 10% tip, 1 person.
The numbers are in the State, so setState can put them back. The field is the tricky part: it keeps its own text, and setting _bill to 0 does not empty it. You would see 40 in the field while the app works with 0. To empty it, the field needs a controller, as in Read a Field With a Controller:
final TextEditingController _billController = TextEditingController();
void _reset() {
_billController.clear();
setState(() {
_bill = 0;
// and the tip and the people
});
}The field keeps its onChanged: the controller is only there so _reset can clear it. Remember to dispose it.
Challenge
Easy- Give the field a
TextEditingControllernamed_billController, and dispose it. - At the bottom, add a
TextButtonlabelledResetthat empties the field and puts the bill back to 0, the tip to 10 and the people to 1.
Try it yourself
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
double _bill = 0;
double _tipPercent = 10;
int _people = 1;
@override
Widget build(BuildContext context) {
final double tip = _bill * _tipPercent / 100;
final double each = (_bill + tip) / _people;
return Scaffold(
appBar: AppBar(title: const Text('Tip Calculator')),
body: Padding(
padding: const EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextField(
keyboardType: const TextInputType.numberWithOptions(decimal: true),
decoration: const InputDecoration(
labelText: 'Bill',
border: OutlineInputBorder(),
),
onChanged: (String value) {
setState(() {
_bill = double.tryParse(value) ?? 0;
});
},
),
const SizedBox(height: 8),
Text('Bill: ${_bill.toStringAsFixed(2)}'),
const SizedBox(height: 24),
Text('Tip: ${_tipPercent.round()}%', style: const TextStyle(fontSize: 20)),
Slider(
value: _tipPercent,
min: 0,
max: 30,
divisions: 6,
onChanged: (double value) {
setState(() {
_tipPercent = value;
});
},
),
Text('Tip amount: ${tip.toStringAsFixed(2)}'),
const SizedBox(height: 24),
Row(
children: [
Text('People: $_people', style: const TextStyle(fontSize: 20)),
const SizedBox(width: 16),
IconButton(
icon: const Icon(Icons.remove),
onPressed: _people > 1
? () {
setState(() {
_people--;
});
}
: null,
),
IconButton(
icon: const Icon(Icons.add),
onPressed: () {
setState(() {
_people++;
});
},
),
],
),
const SizedBox(height: 24),
Text(
'Each person pays \$${each.toStringAsFixed(2)}',
style: const TextStyle(fontSize: 28, fontWeight: FontWeight.bold),
),
],
),
),
);
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Making It Interactive
5Decisions
bool and the ? : OperatorDecisions With if and elseAnd, Or and Not in DartShow and Hide a WidgetGrey Out a Button8Project: Tip Calculator
Tip Calculator: The BillTip Calculator: The TipPractice on your own: Online Flutter compiler