ফ্লাটারে রো, কলাম, ফ্লেক্স ইত্যাদি

একাধিক চাইল্ড সাজানোর জন্য বেশ কিছু লেয়াউট রয়েছে। এগুলোর মধ্যে রয়েছে ডিরেকশনাল, ওভারল্যাপিং ও টেবিল স্টাইল লেআউট। এই লেখায় আমরা ফ্লাটারের ডিরেকশনাল লেআউট সম্পর্কে জানব।

Row (Horizontal Layout)

Row উইজেট ব্যবহার করে এর চাইল্ড গুলোকে হরিজন্টালি (লেফট টু রাইট) সাজানো যায়। যেমনঃ

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(home: RowExample()));
}

class RowExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Row Example")),
      body: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly, // Space between items
        crossAxisAlignment: CrossAxisAlignment.center, // Align items at center
        children: [
          Container(width: 50, height: 50, color: Colors.red),
          Container(width: 50, height: 50, color: Colors.green),
          Container(width: 50, height: 50, color: Colors.blue),
          
        ],
      ),
    );
  }
}

যার আউটপুট পাবো এমনঃ

এখানে MainAxisAlignment.spaceEvenly মডিফায়ার দিয়ে চাইল্ড গুলোতে সমাজ স্পেস যোগ করেছি। এবং CrossAxisAlignment.center দিয়ে চাইল্ড গুলোকে নির্দিষ্ট স্পেসের মাঝখানে রেখেছি। এগুলো অপশনাল।

Row উইজেটে স্ক্রল যোগ করা থাকে না। আমরা যদি স্ক্রিনে যতটুকু স্পেস রয়েছে, তার তুলনায় বেশি চাইল্ড যোগ করি, তাহলে ওভারল্যাপিং এরর দেখাবে। এই জন্য যদি একটা Row তে অনেক গুলো চাইল্ড রাখতে চাই, তাহলে SingleChildScrollView উইজেটে র‍্যাপ করে নিতে হবে। যেমনঃ

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(home: RowExample()));
}

class RowExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Row Example")),
      body: SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: ConstrainedBox(
          constraints: BoxConstraints(
            minWidth: MediaQuery.of(context).size.width + 100,
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              Container(width: 50, height: 50, color: Colors.red),
              Container(width: 50, height: 50, color: Colors.green),
              Container(width: 50, height: 50, color: Colors.blue),
              Container(width: 50, height: 50, color: Colors.orange),
              Container(width: 50, height: 50, color: Colors.purple),
              Container(width: 50, height: 50, color: Colors.yellow),
            ],
          ),
        ),
      ),
    );
  }
}

আউটপুট পাবো এমনঃ

আমরা যখন SingleChildScrollView ব্যবহার করব, তখন আর MainAxisAlignment.spaceEvenly কাজ করবে না। সেক্ষেত্রে রো এর আইটেম গুলোর মধ্যে স্পেস দেওয়ার জন্য ConstrainedBox এর সাহায্য নিতে পারি। যা রো এর প্রতিটা আইটেমের মধ্যে একটা নির্দিষ্ট স্পেস যোগ করে দিবে।

Column (Vertical Layout)

কলাম উইজেট ব্যবহার করে এর চাইল্ড গুলোকে ভার্টিকালি (টপ টু বটম) সাজানো যায়। যেমনঃ

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(home: ColumnExample()));
}

class ColumnExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Column Example")),
      body: Column(
        children: [
          Container(width: 50, height: 50, color: Colors.red),
          Container(width: 50, height: 50, color: Colors.green),
          Container(width: 50, height: 50, color: Colors.blue),
        ],
      ),
    );
  }
}

রো এর মত কলামেও স্ক্রোল যোগ করতে চাইলে SingleChildScrollView এর মধ্যে র‍্যাপ করে নিতে হবে। যেমনঃ

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(home: ColumnExample()));
}

class ColumnExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Column Example")),
      body: SingleChildScrollView(
        child: Container(
          height: MediaQuery.of(context).size.height, // Takes full screen height
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceAround, // Spaces items
            children: [
              Container(width: 50, height: 50, color: Colors.red),
              Container(width: 50, height: 50, color: Colors.green),
              Container(width: 50, height: 50, color: Colors.blue),
              Container(width: 50, height: 50, color: Colors.orange),
              Container(width: 50, height: 50, color: Colors.purple),
              Container(width: 50, height: 50, color: Colors.yellow),
              Container(width: 50, height: 50, color: Colors.cyan),
              Container(width: 50, height: 50, color: Colors.black),
            ],
          ),
        ),
      ),
    );
  }
}

এখানে আলাদা একটা Container ভিউ ব্যবহার করেছি চাইল্ড গুলোর মধ্যে স্পেস দেওয়ার জন্য।

Flex উইজেট

Row বা Column এর মতো এর চাইল্ড গুলোকে ভার্টিকালি বা হরিজন্টালি সাজানো যায়, তবে আরও বেশি কাস্টমাইজ করা যায়। স্ক্রিনের সাইজ অনুযায়ী চাইল্ড গুলোকে সাজানোর জন্য ফ্লেক্স উইজেট ব্যবহার করা হয়। ডিরেকশন মডিফায়ার ব্যবহার করে হরিজন্টালি অথবা ভার্টিকালি চাইল্ড গুলোকে সাজানো হয়। যেমনঃ

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(home: FlexExample()));
}

class FlexExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Flex Example")),
      body: Flex(
        direction: Axis.vertical, // Sets the direction to vertical
        children: [
          Expanded(
            flex: 1, // Takes 1 part of the available space
            child: Container(color: Colors.red),
          ),
          Expanded(
            flex: 2, // Takes 2 parts of the available space
            child: Container(color: Colors.green),
          ),
          Expanded(
            flex: 3, // Takes 3 parts of the available space
            child: Container(color: Colors.blue),
          ),
        ],
      ),
    );
  }
}

উপরের কোডের আউটপুটঃ

এখানে ফ্লেক্স উইজেটের ভেতর তিনটা চাইল্ড রয়েছে। একেকটা চাইল্ডের উচ্চতা স্ক্রিনের সাইজ অনুপাতে সেট করেছি আমরা।

Wrap উইজেট

রো অথবা কলাম যখন ব্যবহার করি, তখন কন্টেন্ট ওভারফ্লো হয়। চাইল্ড বেশি হলে আমরা Wrap উইজেট ব্যবহার করতে পারি। যখন জায়গা কম পড়ে তখন র‍্যাপ উইজেট চাইল্ডগুলোকে নতুন রো বা কলামে সরিয়ে নেয়। যেমনঃ

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(home: WrapExample()));
}

class WrapExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Wrap Example")),
      body: Padding(
        padding: EdgeInsets.all(10),
        child: Wrap(
          spacing: 10, // Horizontal space between items
          runSpacing: 10, // Vertical space between lines
          children: List.generate(10, (index) {
            return Container(
              width: 80,
              height: 80,
              color: Colors.primaries[index % Colors.primaries.length],
            );
          }),
        ),
      ),
    );
  }
}

যার আউটপুট পাবো এমনঃ

র‍্যাপ উইজেট ডিফল্ট ভাবে চাইল্ড গুলোকে হরিজন্টালি সাজিয়ে রাখে। ভার্টিকালি সাজানোর জন্য direction: Axis.vertical, মডিফায়ার ব্যবহার করতে পারি।

র‍্যাপ উইজেটেও স্ক্রল যোগ করা থাকে না। আইটেম বেশি হলে আমাদের নিজেদের স্ক্রল ভিউ যোগ করে নিতে হবে। তার জন্য আমরা SingleChildScrollView উইজেট ব্যবহার করতে পারি।

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(home: WrapScrollableExample()));
}

class WrapScrollableExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Scrollable Wrap Example")),
      body: SingleChildScrollView(
        child: Padding(
          padding: EdgeInsets.all(10),
          child: Wrap(
            spacing: 10, // Horizontal space between items
            runSpacing: 10, // Vertical space between rows
            children: List.generate(30, (index) { 
              return Container(
                width: 80,
                height: 80,
                color: Colors.primaries[index % Colors.primaries.length],
              );
            }),
          ),
        ),
      ),
    );
  }
}

Leave a Comment