Menu
CoddyTech

Kalkulator Tip: Setiap Orang

Termasuk bagian Membuat Aplikasi Interaktif dalam perjalanan Flutter di Coddy. Pelajaran 33 dari 34.

Sekarang, jawaban yang dibutuhkan aplikasi: jumlah yang dibayar setiap orang. Nilai ini mengikuti semua hal lainnya, jadi tambahkan satu lagi final di bagian atas build, setelah tip:

final double each = (_bill + tip) / _people;

Tanda kurungnya penting. Dart melakukan pembagian sebelum penjumlahan, seperti dalam pelajaran matematika, jadi _bill + tip / _people hanya akan membagi tip.

Jumlahnya diberi tanda dolar, dan dalam string Dart, $ mengawali sebuah variabel. Untuk menampilkan tandanya sendiri, letakkan garis miring terbalik di depannya: \$. Lalu, ${...} yang sebenarnya menyusul:

Text('Each person pays \$${each.toStringAsFixed(2)}')
challenge icon

Tantangan

Mudah
  • Di bagian atas build, hitung jumlah yang harus dibayar setiap orang: tagihan ditambah tip, lalu dibagi rata di antara semua orang.
  • Di bagian bawah, tampilkan dengan ukuran 28 dan cetak tebal: Each person pays $44.00 untuk tagihan sebesar 40, tip 10%, dan 1 orang.

Coba sendiri

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++;
                    });
                  },
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
quiz iconUji dirimu

Pelajaran ini berisi kuis singkat. Mulai pelajarannya untuk menjawab kuis dan melacak progresmu.

Semua pelajaran di Membuat Aplikasi Interaktif

Latihan sendiri: Compiler Flutter online