mirror of
https://github.com/flutter/samples.git
synced 2026-04-26 01:17:18 +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:
75
game_template/lib/src/settings/custom_name_dialog.dart
Normal file
75
game_template/lib/src/settings/custom_name_dialog.dart
Normal file
@@ -0,0 +1,75 @@
|
||||
// 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:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:game_template/src/settings/settings.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
void showCustomNameDialog(BuildContext context) {
|
||||
showGeneralDialog(
|
||||
context: context,
|
||||
pageBuilder: (context, animation, secondaryAnimation) =>
|
||||
CustomNameDialog(animation: animation));
|
||||
}
|
||||
|
||||
class CustomNameDialog extends StatefulWidget {
|
||||
final Animation<double> animation;
|
||||
|
||||
const CustomNameDialog({required this.animation, Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<CustomNameDialog> createState() => _CustomNameDialogState();
|
||||
}
|
||||
|
||||
class _CustomNameDialogState extends State<CustomNameDialog> {
|
||||
final TextEditingController _controller = TextEditingController();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ScaleTransition(
|
||||
scale: CurvedAnimation(
|
||||
parent: widget.animation,
|
||||
curve: Curves.easeOutCubic,
|
||||
),
|
||||
child: SimpleDialog(
|
||||
title: const Text('Change name'),
|
||||
children: [
|
||||
TextField(
|
||||
controller: _controller,
|
||||
autofocus: true,
|
||||
maxLength: 12,
|
||||
maxLengthEnforcement: MaxLengthEnforcement.enforced,
|
||||
textAlign: TextAlign.center,
|
||||
textCapitalization: TextCapitalization.words,
|
||||
textInputAction: TextInputAction.done,
|
||||
onChanged: (value) {
|
||||
context.read<SettingsController>().setPlayerName(value);
|
||||
},
|
||||
onSubmitted: (value) {
|
||||
// Player tapped 'Submit'/'Done' on their keyboard.
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Close'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
_controller.text = context.read<SettingsController>().playerName.value;
|
||||
super.didChangeDependencies();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user