DEV Community

Dhany Ms
Dhany Ms

Posted on

Dasar-dasar Widget di Flutter

****# Dasar-Dasar Widget di Flutter

Flutter adalah framework yang menggunakan konsep widget sebagai dasar dari UI (User Interface). Dalam Flutter, segala sesuatu adalah widget, mulai dari layout, tombol, hingga animasi.

1. Apa Itu Widget?

Widget adalah elemen dasar dalam Flutter yang membentuk tampilan aplikasi. Ada dua jenis widget utama:

  • StatelessWidget: Widget yang tidak berubah (immutable). Contohnya adalah Text, Icon, dan Container.
  • StatefulWidget: Widget yang dapat berubah saat aplikasi berjalan (memiliki state). Contohnya adalah TextField, Checkbox, dan AnimatedContainer.

2. Contoh StatelessWidget

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Stateless Widget')),
        body: Center(
          child: Text('Halo, Flutter!'),
        ),
      ),
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

3. Contoh StatefulWidget

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int counter = 0;

  void incrementCounter() {
    setState(() {
      counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Stateful Widget')),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('Counter: $counter', style: TextStyle(fontSize: 20)),
              SizedBox(height: 20),
              ElevatedButton(
                onPressed: incrementCounter,
                child: Text('Tambah'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

4. Widget Umum yang Sering Digunakan

Berikut beberapa widget dasar yang sering digunakan dalam Flutter:

1. Text

Digunakan untuk menampilkan teks.

Text('Halo, Flutter!', style: TextStyle(fontSize: 20, color: Colors.blue))
Enter fullscreen mode Exit fullscreen mode

2. Container

Widget serbaguna untuk menampung elemen lain dan dapat diatur warna, margin, padding, dll.

Container(
  width: 100,
  height: 100,
  color: Colors.blue,
  child: Center(child: Text('Kotak')),
)
Enter fullscreen mode Exit fullscreen mode

3. Row & Column

Digunakan untuk mengatur layout secara horizontal (Row) atau vertikal (Column).

Row(
  mainAxisAlignment: MainAxisAlignment.center,
  children: [
    Icon(Icons.star, color: Colors.yellow),
    SizedBox(width: 10),
    Text('Rating: 5.0')
  ],
)
Enter fullscreen mode Exit fullscreen mode

4. ElevatedButton

Tombol dengan efek elevasi.

ElevatedButton(
  onPressed: () => print('Tombol ditekan!'),
  child: Text('Klik Saya'),
)
Enter fullscreen mode Exit fullscreen mode

5. Image

Digunakan untuk menampilkan gambar dari file lokal atau internet.

Image.asset('assets/logo.png', width: 100, height: 100)
Enter fullscreen mode Exit fullscreen mode

5. Kesimpulan

Flutter menggunakan konsep widget untuk membangun UI aplikasi. Memahami perbedaan antara StatelessWidget dan StatefulWidget serta berbagai widget dasar sangat penting bagi pengembang Flutter.

Dengan dasar-dasar ini, Mimin bisa mulai mengembangkan aplikasi Flutter dengan lebih percaya diri! 🚀

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️