簡體   English   中英

如何更改 flutter 中選定的選項卡背景顏色

[英]How to change the selected tab background colours in flutter

我想更改所選標簽的背景,我將擁有自定義標簽,因此我無法使用指示器更改所選標簽的背景:BoxDecoration

我想實現這樣的標簽欄

在此處輸入圖像描述

請指導我在設計中實現標簽欄。 我剛開始學習 flutter。

這是你想要的?

class TabBarExample extends StatefulWidget {
  @override
  _TabBarExampleState createState() => _TabBarExampleState();
}

class _TabBarExampleState extends State<TabBarExample>
    with SingleTickerProviderStateMixin {
  // Define a tab controller object
  TabController _tabController;

  @override
  void initState() {
    _tabController = TabController(length: 3, vsync: this);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          color: Colors.grey[300],
          child: TabBar(
            controller: _tabController,
            indicatorColor: Colors.transparent,
            labelColor: Colors.pink,
            unselectedLabelColor: Colors.grey,
            labelStyle: TextStyle(
              fontSize: 16,
            ),
            unselectedLabelStyle: TextStyle(
              fontSize: 16,
            ),
            indicator: BoxDecoration(
              color: Colors.white,
            ),
            tabs: <Widget>[
              Tab(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text('STEP 1'),
                    Text(
                      'Investment',
                      style: TextStyle(
                        fontSize: 12,
                      ),
                    ),
                  ],
                ),
              ),
              Tab(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text('STEP 2'),
                    Text(
                      'Investment',
                      style: TextStyle(fontSize: 12),
                    ),
                  ],
                ),
              ),
              Tab(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text('STEP 3'),
                    Text(
                      'Investment',
                      style: TextStyle(fontSize: 12),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

OUTPUT:

在此處輸入圖像描述

對於未選擇的標簽顏色:用 ColoredBox 包裹並給出您的顏色。

對於選定的選項卡顏色:給一個 BoxDecoration 以您所需的顏色作為指示器。

ColoredBox(
 /// Unselected tab color
 color: Colors.black,
 child: TabBar(
   /// Selected tab color
   indicator: BoxDecoration(color: Colors.white),
   tabs: const [
     Tab(text: 'A'),
     Tab(text: 'B'),
    ],
   ),
 )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM