Flutter赛事锦标赛对阵图插件flutter_tournament_bracket的使用

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

Flutter赛事锦标赛对阵图插件flutter_tournament_bracket的使用

🎬 Demo

Demo Desktop

🌟 Features

  • 自定义比赛卡片:您可以根据需要定制每场比赛的信息展示。
  • 支持缩放和平移:为了更好地查看详细信息,提供了缩放和平移功能。
  • 自定义颜色和尺寸:调整线条、卡片的颜色及大小以匹配您的应用风格。

🚀 Getting Started

首先,在pubspec.yaml文件中添加依赖:

dependencies:
  flutter_tournament_bracket: ^latest_version # 请替换为最新版本号

然后在Dart代码中导入包:

import 'package:flutter_tournament_bracket/flutter_tournament_bracket.dart';

🛠️ Usage

使用TournamentBracket小部件来显示您的锦标赛:

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: TournamentBracket(
          list: [
            Tournament(matches: [
              TournamentMatch(id: "1", teamA: "Real Madrid", teamB: "Barcelona", scoreTeamA: "3", scoreTeamB: "1"),
              // 更多比赛...
            ]),
            // 更多轮次...
          ],
          card: (item) => customMatchCard(item),
          itemsMarginVertical: 20.0,
          cardWidth: 220.0,
          cardHeight: 100,
          lineWidth: 80,
          lineThickness: 5,
          lineBorderRadius: 12,
          lineColor: Colors.green,
        ),
      ),
    );
  }
}

完整示例

这里是一个完整的例子,展示了如何将所有部分组合在一起:

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

// 定义锦标赛数据
final List<Tournament> _tournaments = [
  Tournament(matches: [
    TournamentMatch(id: "1", teamA: "Real Madrid", teamB: "Barcelona", scoreTeamA: "3", scoreTeamB: "1"),
    // 其他比赛...
  ]),
  // 更多轮次...
];

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

/// 主应用程序组件
class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Tournament Bracket',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: Scaffold(
        appBar: AppBar(title: Text('Tournament Bracket')),
        body: Center(
          child: TournamentBracket(
            list: _tournaments,
            card: (item) => customMatchCard(item),
            itemsMarginVertical: 20.0,
            cardWidth: 220.0,
            cardHeight: 100,
            lineWidth: 80,
            lineThickness: 5,
            lineBorderRadius: 12,
            lineColor: Colors.green,
          ),
        ),
      ),
    );
  }

  /// 自定义比赛详情卡片
  Container customMatchCard(TournamentMatch item) {
    return Container(
      padding: const EdgeInsets.all(12),
      decoration: BoxDecoration(
        color: Colors.blue,
        borderRadius: BorderRadius.circular(12),
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Expanded(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  overflow: TextOverflow.ellipsis,
                  maxLines: 1,
                  item.teamA ?? "No Info",
                  style: const TextStyle(fontSize: 16, color: Colors.white),
                ),
                const SizedBox(height: 8),
                Text(
                  item.teamB ?? "No Info",
                  overflow: TextOverflow.ellipsis,
                  maxLines: 1,
                  style: const TextStyle(fontSize: 16, color: Colors.white),
                )
              ],
            ),
          ),
          const VerticalDivider(color: Colors.white),
          const SizedBox(width: 5),
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                item.scoreTeamA ?? "",
                overflow: TextOverflow.ellipsis,
                maxLines: 1,
                style: const TextStyle(fontSize: 16, color: Colors.white),
              ),
              const SizedBox(height: 8),
              Text(
                item.scoreTeamB ?? "",
                overflow: TextOverflow.ellipsis,
                maxLines: 1,
                style: const TextStyle(fontSize: 16, color: Colors.white, fontWeight: FontWeight.w600),
              )
            ],
          ),
          const SizedBox(width: 5)
        ],
      ),
    );
  }
}

🐞 遇到问题?

如果您遇到任何问题或认为该库缺少某些特性,请在GitHub Issues上提交反馈,作者会尽快处理。

📃 License

本项目采用MIT许可证,详情参见LICENSE


更多关于Flutter赛事锦标赛对阵图插件flutter_tournament_bracket的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter赛事锦标赛对阵图插件flutter_tournament_bracket的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用flutter_tournament_bracket插件来创建一个赛事锦标赛对阵图的示例代码。

首先,确保你的Flutter项目已经配置好,并且你已经在pubspec.yaml文件中添加了flutter_tournament_bracket依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_tournament_bracket: ^最新版本号  # 请替换为最新的版本号

然后运行flutter pub get来安装依赖。

接下来,你可以在你的Dart文件中使用flutter_tournament_bracket插件来创建一个赛事对阵图。以下是一个完整的示例代码:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Tournament Bracket Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: TournamentBracketScreen(),
    );
  }
}

class TournamentBracketScreen extends StatelessWidget {
  // 示例数据:对阵信息
  final List<Match> matches = [
    Match(
      id: '1',
      team1: 'Team A',
      team2: 'Team B',
      score1: 2,
      score2: 1,
      round: 1,
    ),
    Match(
      id: '2',
      team1: 'Team C',
      team2: 'Team D',
      score1: 3,
      score2: 0,
      round: 1,
    ),
    Match(
      id: '3',
      team1: 'Winner 1-A/B',
      team2: 'Winner 1-C/D',
      score1: null, // 未进行比赛时分数可以为null
      score2: null,
      round: 2,
    ),
    // 可以继续添加更多轮次的比赛
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Tournament Bracket'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Bracket(
          matches: matches,
          onMatchTap: (Match match) {
            // 点击对阵时的回调,可以导航到详细信息页面等
            showDialog(
              context: context,
              builder: (BuildContext context) {
                return AlertDialog(
                  title: Text('Match Details'),
                  content: Text('Match between ${match.team1} and ${match.team2}'),
                  actions: <Widget>[
                    TextButton(
                      onPressed: () => Navigator.of(context).pop(),
                      child: Text('OK'),
                    ),
                  ],
                );
              },
            );
          },
        ),
      ),
    );
  }
}

// 定义对阵信息的类
class Match {
  final String id;
  final String team1;
  final String team2;
  final int? score1;
  final int? score2;
  final int round;

  Match({
    required this.id,
    required this.team1,
    required this.team2,
    this.score1,
    this.score2,
    required this.round,
  });
}

在这个示例中,我们定义了一个Match类来表示对阵信息,包括对阵的ID、两个队伍的名称、比分以及轮次。然后,在TournamentBracketScreen中,我们创建了一个包含对阵信息的列表,并将其传递给Bracket小部件。

Bracket小部件负责渲染对阵图,并且我们提供了一个onMatchTap回调来处理用户点击对阵时的操作,比如显示对阵详情。

你可以根据实际需求调整对阵信息的列表和onMatchTap回调中的逻辑。希望这个示例能帮助你快速上手flutter_tournament_bracket插件的使用!

回到顶部