Flutter

[Flutter] Tabbar Widget 탭바 위젯

izongg 2024. 3. 20. 10:58
반응형

 

 

Tabbar 위젯 기본 코드

import 'package:flutter/material.dart';

/// Flutter code sample for [TabBar].

void main() => runApp(const TabBarApp());

class TabBarApp extends StatelessWidget {
  const TabBarApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(useMaterial3: true),
      home: const TabBarExample(),
    );
  }
}

class TabBarExample extends StatelessWidget {
  const TabBarExample({super.key});

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      initialIndex: 1,
      length: 3,
      child: Scaffold(
        appBar: AppBar(
          title: const Text('TabBar Sample'),
          bottom: const TabBar(
            tabs: <Widget>[
              Tab(
                icon: Icon(Icons.cloud_outlined),
              ),
              Tab(
                icon: Icon(Icons.beach_access_sharp),
              ),
              Tab(
                icon: Icon(Icons.brightness_5_sharp),
              ),
            ],
          ),
        ),
        body: const TabBarView(
          children: <Widget>[
            Center(
              child: Text("It's cloudy here"),
            ),
            Center(
              child: Text("It's rainy here"),
            ),
            Center(
              child: Text("It's sunny here"),
            ),
          ],
        ),
      ),
    );
  }
}

 

 

탭 String으로 변경

 

	TabBar(
            tabs: <Widget>[
              Tab(child: Text("바나나",style: TextStyle(fontWeight: FontWeight.w800))),
              Tab(child: Text("사과",style: TextStyle(fontWeight: FontWeight.w800))),
              Tab(child: Text("딸기",style: TextStyle(fontWeight: FontWeight.w800))),
            ],
          ),

 

 

Indicator 탭 꽉 차게 설정

 

	TabBar(
    	    // indicator 색 설정
            indicator: BoxDecoration(color: Colors.green[100]),
            
            // 꽉차게 설정
            indicatorSize: TabBarIndicatorSize.tab,
            
            tabs: <Widget>[
              const Tab(
                child: Text("바나나",style: TextStyle(fontWeight: FontWeight.w800))
              ),
              const Tab(
                child: Text("사과",style: TextStyle(fontWeight: FontWeight.w800))
              ),
              const Tab(
                child: Text("딸기",style: TextStyle(fontWeight: FontWeight.w800))
              ),
            ],
          ),

 

탭 레이블 컬러 설정

 

	TabBar(
            indicator: BoxDecoration(color: Colors.green[100]),
            indicatorSize: TabBarIndicatorSize.tab,

	    // 선택된 탭 label 컬러
            labelColor: Colors.white,
            
            // 선택되지않은 탭 label 컬러
            unselectedLabelColor: Colors.black ,
            
            ... )

 

 

참고자료

https://api.flutter.dev/flutter/material/TabBar-class.html

 

반응형