1
0
mirror of https://github.com/flutter/samples.git synced 2026-03-23 12:51:57 +00:00
Files
samples/game_template/lib/src/settings/custom_name_dialog.dart
Toshik 6e6bce1998 fix: import fixed to make game_template working out of this repository (#1329)
* fix: import fixed to make game_template working out of this repository

* fix: import fixed to make game_template working out of this repository
2022-07-20 05:51:27 +10:00

76 lines
2.2 KiB
Dart

// 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:provider/provider.dart';
import 'settings.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, super.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();
}
}