Máy tính tiền boa: Mỗi người
Thuộc phần Tạo tính tương tác trong hành trình Flutter của Coddy. Bài học 33 trên 34.
Bây giờ là đáp án cho câu hỏi mà ứng dụng hướng đến: mỗi người trả bao nhiêu. Giá trị này được tính từ mọi thứ khác, nên đây là một final nữa ở đầu build, sau tip:
final double each = (_bill + tip) / _people;Dấu ngoặc đơn rất quan trọng. Dart thực hiện phép chia trước phép cộng, giống như trong môn toán, nên _bill + tip / _people sẽ chỉ chia tiền boa.
Số tiền có ký hiệu đô la, và trong chuỗi Dart, $ mở đầu cho một biến. Để hiển thị chính ký hiệu đó, hãy đặt dấu gạch chéo ngược phía trước: \$. Sau đó là ${...} thực sự:
Text('Each person pays \$${each.toStringAsFixed(2)}')Thử thách
Dễ- Ở đầu
build, hãy tính số tiền mỗi người phải trả: tiền hóa đơn cộng tiền boa, chia đều cho mọi người. - Ở cuối, hiển thị kết quả bằng chữ đậm, cỡ
28:Each person pays $44.00với hóa đơn 40, tiền boa 10% và 1 người.
Tự làm thử
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;
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++;
});
},
),
],
),
],
),
),
);
}
}
Bài học này có một bài trắc nghiệm ngắn. Bắt đầu bài học để trả lời và theo dõi tiến độ.
Tất cả bài học trong Tạo tính tương tác
5Quyết định
bool và toán tử ? :Quyết định với if và elseAnd, Or và Not trong DartHiện và ẩn widgetLàm mờ nút8Dự án: Máy tính tiền boa
Máy tính tiền boa: Hóa đơnMáy tính tiền boa: Tiền boaTự luyện tập: Trình biên dịch Flutter online