ফ্লাটারে বেশ কিছু সিঙ্গেল চাইল্ড লেআউট উইজেট রয়েছে। এগুলো মূলত কন্টেইনার। এগুলোর ভেতর আমরা যে কোন টেক্সট, ইমেজ সহ অন্যান্য উইজেট যোগ করতে পারি। তবে এগুলোতে একটা চাইল্ড যোগ করা যাবে। আমরা চাইলে এগুলোর মধ্য অন্য মাল্টি চাইল্ড লেআউট উইজেট যেমন লিস্ট, গ্রিড ইত্যাদি যোগ করতে পারব।
কন্টেইনার উইজেট
প্যাডিং, মার্জিন, বর্ডার, ব্যাকগ্রাউন্ড কালার ইত্যাদি যোগ করার জন্য কন্টেইনার (Container) উইজেট ব্যবহার করা হয়। নিচের উদাহরণ দেখিঃ
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(debugShowCheckedModeBanner: false, home: ContainerExample()),
);
}
class ContainerExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Container Example")),
body: Center(
child: Container(
width: 200, // Fixed width
height: 100, // Fixed height
color: Colors.blue, // Background color
child: Center(
child: Text(
"Hello Flutter!",
style: TextStyle(color: Colors.white, fontSize: 18),
),
),
),
),
);
}
}
এখানে Container উইজেট ব্যবহার করে একটা ফিক্সড সাইজ বক্স তৈরি করেছি। যার আউটপুট দেখব এমনঃ
আমরা চাইলে কন্টেইনারে কর্নার রেডিয়াস, বক্স শ্যাডো ইত্যাদি যোগ করতে পারি
Container(
width: 200,
height: 100,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(15), // Rounded corners
boxShadow: [
BoxShadow(
color: Colors.black26,
blurRadius: 10,
spreadRadius: 2,
offset: Offset(4, 4), // Shadow position
)
],
),
child: Center(
child: Text("Stylish Box", style: TextStyle(color: Colors.white)),
),
)
আবার প্যাডিং এবং মার্জিনও যোগ করতে পারিঃ
Container(
margin: EdgeInsets.all(20), // Adds space outside the container
padding: EdgeInsets.all(15), // Adds space inside the container
decoration: BoxDecoration(
color: Colors.orange,
borderRadius: BorderRadius.circular(10),
),
child: Text("Padded Text", style: TextStyle(color: Colors.white)),
)
প্যাডিং কন্টেইনারের ভেতরে স্পেস যোগ করে। মার্জিন কন্টেইনারের বাহিরে স্পেস যোগ করে।
SizedBox উইজেট
সাইজড বক্স দিয়েও ফিক্সড সাইজ বক্স তৈরি করা যায়। SizedBox লাইট ওয়েট। নির্দিষ্ট সাইজের বক্স তৈরির পাশাপাশি দুইটা উইজেটের মাঝে স্পেস যোগ করার জন্যও SizedBox ব্যবহার করা হয়। যেমনঃ
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(home: SizedBoxExample()));
}
class SizedBoxExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("SizedBox Example")),
body: Center(
child: SizedBox(
width: 200, // Fixed width
height: 50, // Fixed height
child: ElevatedButton(
onPressed: () {},
child: Text("Click Me"),
),
),
),
);
}
}
দুইটা উইজেটের মাঝে স্পেস যোগ করাঃ
Column(
children: [
Text("First Text"),
SizedBox(height: 20), // Adds 20 pixels of space
Text("Second Text"),
],
)
Align উইজেট
চাইল্ডকে প্যারেন্টের নির্দিষ্ট পজিশনে রাখার জন্য Align উইজেট ব্যবহার করা হয়। যেমন bottomLeft, topRight ইত্যাদি।
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(home: AlignExample()));
}
class AlignExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Align Widget Example")),
body: Container(
color: Colors.grey[300], // Background color for reference
width: double.infinity,
height: double.infinity,
child: Align(
alignment: Alignment.bottomRight, // Aligns to bottom-right corner
child: Text("Hello, Flutter!")
),
),
);
}
}
উপরের কোড রান করলে দেখব স্ক্রিনের নিচের দিকে ডান কোনায় লেখা রয়েছে Hello, Flutter!।
Center উইজেট
চাইল্ডকে প্যারেন্টের মাঝখানে রাখার জন্য সেন্টার উইজেট ব্যবহার করা হয়। যদিও উপরের উদাহরণ গুলোতে আমরা সেন্টার উইজেট ব্যবহার করেছি। নিচে আরেকটা উদাহরণ দেখিঃ
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(home: CenterExample()));
}
class CenterExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Center Widget Example")),
body: Center(
child: Text(
"Centered Text",
style: TextStyle(color: Colors.blue, fontSize: 24),
),
),
);
}
}
Padding উইজেট
একটা উইজেটে একাধিক ভাবে প্যাডিং যোগ করা যায়। যেমন প্যাডিং মডিফায়ার ব্যবহার করে। আবার আমরা চাইলে প্যাডিং উইজেটও ব্যবহার করতে পারি। যেমনঃ
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(home: PaddingExample()));
}
class PaddingExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Padding Widget Example")),
body: Padding(
padding: EdgeInsets.all(50), // Adds 50 pixels of padding on all sides
child: Text(
"Padded Text",
style: TextStyle(color: Colors.blue, fontSize: 18),
),
),
);
}
}
FractionallySizedBox উইজেট
প্যারেন্টের সাইজের অনুপাতে চাইল্ডের সাইজ সেট করার জন্য ব্যবহৃত হয়। যেমন প্যারেন্টের 90% ইত্যাদি। উদাহরণঃ
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(home: FractionallySizedBoxExample()));
}
class FractionallySizedBoxExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("FractionallySizedBox Example")),
body: Center(
child: FractionallySizedBox(
widthFactor: 0.5, // 50% of parent width
heightFactor: 0.5, // 50% of parent height
alignment: Alignment.center, // Centered within parent
child: Container(
color: Colors.blue,
child: Center(
child: Text(
"50% Width, 30% Height",
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white),
),
),
),
),
),
);
}
}
আউটপুটঃ
রেসপন্সিভ ডিজাইনের ক্ষেত্রে FractionallySizedBox কাজে দেয়।