Flutter列表视图插件flop_list_view的使用

Flutter列表视图插件flop_list_view的使用

功能

  • 滚动锚定

待办事项

  • ❌ …

致谢

完整示例

示例代码

import 'dart:math';
import 'dart:ui';

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

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

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'FlopListView',
      home: const MyHomePage(title: "Flop's ListView Example"),
      scrollBehavior: const MaterialScrollBehavior().copyWith(
        dragDevices: PointerDeviceKind.values.toSet(),
      ),
    );
  }
}

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> {
  final _itemCount = 100;
  late final List<int> _itemHeights;
  final _flopListController = FlopListController();

  [@override](/user/override)
  void initState() {
    super.initState();
    final heightGenerator = Random(2147483647);
    _itemHeights = List.generate(_itemCount, (index) {
      return 100 + heightGenerator.nextInt(200);
    });
  }

  Widget _buildItem(String name, int index) {
    return FutureBuilder(
      future: Future.delayed(
        Duration(milliseconds: 1000 + _itemHeights[index] * 5),
        () => _itemHeights[index].toDouble(),
      ),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return Card(
            child: SizedBox(
              height: snapshot.data!,
              child: Center(child: Text('$name $index')),
            ),
          );
        }
        return Card(
          child: SizedBox(
            height: 100,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const Flexible(
                  child: SizedBox(
                    width: 10,
                    height: 10,
                    child: CircularProgressIndicator(strokeWidth: 2),
                  ),
                ),
                const Flexible(
                  child: SizedBox(width: 10),
                ),
                Flexible(
                  flex: 10,
                  child: Text('$name $index'),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title)),
      body: FlopListView.builder(
        anchor: 0.0,
        trailing: true,
        anchorMask: true,
        trailingMask: true,
        itemCount: _itemCount,
        controller: _flopListController,
        initialScrollIndex: _itemCount - 1,
        physics: const BouncingScrollPhysics(),
        itemBuilder: (context, index) => _buildItem('FlopListView', index),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
      floatingActionButton: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: <Widget>[
            Flexible(
              child: FloatingActionButton(
                onPressed: () => _flopListController.jumpTo(0),
                child: const Icon(Icons.keyboard_double_arrow_up),
              ),
            ),
            const Flexible(child: Padding(padding: EdgeInsets.all(8.0))),
            Flexible(
              child: FloatingActionButton(
                onPressed: () => _flopListController.jumpTo(_itemCount - 1),
                child: const Icon(Icons.keyboard_double_arrow_down),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

更多关于Flutter列表视图插件flop_list_view的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter列表视图插件flop_list_view的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


flop_list_view 是一个 Flutter 插件,用于创建具有翻转效果的列表视图。它允许你在列表中为每个项目添加翻转动画,提供更加动态和交互式的用户体验。以下是如何使用 flop_list_view 的基本步骤:

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 flop_list_view 的依赖:

dependencies:
  flutter:
    sdk: flutter
  flop_list_view: ^0.1.0  # 请检查最新版本

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

2. 导入包

在你的 Dart 文件中导入 flop_list_view

import 'package:flop_list_view/flop_list_view.dart';

3. 使用 FlopListView

FlopListView 的使用方式与普通的 ListView 类似,但它为每个项目添加了翻转效果。你可以通过 itemBuilder 来构建每个列表项。

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flop List View Example'),
      ),
      body: FlopListView.builder(
        itemCount: 10, // 列表项的数量
        itemBuilder: (context, index) {
          return FlopCard(
            front: Container(
              color: Colors.blue,
              child: Center(
                child: Text(
                  'Front $index',
                  style: TextStyle(color: Colors.white, fontSize: 24),
                ),
              ),
            ),
            back: Container(
              color: Colors.red,
              child: Center(
                child: Text(
                  'Back $index',
                  style: TextStyle(color: Colors.white, fontSize: 24),
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}

4. 自定义翻转效果

FlopCardFlopListView 中的一个组件,用于定义每个列表项的翻转效果。你可以通过 frontback 属性来定义卡片的前面和背面内容。

  • front: 卡片正面的内容。
  • back: 卡片背面的内容。

5. 运行应用

保存代码并运行你的应用。你应该会看到一个带有翻转效果的列表视图,点击每个项目时,它会翻转显示背面内容。

6. 额外配置

FlopListView 还支持一些额外的配置选项,例如动画持续时间、翻转方向等。你可以根据需要进一步自定义。

FlopListView.builder(
  itemCount: 10,
  itemBuilder: (context, index) {
    return FlopCard(
      duration: Duration(milliseconds: 500), // 动画持续时间
      direction: Axis.horizontal, // 翻转方向
      front: Container(
        color: Colors.blue,
        child: Center(
          child: Text(
            'Front $index',
            style: TextStyle(color: Colors.white, fontSize: 24),
          ),
        ),
      ),
      back: Container(
        color: Colors.red,
        child: Center(
          child: Text(
            'Back $index',
            style: TextStyle(color: Colors.white, fontSize: 24),
          ),
        ),
      ),
    );
  },
);
回到顶部