Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
120 views

Slideshow Modul 1 - Hello Flutter

This document provides instructions for creating a first Flutter application called "Hello World". It discusses the necessary technical requirements and introduces some basic Dart and Flutter concepts. It explains how to create a new Flutter project, make some modifications to the main.dart file to display text, and run the application. The goal is to understand how to use basic widgets like Scaffold, AppBar, and Text to build a simple single screen app with a message that is displayed when a button is clicked.

Uploaded by

Syarif Irfan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views

Slideshow Modul 1 - Hello Flutter

This document provides instructions for creating a first Flutter application called "Hello World". It discusses the necessary technical requirements and introduces some basic Dart and Flutter concepts. It explains how to create a new Flutter project, make some modifications to the main.dart file to display text, and run the application. The goal is to understand how to use basic widgets like Scaffold, AppBar, and Text to build a simple single screen app with a message that is displayed when a button is clicked.

Uploaded by

Syarif Irfan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Modul 1: Hello Flutter

Fitri Wibowo

Materi diambil dari e-book “Flutter Project” karya Simone Alessandria


Goal

► By the end of this chapter, you'll


be able to build the presentation
screen of the Hello World Travel
company
Langkah yang harus dipenuhi

► Understanding the Dart language basics


► Creating your first Flutter app:
► Using some basic widgets: Scaffold, AppBar, RaisedButton, and Text
► Downloading an image and showing it to the user
► Responding to a button click and showing a dialog
Kebutuhan teknis

► A PC with a recent Windows version, or a Mac with a recent version of the


macOS or Linux operating system.
► An Android/iOS setup. You'll need to set up your Android and iOS
environments to build apps.
► The Flutter SDK. It's free, light, and open source.
► Physical device/emulator/simulator. In order to try your code, you will need
an Android or iOS device. Alternatively, you can also install an Android
emulator or iOS simulator.
► Your favorite editor. The supported editors at this time are:
► Android Studio/IntelliJ IDEA
► Visual Studio Code
Bahasa pemrograman Dart

► It's easy to learn: If you have some knowledge of Java, C#, or JavaScript, Dart
will be extremely easy to learn, as you'll see in the next few pages.
► It's aimed at productivity: Its syntax is exceptionally concise and easy to read
and debug.
► It can transpile to JavaScript, in order to maximize compatibility with web
development.
► It has a general purpose: You can use it for client-side, server-side, and
mobile development.
► As an added bonus, Google is deeply involved in this project and has some big
plans for Dart, including a new operating system, called Google Fuchsia.
Hello dart

► https://dartpad.dev/
► Write the following code & run

void main() {
String name = "Dart";
print("Hello $name!");
}
Program penghitung luas

► There are two types of numbers in Dart:


► int: Contains integer values no larger than 64 bits
► double: Contains 64 -bit, double-precision floating-point numbers
► Since Dart 2.1, the int literals are automatically converted to doubles; for
example, you can write: double value = 2;. This is instead of having to write:
double value = 2.0;.
double calculateArea (double width, double height, [bool isTriangle])
► calculateArea function: {
double area;
if (isTriangle) {
area = width * height / 2;
} else {
area = width * height;
}
return area;
}
Program penghitung luas (lanjutan)

► main() method:
void main() {
double result = calculateArea (12, 5, false);
print('The result for a rectangle is ' + result.toString());
result = calculateArea (12, 5, true);
print('The result for a triangle is ' + result.toString());
}
Program penghitung luas (lanjutan)

► Result:
Foor loop dan string

► Dart supports the same loops as many other C-influenced languages: the for,
while, and do while loops
► An example, a program that reverse a string.
► main() method:
void main() {
String myString = 'Throw your Dart' ;
String result = reverse(myString) ;
print(result);
}
Foor loop dan string (lanjutan)

► reverse() method: ► result:

String reverse(String old) {


int length = old.length;
String res = '';
for (int i = length - 1; i >= 0; i--)
{
res += old.substring(i, i + 1);
}
return res;
}
Sintaks “panah” dan operator ternary

► The arrow operator “=>” is a shortcut to simplify writing a method.


► Contoh:

