Flutter AppBar Modifikasi
Flutter AppBar adalah sebuah widget dari flutter yang berada di bagian atas suatu aplikasi mobile yang biasanya berisi menu, title, toolbar widgets, leading. biasanya AppBar di bungkus didalam scaffold dan bisa di beri property seperti : actions, actionsIconTheme, automaticallyImplyLeading, backgroundColor, bottom, bottomOpacity, centerTitle, clipBehavior, elevation, excludeHeaderSemantics, flexibleSpace,
forceMaterialTransparency, foregroundColor, hashCode, iconTheme, key, leading, leadingWidth, notificationPredicate, preferredSize, primary, runtimeType, scrolledUnderElevation,
shadowColor, shape, surfaceTintColor, systemOverlayStyle, title, titleSpacing, titleTextStyle, toolbarHeight, toolbarOpacity,toolbarTextStyle
Bagaimanakah cara membuat appbar di flutter ?
Pertama tentu kita harus membuat dahulu project baru, Buka VScode lalu pilih menu Terminal-> New terminal lalu ketikan dengan perintah “flutter create app789” untuk membuat project baru dengan nama app789
lalu masuk ke project kita dengan perintah “cd app789“, karena saya menggunakan VScode anda bisa langsung menjalankan project yang baru kita buat dengan cara memilih menu Run->start_debugging jika tidak ada error akan muncul seperti gambar ini
Contoh Flutter AppBar
Dibawah ini ada beberapa contoh appbar, tapi sebelumnya ada tahapan awal sebelum melanjutkan masuk kedalam coding appbar yaitu :
- Cara menggunakan image dari URL di Flutter adalah dengan menggunakan NetworkImage, contoh penggunaannya sebagai berikut :
Contoh AppBar Pertama
pada contoh appbar yang pertama ini kita akan menambahkan icon rstaurant, icon seacrh, icon more_vert dan logo, disini kita juga akan memberi warna background dari Appbar kita
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// TRY THIS: Try running your application with "flutter run". You'll see
// the application has a blue toolbar. Then, without quitting the app,
// try changing the seedColor in the colorScheme below to Colors.green
// and then invoke "hot reload" (save your changes or press the "hot
// reload" button in a Flutter-supported IDE, or press "r" if you used
// the command line to start the app).
//
// Notice that the counter didn't reset back to zero; the application
// state is not lost during the reload. To reset the state, use hot
// restart instead.
//
// This works for code too, not just values: Most code changes can be
// tested with just a hot reload.
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
//title: const Text("Lemongrass"),
backgroundColor: const Color.fromARGB(255, 26, 25, 25),
leading: GestureDetector(
onTap: () {/* Write listener code here */},
child: const Icon(
Icons.restaurant, // add custom icons also
),
),
//set warna icon
iconTheme: IconThemeData(
color: Colors.white, //change your color here
),
actions: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 55.0),
child: Image.asset(
'assets/images/logo.png',
scale: 6,
),
),
Padding(
padding: const EdgeInsets.only(right: 20.0),
child: GestureDetector(
onTap: () {},
child: const Icon(
Icons.search,
size: 26.0,
),
)),
Padding(
padding: const EdgeInsets.only(right: 20.0),
child: GestureDetector(
onTap: () {},
child: const Icon(Icons.more_vert),
)),
],
),
);
}
}
class CustomAppBarShape extends ContinuousRectangleBorder {
final double multi;
const CustomAppBarShape({this.multi = 0.1});
@override
Path getOuterPath(Rect rect, {TextDirection? textDirection}) {
double height = rect.height;
double width = rect.width;
var path = Path();
path.lineTo(0, height + width * multi);
path.arcToPoint(
Offset(width * multi, height),
radius: Radius.circular(width * multi),
);
path.lineTo(width * (1 - multi), height);
path.arcToPoint(
Offset(width, height + width * multi),
radius: Radius.circular(width * multi),
);
path.lineTo(width, 0);
path.close();
return path;
}
}
Jika berhasil maka akan tampil Appbar seperti dibawah ini
Contoh AppBar Kedua
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// TRY THIS: Try running your application with "flutter run". You'll see
// the application has a blue toolbar. Then, without quitting the app,
// try changing the seedColor in the colorScheme below to Colors.green
// and then invoke "hot reload" (save your changes or press the "hot
// reload" button in a Flutter-supported IDE, or press "r" if you used
// the command line to start the app).
//
// Notice that the counter didn't reset back to zero; the application
// state is not lost during the reload. To reset the state, use hot
// restart instead.
//
// This works for code too, not just values: Most code changes can be
// tested with just a hot reload.
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// memberi warna background
backgroundColor: Color.fromARGB(255, 48, 48, 48),
//setting title di tengah" / center
centerTitle: true,
//menambah title dan memberi warna textnya
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(
'assets/images/logo.png',
scale: 8,
alignment: Alignment.topLeft,
),
],
),
actions: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 20.0),
child: Text(
'Hello Guest',
textAlign: TextAlign.right,
style: TextStyle(
color: Color.fromARGB(255, 252, 251, 251),
), //<-- S
)),
],
//menambah icon
leading: const Icon(Icons.restaurant),
//set warna icon
iconTheme: IconThemeData(
color: Colors.white, //change your color here
),
//menambah shape dengan class CustomAppBarShape,untuk class ada dibawah
shape: const CustomAppBarShape(multi: 0.05),
),
);
}
}
class CustomAppBarShape extends ContinuousRectangleBorder {
final double multi;
const CustomAppBarShape({this.multi = 0.1});
@override
Path getOuterPath(Rect rect, {TextDirection? textDirection}) {
double height = rect.height;
double width = rect.width;
var path = Path();
path.lineTo(0, height + width * multi);
path.arcToPoint(
Offset(width * multi, height),
radius: Radius.circular(width * multi),
);
path.lineTo(width * (1 - multi), height);
path.arcToPoint(
Offset(width, height + width * multi),
radius: Radius.circular(width * multi),
);
path.lineTo(width, 0);
path.close();
return path;
}
}
Jika berhasil akan tampak seperti gambar dibawah ini
Contoh Appbar Ketiga
Widget AppBar dapat kita berikan border radius dan background images sehingga AppBar jadi lebih menarik, berikut contoh AppBar yang ke 3
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// TRY THIS: Try running your application with "flutter run". You'll see
// the application has a blue toolbar. Then, without quitting the app,
// try changing the seedColor in the colorScheme below to Colors.green
// and then invoke "hot reload" (save your changes or press the "hot
// reload" button in a Flutter-supported IDE, or press "r" if you used
// the command line to start the app).
//
// Notice that the counter didn't reset back to zero; the application
// state is not lost during the reload. To reset the state, use hot
// restart instead.
//
// This works for code too, not just values: Most code changes can be
// tested with just a hot reload.
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: PreferredSize(
preferredSize: const Size.fromHeight(120),
child: AppBar(
centerTitle: true,
flexibleSpace: ClipRRect(
borderRadius: const BorderRadius.only(
bottomRight: Radius.circular(50),
bottomLeft: Radius.circular(50)),
child: Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: NetworkImage(
'https://drive.google.com/uc?export=download&id=1Kbdez5JbjC8oLMtn51mYSPSMKBobt9HW'),
fit: BoxFit.fill,
)),
),
),
title: const Text(
'LEMONGRASS',
style: TextStyle(color: Color.fromARGB(255, 170, 170, 170)),
),
leading: GestureDetector(
onTap: () {/* Write listener code here */},
child: const Icon(
Icons.restaurant, // add custom icons also
),
),
//set warna icon
iconTheme: const IconThemeData(
color:
Color.fromARGB(255, 170, 170, 170), //change your color here
),
actions: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 20.0),
child: GestureDetector(
onTap: () {},
child: const Icon(
Icons.more_vert,
size: 26.0,
),
)),
],
backgroundColor: Colors.amber,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.only(
bottomRight: Radius.circular(50),
bottomLeft: Radius.circular(50)),
),
)),
);
}
}
class CustomAppBarShape extends ContinuousRectangleBorder {
final double multi;
const CustomAppBarShape({this.multi = 0.1});
@override
Path getOuterPath(Rect rect, {TextDirection? textDirection}) {
double height = rect.height;
double width = rect.width;
var path = Path();
path.lineTo(0, height + width * multi);
path.arcToPoint(
Offset(width * multi, height),
radius: Radius.circular(width * multi),
);
path.lineTo(width * (1 - multi), height);
path.arcToPoint(
Offset(width, height + width * multi),
radius: Radius.circular(width * multi),
);
path.lineTo(width, 0);
path.close();
return path;
}
}
Jika di jalankan di emulator akan tampak seperti gambar dibawah ini
Demikian tutorial Flutter Appbar kali ini, semoga dapat memberikan manfaat…