games_services 5.0.0 copy "games_services: ^5.0.0" to clipboard
games_services: ^5.0.0 copied to clipboard

A new Flutter plugin to support game center and google play games services.

example/lib/main.dart

import 'dart:convert';

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

void main() => runApp(const App());

class App extends StatefulWidget {
  const App({Key? key}) : super(key: key);

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

class AppState extends State<App> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        appBarTheme: const AppBarTheme(
          backgroundColor: Colors.black,
          foregroundColor: Colors.white,
        ),
        elevatedButtonTheme: ElevatedButtonThemeData(
            style: ElevatedButton.styleFrom(
          backgroundColor: Colors.black,
        )),
      ),
      home: Scaffold(
          appBar: AppBar(
            title: const Text('Games Services plugin example'),
          ),
          body: StreamBuilder<PlayerData?>(
              stream: GameAuth.player,
              builder: (context, snapshot) {
                final player = snapshot.data;
                return SingleChildScrollView(
                  child: Center(
                    child: Column(
                      children: [
                        Padding(
                          padding: const EdgeInsets.all(20),
                          child: Image.asset(
                            "assets/logo.png",
                            width: 200,
                          ),
                        ),
                        Padding(
                          padding: const EdgeInsets.all(20),
                          child: player == null
                              ? ElevatedButton(
                                  onPressed: _signIn,
                                  style: ElevatedButton.styleFrom(
                                    minimumSize: const Size(150, 40),
                                    backgroundColor: Colors.black,
                                    foregroundColor: Colors.white,
                                  ),
                                  child: const Text('Sign In'),
                                )
                              : Column(
                                  mainAxisSize: MainAxisSize.min,
                                  children: [
                                    Row(
                                      children: [
                                        if (player.iconImage != null)
                                          CircleAvatar(
                                            foregroundImage: MemoryImage(
                                              base64Decode(player.iconImage!),
                                            ),
                                          )
                                        else
                                          CircleAvatar(
                                            child: Text(player.displayName[0]),
                                          ),
                                        const SizedBox(width: 16.0),
                                        Expanded(
                                          child: Text(
                                              'playerID: ${player.playerID}\n'
                                              'displayName: ${player.displayName}'),
                                        ),
                                      ],
                                    ),
                                    const SizedBox(height: 16.0),
                                    Theme(
                                      data: ThemeData(
                                        elevatedButtonTheme:
                                            ElevatedButtonThemeData(
                                          style: ElevatedButton.styleFrom(
                                            minimumSize: const Size(150, 40),
                                            backgroundColor: Colors.black,
                                            foregroundColor: Colors.white,
                                          ),
                                        ),
                                      ),
                                      child: SingleChildScrollView(
                                        child: Wrap(
                                          spacing: 20,
                                          runSpacing: 10,
                                          children: <Widget>[
                                            ElevatedButton(
                                              onPressed: _showAchievements,
                                              child: const Text(
                                                  'Show Achievements'),
                                            ),
                                            ElevatedButton(
                                              onPressed: _showLeaderboards,
                                              child: const Text(
                                                  'Show Leaderboards'),
                                            ),
                                            ElevatedButton(
                                              onPressed: _submitScore,
                                              child: const Text('Submit Score'),
                                            ),
                                            ElevatedButton(
                                              onPressed: _unlockAchievement,
                                              child: const Text(
                                                  'Unlock Achievement'),
                                            ),
                                            ElevatedButton(
                                              onPressed: _loadAchievement,
                                              child: const Text(
                                                  'Load Achievement'),
                                            ),
                                            ElevatedButton(
                                              onPressed: _resetAchievement,
                                              child: const Text(
                                                  'Reset Achievement'),
                                            ),
                                            ElevatedButton(
                                              onPressed: _loadLeaderboardScores,
                                              child: const Text(
                                                  'Load Leaderboard Scores'),
                                            ),
                                            ElevatedButton(
                                              onPressed: _incrementAchievement,
                                              child: const Text(
                                                  'Increment Achievement (Android only)'),
                                            ),
                                            ElevatedButton(
                                              onPressed: _showAccessPoint,
                                              child: const Text(
                                                  'Show AccessPoint (iOS only)'),
                                            ),
                                            ElevatedButton(
                                              onPressed: _hideAccessPoint,
                                              child: const Text(
                                                  'Hide AccessPoint (iOS only)'),
                                            ),
                                            ElevatedButton(
                                              onPressed: _getPlayerScore,
                                              child: const Text(
                                                  'Get player score'),
                                            ),
                                            ElevatedButton(
                                              onPressed: _getPlayerScoreObject,
                                              child: const Text(
                                                  'Load Player Centered Scores'),
                                            ),
                                            ElevatedButton(
                                              onPressed:
                                                  _loadPreviousOccurrence,
                                              child: const Text(
                                                  'Load Previous Occurrence (iOS only)'),
                                            ),
                                            ElevatedButton(
                                              onPressed: _getSavedGames,
                                              child:
                                                  const Text('Get saved games'),
                                            ),
                                            ElevatedButton(
                                              onPressed: _saveGame,
                                              child: const Text('Save game'),
                                            ),
                                            ElevatedButton(
                                              onPressed: _loadGame,
                                              child: const Text('Load game'),
                                            ),
                                            ElevatedButton(
                                              onPressed: _deleteGame,
                                              child: const Text(
                                                  'Delete saved game'),
                                            ),
                                            ElevatedButton(
                                              onPressed:
                                                  _fetchIdentityVerificationSignature,
                                              child: const Text(
                                                  'Fetch Identity Verification (iOS and MacOS)'),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                        ),
                      ],
                    ),
                  ),
                );
              })),
    );
  }

  void _signIn() async {
    final result = await GameAuth.signIn();
    print(result);
  }

  void _getPlayerScore() async {
    final result = await Player.getPlayerScore();
    print(result);
  }

  void _getPlayerScoreObject() async {
    final result = await Leaderboards.getPlayerScoreObject(
        iOSLeaderboardID: "ios_leaderboard_id",
        androidLeaderboardID: "android_leaderboard_id",
        scope: PlayerScope.global,
        timeScope: TimeScope.allTime);
    print(result);
  }

  void _loadPreviousOccurrence() async {
    final result = await Leaderboards.loadPreviousOccurrence(
        iOSLeaderboardID: "ios_leaderboard_id",
        androidLeaderboardID: "android_leaderboard_id",
        timeScope: TimeScope.allTime);
    if (result != null) {
      print('Previous score found:');
      print('  Score: ${result.rawScore}');
      print('  Rank: ${result.rank}');
      print('  Display Score: ${result.displayScore}');
      print(
          '  Timestamp: ${DateTime.fromMillisecondsSinceEpoch(result.timestampMillis)}');
      print('  Player: ${result.scoreHolder.displayName}');
    } else {
      print('No previous occurrence found');
    }
  }

  void _showAccessPoint() async {
    final result = await Player.showAccessPoint(AccessPointLocation.topLeading);
    print(result);
  }

  void _hideAccessPoint() async {
    final result = await Player.hideAccessPoint();
    print(result);
  }

  void _resetAchievement() async {
    final result = await Achievements.resetAchievements();
    print(result);
  }

  void _incrementAchievement() async {
    final result = await Achievements.increment(
        achievement: Achievement(androidID: 'android_id', steps: 50));
    print(result);
  }

  void _unlockAchievement() async {
    final result = await Achievements.unlock(
        achievement: Achievement(
            androidID: 'android_id', iOSID: 'ios_id', percentComplete: 100));
    print(result);
  }

  void _loadAchievement() async {
    final result = await Achievements.loadAchievements();
    print(result);
  }

  void _loadLeaderboardScores() async {
    final result = await Leaderboards.loadLeaderboardScores(
        iOSLeaderboardID: "ios_leaderboard_id",
        androidLeaderboardID: "android_leaderboard_id",
        scope: PlayerScope.global,
        timeScope: TimeScope.allTime,
        maxResults: 10);
    print(result);
  }

  void _submitScore() async {
    final result = await Leaderboards.submitScore(
        score: Score(
            androidLeaderboardID: 'android_leaderboard_id',
            iOSLeaderboardID: 'ios_leaderboard_id',
            value: 5));
    print(result);
  }

  void _showLeaderboards() async {
    final result = await Leaderboards.showLeaderboards(
        androidLeaderboardID: 'android_leaderboard_id',
        iOSLeaderboardID: 'ios_leaderboard_id');
    print(result);
  }

  void _showAchievements() async {
    final result = await Achievements.showAchievements();
    print(result);
  }

  void _getSavedGames() async {
    final result = await SaveGame.getSavedGames();
    print(result);
  }

  void _saveGame() async {
    final data = jsonEncode(GameData(96, "sword").toJson());
    final result = await SaveGame.saveGame(data: data, name: "slot1");
    print(result);
  }

  void _loadGame() async {
    final result = await SaveGame.loadGame(name: "slot1");
    if (result != null) {
      final Map json = jsonDecode(result);
      final gameData = GameData.fromJson(json);
      print("Player progress ${gameData.progress}");
      print("Player weapon ${gameData.weapon}");
    }
  }

  void _deleteGame() async {
    final result = await SaveGame.deleteGame(name: "slot1");
    print(result);
  }

  void _fetchIdentityVerificationSignature() async {
    final result = await GameAuth.fetchIdentityVerificationSignature();
    if (result != null) {
      print('Identity Verification Signature:');
      print('  Public Key URL: ${result.publicKeyURL}');
      print('  Signature: ${result.signature}');
      print('  Salt: ${result.salt}');
      print('  Timestamp: ${result.timestamp}');
    } else {
      print('Identity verification not available (iOS and MacOS)');
    }
  }
}

class GameData {
  int progress;
  String weapon;
  GameData(this.progress, this.weapon);

  factory GameData.fromJson(Map json) {
    return GameData(json["progress"], json["weapon"]);
  }

  Map toJson() => {'progress': progress, 'weapon': weapon};
}
169
likes
150
points
4.67k
downloads

Publisher

verified publisherakdebuging.com

Weekly Downloads

A new Flutter plugin to support game center and google play games services.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, games_services_platform_interface

More

Packages that depend on games_services

Packages that implement games_services