► Ternary operator:
bool convertToBoolLong (int value)
{
if (value == 1) {
return false; bool convertToBool (int value) => (value == 1) ? false : true;
} else {
return true;
}
While loop, list, dan generic
► To define a collection in Dart, you use List objects. It’s called array in
another programming language.
► Example: void main() {
String mySongs = sing();
print(mySongs);
}

String sing() {
var songs = List<String>();
var songString = '';
songs.add('We will Rock You');
songs.add('One');
songs.add('Sultans of Swing');
int i = 0;
while (i < songs.length) {
songString += '${songs[i]} - ';
i++;
}
return songString;
}
While loop, list, dan generic (lanjutan)

► Result:
foreach()

► The for and while loops can be generally used for any type of loop, but lists
also have some specific methods that help us write elegant and readable
code.
► The foreach() method of a lists lets us run a function on each element in the
array.
► In the previous code, you could delete while loop and use this code instead
songs.forEach((song) => songString += song + " - ");
map()
► The map() method transforms each element in a list and return the result of the
transformation list.
► Example: void main() {
String mySongs = sing();
print(mySongs);
}

String sing() {
var songs = List<String>();
songs.add('We will Rock You');
songs.add('One');
songs.add('Sultans of Swing');
var capitalSongs = songs.map((song) => song.toUpperCase());
return capitalSongs.toString();
}

► Result:
where()

► Take a look at the example:


void main() {
String mySongs = sing();
print(mySongs);
}

String sing() {
var songs = List<String>();
songs.add('We will Rock You' );
songs.add('One');
songs.add('Sultans of Swing' );
var wSongs = songs.where((song) => song.contains('w'));
return wSongs.toString();
}
► The where() method only returns the elements that satisfy song.contains(‘w’)
Classes and objects

► Example:

class Person {
String name;
String surname;
}

main() {
Person clark = Person();
clark.name = 'Clark';
clark.surname = 'Kent';
print('${clark.name} ${clark.surname}');
}
► In Dart, there are no identifiers such as private or public. Each property of a class
is considered public unless its name begins with an underscore character (_)
Menggunakan getter and setter
► Example: class Person {
String name, surname;
int _age;
set age(int years) {
if (years > 0 && years < 120) {
_age = years;
} else {
_age = 0;
}
}

int get age {


return _age;
}
}

main() {
Person clark = Person();
clark.name = 'Clark';
clark.surname = 'Kent';
clark.age = 30;
print('${clark.name} ${clark.surname} ${clark.age}');
}
Constructors

► Example: main() {
Person clark = Person('Clark', 'Kent');
print('${clark.name} ${clark.surname}');
}

class Person {
String name, surname;
Person(String name, String surname) {
this.name = name;
this.surname = surname;
}
}
this keyword

► Example:

main() { main() {
Person clark = Person('Clark', 'Kent'); Person clark = Person('Clark', 'Kent');
print('${clark.name} ${clark.surname}'); print('${clark.name} ${clark.surname}');
} }

class Person { class Person {


String name, surname; String name, surname;
Person(String name, String surname) { Person(this.name, this.surname);
this.name = name; }
this.surname = surname;
}
}
Membuat aplikasi Flutter pertamamu

► A flutter application is made of widgets


► Widgets are the description of a part of the user interface.
► In Flutter almost everything is a Widget.
► We will use Dart to write widgets.
► The app we’ll build is a single screen app, with some text, a picture, and a
button that, when clicked gives the user a message.
► From those simple app, we’ll learn how to use a widgets, styling a text
downloading images from a web, and the creation of alerts.
Menjalankan “Hello World” Flutter

1. Buka terminal dan ketik flutter doctor


Menjalankan “Hello World” Flutter
(lanjutan)
2. Ketik dan jalankan flutter create nama_project :

3. Untuk melihat menjalankan project yang telah dibuat ketik:

4. Hasil
Menjalankan “Hello World” Flutter
(lanjutan)
5. Buka project di code editor, dan ubah isi lib/main.dart menjadi:
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {


@override
Widget build(BuildContext context) {
return Center(
child: Text(
'Hello World Travel',
textDirection: TextDirection.ltr,
),
);
}
}
Menggunakan MaterialApp dan
Scaffold
► Ubah kode main.dart dengan kode berikut dan jalankan:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {


@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Hello World Travel Title",
home: Center(child: Text('Hello World Travel')));
}
}
Menggunakan MaterialApp dan
Scaffold (lanjutan)
► Ubah kode main.dart dengan kode berikut dan jalankan:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {


@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Hello World Travel Title",
home: Scaffold(
appBar: AppBar(title: Text("Hello World Travel App")),
body: Center(child: Text('Hello World Travel'))));
}
}
Format Text menggunakan Columns

body: Center(
► Ubah kode class child: Column(children: [
MyApp pada property Text(
body di main.dart 'Hello World Travel' ,
dengan kode berikut: style: TextStyle(
fontSize : 26,
fontWeight : FontWeight .bold,
color : Colors.blue[800]),
),
Text(
'Discover the World' ,
style: TextStyle(fontSize: 20, color: Colors.deepPurpleAccent) ,
)
]))
Menampilkan citra digital dari internet
dengan Image
► Kode untuk menampilkan image dari URL

Image.network(
'https://media-cdn.tripadvisor.com/media/photo-s/0c/6a/e4/48/bukit-
rindu-alam-tanjung.jpg',
height: 250,
),
Menambahkan button

► Kode untuk menambah button

ElevatedButton(
child: Text('Contact Us'),
onPressed: () => true,
),
Menampilkan AlertDialog saat button
di-klik
► Untuk menampilkan AlertDialog membutuhkan beberapa langkah:
► Buat sebuah void method dengan yang menerima parameter BuildContext
► Buat sebuah method didalam method pada langkah 1 misalkan showDialog()
► Setting context
► Setting builder
► Kembalikan properti AlertDialog
► Setting properti AlertDialog
Menampilkan AlertDialog saat button
di-klik (lanjutan)
► Contoh kode showDialog() di dalam void contactUs(BuildContext context)

void contactUs(BuildContext context) {


showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Contact Us'),
content: Text('Mail us at hello@world.com'),
actions: <Widget>[
TextButton(
child: Text('Close'),
onPressed: () => Navigator.of(context).pop(),
)
],
);
},
);
}
Menampilkan AlertDialog saat button
di-klik (lanjutan)
► Ubah method main() menjadi
void main() => runApp(MaterialApp (home: MyApp()));

► Ubah property onPressed() pada ElevatedButton menjadi

ElevatedButton (
child: Text('Contact Us' ),
onPressed : () => contactUs(context),
),
Hasil
Menggunakan padding

► Properti: padding
► Nilai:
► EdgeInsets.all(24) // Semua
► EdgeInsets.only(right:80) // Kanan saja
► EdgeInsets.symmetric(vertical:48.5) // Atas bawah
Menggunakan SingleChildScrollView

► Always check your app in every orientation when developing for mobile.
► To fix bottom overflow:


builder: (context) => SingleChildScrollView(
child: Padding(

End

You might also like