Flutter带标签的固定扩展列表插件sliver_fixed_extend_list_with_tabs的使用

发布于 1周前 作者 ionicwang 来自 Flutter

Flutter带标签的固定扩展列表插件sliver_fixed_extend_list_with_tabs的使用

概述

sliver_fixed_extend_list_with_tabs 是一个 Flutter 包,它提供了一个带有可自定义标签栏的 sliver 小部件,并且包含一个固定的扩展列表项(包括父项和子项)。如果最后一个部分(父项及其子项)太短而无法到达屏幕顶部,则该包提供了可自定义的页脚来填充缺失的空间。

安装

pubspec.yaml 文件中添加以下依赖:

dependencies:
  sliver_fixed_extend_list_with_tabs: ^0.0.1

使用

使用 SliverFixedExtendListWithTabs 小部件来创建带有标签栏的固定扩展列表。根据需要自定义标签栏、列表项和页脚。

import 'package:flutter/material.dart';
import 'package:sliver_fixed_extend_list_with_tabs/sliver_fixed_extend_list_with_tabs.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'SliverFixedExtendListWithTabs Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'SliverFixedExtendListWithTabs Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<Section> sections = [];
  double listItemHeight = 80;
  
  [@override](/user/override)
  void initState() {
    super.initState();
    double offsetStart = 0;
    List<ChildItem> children = List.generate(
      10,
      (index) => const Child(),
    );
    for (int i = 0; i < 11; i++) {
      Header header = Header(
        key: ValueKey(i),
        name: 'Header item $i',
        offsetStart: offsetStart,
        childrenCount: children.length,
        childrenHeight: listItemHeight,
      );
      sections.add(Section(
        header: header,
        children: children,
      ));
      offsetStart = header.offsetEnd;
    }
  }

  Widget buildHeader(BuildContext context, HeaderItem item) {
    Header header = item as Header;
    return Container(
      color: Colors.orange,
      child: Center(
        child: Text(
          header.name,
          style: const TextStyle(fontSize: 30),
        ),
      ),
    );
  }

  Widget buildChild(BuildContext context, ChildItem item) {
    return Container(
      color: Colors.blue.shade400,
      child: const Center(
        child: Text(
          'Child item',
          style: TextStyle(fontSize: 20),
        ),
      ),
    );
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: CustomScrollView(
          slivers: [
            SliverAppBar(
              pinned: true,
              title: Text(widget.title),
            ),
            SliverFixedExtendListWithTabs(
              controller: PrimaryScrollController.of(context),
              indicatorPadding: const EdgeInsets.symmetric(
                horizontal: 8.0,
                vertical: 4.0,
              ),
              tabBarIndicator: BoxDecoration(
                borderRadius: BorderRadius.circular(
                  25.0,
                ),
                color: Colors.green,
              ),
              listItemHeight: listItemHeight,
              sections: sections,
              headerBuilder: buildHeader,
              childBuilder: buildChild,
            )
          ],
        ),
      ),
    );
  }
}

class Header extends HeaderItem {
  Header({
    required super.key,
    required this.name,
    required super.offsetStart,
    required super.childrenCount,
    required super.childrenHeight,
  });
  final String name;
}

class Child extends ChildItem {
  const Child();
}

更多关于Flutter带标签的固定扩展列表插件sliver_fixed_extend_list_with_tabs的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter带标签的固定扩展列表插件sliver_fixed_extend_list_with_tabs的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何在Flutter中使用sliver_fixed_extend_list_with_tabs插件来实现带标签的固定扩展列表的示例代码。请注意,由于sliver_fixed_extend_list_with_tabs并非Flutter官方插件,而是一个假设的第三方插件,实际使用时你需要确保该插件已在pubspec.yaml文件中添加依赖并正确导入。

首先,确保你的pubspec.yaml文件中包含该插件的依赖项(假设插件名为sliver_fixed_extend_list_with_tabs):

dependencies:
  flutter:
    sdk: flutter
  sliver_fixed_extend_list_with_tabs: ^x.y.z  # 替换为实际版本号

然后,运行flutter pub get来获取依赖。

接下来是示例代码,展示如何使用该插件:

import 'package:flutter/material.dart';
import 'package:sliver_fixed_extend_list_with_tabs/sliver_fixed_extend_list_with_tabs.dart'; // 假设的包导入路径

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Sliver Fixed Extend List with Tabs Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  late TabController _tabController;

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

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Sliver Fixed Extend List with Tabs'),
        bottom: TabBar(
          controller: _tabController,
          tabs: [
            Tab(icon: Icon(Icons.directions_car), text: 'Tab 1'),
            Tab(icon: Icon(Icons.directions_transit), text: 'Tab 2'),
          ],
        ),
      ),
      body: SliverFixedExtendListWithTabs(
        tabController: _tabController,
        delegate: SliverChildBuilderDelegate(
          (context, index) {
            // 根据选中的Tab返回不同的列表项
            if (_tabController.index == 0) {
              return ListTile(
                title: Text('Item $index in Tab 1'),
              );
            } else {
              return ListTile(
                title: Text('Item $index in Tab 2'),
              );
            }
          },
          childCount: 20, // 假设每个Tab下有20个列表项
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个带有标签栏的页面,每个标签对应一个列表。SliverFixedExtendListWithTabs是一个假设的自定义Widget,用于结合TabBarSliverList实现带标签的固定扩展列表。实际使用时,你需要根据sliver_fixed_extend_list_with_tabs插件的具体API进行调整。

注意:由于sliver_fixed_extend_list_with_tabs并非一个真实存在的Flutter官方或广泛认可的第三方插件,上述代码是一个基于假设的示例。如果你确实需要这样的功能,但找不到现成的插件,你可能需要自己实现一个类似的Widget,结合TabBarTabBarViewSliverList等Flutter内置组件。

回到顶部