mirror of
https://github.com/flutter/samples.git
synced 2026-03-31 16:55:34 +00:00
Add game_template (#1180)
Adds a template / sample for games built in Flutter, with all the bells and whistles, like ads, in-app purchases, audio, main menu, settings, and so on. Co-authored-by: Parker Lougheed Co-authored-by: Shams Zakhour
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
// Copyright 2022, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'settings_persistence.dart';
|
||||
|
||||
/// An implementation of [SettingsPersistence] that uses
|
||||
/// `package:shared_preferences`.
|
||||
class LocalStorageSettingsPersistence extends SettingsPersistence {
|
||||
final Future<SharedPreferences> instanceFuture =
|
||||
SharedPreferences.getInstance();
|
||||
|
||||
@override
|
||||
Future<bool> getMusicOn() async {
|
||||
final prefs = await instanceFuture;
|
||||
return prefs.getBool('musicOn') ?? true;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> getMuted({required bool defaultValue}) async {
|
||||
final prefs = await instanceFuture;
|
||||
return prefs.getBool('mute') ?? defaultValue;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> getPlayerName() async {
|
||||
final prefs = await instanceFuture;
|
||||
return prefs.getString('playerName') ?? 'Player';
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> getSoundsOn() async {
|
||||
final prefs = await instanceFuture;
|
||||
return prefs.getBool('soundsOn') ?? true;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> saveMusicOn(bool value) async {
|
||||
final prefs = await instanceFuture;
|
||||
await prefs.setBool('musicOn', value);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> saveMuted(bool value) async {
|
||||
final prefs = await instanceFuture;
|
||||
await prefs.setBool('mute', value);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> savePlayerName(String value) async {
|
||||
final prefs = await instanceFuture;
|
||||
await prefs.setString('playerName', value);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> saveSoundsOn(bool value) async {
|
||||
final prefs = await instanceFuture;
|
||||
await prefs.setBool('soundsOn', value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// Copyright 2022, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'package:game_template/src/settings/persistence/settings_persistence.dart';
|
||||
|
||||
/// An in-memory implementation of [SettingsPersistence].
|
||||
/// Useful for testing.
|
||||
class MemoryOnlySettingsPersistence implements SettingsPersistence {
|
||||
bool musicOn = true;
|
||||
|
||||
bool soundsOn = true;
|
||||
|
||||
bool muted = false;
|
||||
|
||||
String playerName = 'Player';
|
||||
|
||||
@override
|
||||
Future<bool> getMusicOn() async => musicOn;
|
||||
|
||||
@override
|
||||
Future<bool> getMuted({required bool defaultValue}) async => muted;
|
||||
|
||||
@override
|
||||
Future<String> getPlayerName() async => playerName;
|
||||
|
||||
@override
|
||||
Future<bool> getSoundsOn() async => soundsOn;
|
||||
|
||||
@override
|
||||
Future<void> saveMusicOn(bool value) async => musicOn = value;
|
||||
|
||||
@override
|
||||
Future<void> saveMuted(bool value) async => muted = value;
|
||||
|
||||
@override
|
||||
Future<void> savePlayerName(String value) async => playerName = value;
|
||||
|
||||
@override
|
||||
Future<void> saveSoundsOn(bool value) async => soundsOn = value;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright 2022, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
/// An interface of persistence stores for settings.
|
||||
///
|
||||
/// Implementations can range from simple in-memory storage through
|
||||
/// local preferences to cloud-based solutions.
|
||||
abstract class SettingsPersistence {
|
||||
Future<bool> getMusicOn();
|
||||
|
||||
Future<bool> getMuted({required bool defaultValue});
|
||||
|
||||
Future<String> getPlayerName();
|
||||
|
||||
Future<bool> getSoundsOn();
|
||||
|
||||
Future<void> saveMusicOn(bool value);
|
||||
|
||||
Future<void> saveMuted(bool value);
|
||||
|
||||
Future<void> savePlayerName(String value);
|
||||
|
||||
Future<void> saveSoundsOn(bool value);
|
||||
}
|
||||
Reference in New Issue
Block a user