如何更改标签栏突出显示的颜色

问题描述 投票:0回答:3

Flutter Tab Color :来自stackoverflow的关于此问题的解决方案未解决我的问题我的完整代码

import 'package:flutter/material.dart';

class BookingHistory extends StatefulWidget {
  BookingHistory({Key key}) : super(key: key);

  @override
  _BookingHistoryState createState() => _BookingHistoryState();
}

class _BookingHistoryState extends State<BookingHistory> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DefaultTabController(
          length: 3,
          child: Scaffold(
            // backgroundColor: Colors.white,
            appBar: AppBar(

              flexibleSpace: Container(
                color: Colors.green,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: <Widget>[
                    TabBar(indicatorColor: Colors.blue,
                      labelColor: Colors.red,
                      unselectedLabelColor: Colors.green,

                        tabs: [
                      Tab(
                        child: Text(
                          "Completed",
                          style: TextStyle(color: Colors.white),
                        ),
                      ),
                      Tab(
                        child: Text(
                          "Requested",
                          style: TextStyle(color: Colors.white),
                        ),
                      ),
                      Tab(
                        child: Text(
                          "Accepted",
                          style: TextStyle(color: Colors.white),
                        ),
                      )
                    ])
                  ],
                ),
              ),
            ),

            body: TabBarView(children: [
              Container(
                child: Center(
                  child: Text("i am tab 1"),
                ),
              ),
              Container(
                child: Center(
                  child: Text("i am tab 2"),
                ),
              ),
              Container(
                child: Center(
                  child: Text("i am tab 3"),
                ),
              )
            ]),
          )),
    );
  }
}

答案为了使indicatorColor,labelColor或unselectedLabelColor生效,我们需要使用Material Widget(<<< [Ravinder Kumar建议的解决方案包装Widget),或者如果我们只需要更改indicatorColor颜色,则使用名称结尾为(Aiding Aidan建议的解决方案)

flutter flutter-layout
3个回答
1
投票
有效,但不起作用Colors.blue

1
投票
输出:

Material( color: Colors.green, child: TabBar( indicatorColor: Colors.red, labelColor: Colors.red, unselectedLabelColor: Colors.green, .....

完整代码

enter image description here

UPDATE:奇怪的是,除了看起来还可以使用以外的任何颜色,例如使用class BookingHistory extends StatefulWidget { BookingHistory({Key key}) : super(key: key); @override _BookingHistoryState createState() => _BookingHistoryState(); } class _BookingHistoryState extends State<BookingHistory> { @override Widget build(BuildContext context) { return MaterialApp( home: DefaultTabController( length: 3, child: Scaffold( // backgroundColor: Colors.white, appBar: AppBar( flexibleSpace: Container( color: Colors.green, child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.end, children: <Widget>[ Material( color: Colors.green, child: TabBar( indicatorColor: Colors.red, labelColor: Colors.red, unselectedLabelColor: Colors.green, tabs: [ Tab( child: Text( "Completed", style: TextStyle(color: Colors.white), ), ), Tab( child: Text( "Requested", style: TextStyle(color: Colors.white), ), ), Tab( child: Text( "Accepted", style: TextStyle(color: Colors.white), ), ) ])) ], ), ), ), body: TabBarView(children: [ Container( child: Center( child: Text("i am tab 1"), ), ), Container( child: Center( child: Text("i am tab 2"), ), ), Container( child: Center( child: Text("i am tab 3"), ), ) ]), )), ); } }


0
投票
在每个unselectedLabelColor中,您无需指定Tab。它将覆盖TextStyle中的labelColorunselectedLabelColor

TabBar

© www.soinside.com 2019 - 2024. All rights reserved.