dart 创建自定义标签栏在Flutter

disbfnqx  于 2023-09-28  发布在  Flutter
关注(0)|答案(1)|浏览(119)

如何创建如下图所示的标签栏?

目前我只能做到这样

class RecurringItemsAndInvoiceTabScreen extends HookWidget {
  final String id;

  final List<Widget> list = [
    Padding(
        padding: const EdgeInsets.all(10), child: Text("depositItems".tr())),
    Padding(
        padding: const EdgeInsets.all(10), child: Text("recurringItems".tr())),
    Padding(padding: const EdgeInsets.all(10), child: Text("invoice".tr())),
  ];

  RecurringItemsAndInvoiceTabScreen(this.id, {super.key});

  @override
  Widget build(BuildContext context) {
    final controller = useTabController(initialLength: list.length);
    final index = useState(0);

    controller.addListener(() {
      index.value = controller.index;
    });

    return DefaultTabController(
        length: list.length,
        child: Scaffold(
          appBar: AppBar(
            backgroundColor: AppTheme.light.colorScheme.primary,
            iconTheme: const IconThemeData(color: Colors.white),
            title: Text('viewTenancy'.tr(),
                style: const TextStyle(
                  color: Colors.white,
                )),
            bottom: TabBar(
              indicator: BoxDecoration(
                  borderRadius: BorderRadius.circular(50),
                  color: Colors.green.shade200),
              indicatorColor: AppTheme.light.colorScheme.primary,
              unselectedLabelColor: Colors.white,
              labelColor: Colors.green,
              isScrollable: false,
              onTap: (index) {},
              controller: controller,
              tabs: list,
            ),
            bottomOpacity: 1,
          ),
          body: TabBarView(
            controller: controller,
            children: [
             const Text("first page")
             const Text("second page")
             const Text("third page")
            ],
          ),
        ));
  }
}
xjreopfe

xjreopfe1#

参考此answer
只需添加不要忘记将此参数添加到TabBar()小部件:

indicatorSize: TabBarIndicatorSize.tab

否则它不会填充宽度。

相关问题