flutter: refactor: Move countdown dialog to dialog.dart
This commit is contained in:
parent
4c54ace4c0
commit
98872c1b83
|
@ -0,0 +1,98 @@
|
||||||
|
/*
|
||||||
|
This file is part of Sanmill.
|
||||||
|
Copyright (C) 2019-2021 The Sanmill developers (see AUTHORS file)
|
||||||
|
|
||||||
|
Sanmill is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
Sanmill is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:sanmill/generated/l10n.dart';
|
||||||
|
|
||||||
|
int _counter = 0;
|
||||||
|
Timer? _timer;
|
||||||
|
|
||||||
|
void startTimer(var counter, var events) {
|
||||||
|
_counter = counter;
|
||||||
|
if (_timer != null) {
|
||||||
|
_timer!.cancel();
|
||||||
|
}
|
||||||
|
_timer = Timer.periodic(Duration(seconds: 1), (timer) {
|
||||||
|
(_counter > 0) ? _counter-- : _timer!.cancel();
|
||||||
|
events.add(_counter);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void showCountdownDialog(
|
||||||
|
BuildContext ctx, var seconds, var events, void fun()) {
|
||||||
|
var alert = AlertDialog(
|
||||||
|
content: StreamBuilder<int>(
|
||||||
|
stream: events.stream,
|
||||||
|
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
|
||||||
|
print("Count down: " + snapshot.data.toString());
|
||||||
|
|
||||||
|
if (snapshot.data == 0) {
|
||||||
|
fun();
|
||||||
|
if (Platform.isAndroid) {
|
||||||
|
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
|
||||||
|
} else {}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Container(
|
||||||
|
height: 128,
|
||||||
|
child: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.center,
|
||||||
|
children: <Widget>[
|
||||||
|
Text(
|
||||||
|
snapshot.data != null ? '${snapshot.data.toString()}' : "10",
|
||||||
|
style: TextStyle(fontSize: 64),
|
||||||
|
),
|
||||||
|
SizedBox(
|
||||||
|
height: 20,
|
||||||
|
),
|
||||||
|
InkWell(
|
||||||
|
onTap: () {
|
||||||
|
Navigator.of(context).pop();
|
||||||
|
},
|
||||||
|
child: Container(
|
||||||
|
child: Center(
|
||||||
|
child: Text(
|
||||||
|
S.of(ctx).cancel,
|
||||||
|
style: TextStyle(
|
||||||
|
color: Colors.black,
|
||||||
|
fontSize: 16,
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
),
|
||||||
|
)),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
startTimer(seconds, events);
|
||||||
|
|
||||||
|
showDialog(
|
||||||
|
context: ctx,
|
||||||
|
builder: (BuildContext c) {
|
||||||
|
return alert;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
|
@ -20,7 +20,6 @@ import 'dart:async';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
|
||||||
import 'package:sanmill/common/config.dart';
|
import 'package:sanmill/common/config.dart';
|
||||||
import 'package:sanmill/common/settings.dart';
|
import 'package:sanmill/common/settings.dart';
|
||||||
import 'package:sanmill/generated/l10n.dart';
|
import 'package:sanmill/generated/l10n.dart';
|
||||||
|
@ -29,6 +28,7 @@ import 'package:sanmill/widgets/settings_card.dart';
|
||||||
import 'package:sanmill/widgets/settings_list_tile.dart';
|
import 'package:sanmill/widgets/settings_list_tile.dart';
|
||||||
import 'package:sanmill/widgets/settings_switch_list_tile.dart';
|
import 'package:sanmill/widgets/settings_switch_list_tile.dart';
|
||||||
|
|
||||||
|
import 'dialog.dart';
|
||||||
import 'list_item_divider.dart';
|
import 'list_item_divider.dart';
|
||||||
|
|
||||||
class Developer {
|
class Developer {
|
||||||
|
@ -44,9 +44,7 @@ class _GameSettingsPageState extends State<GameSettingsPage> {
|
||||||
Color pickerColor = Color(0xFF808080);
|
Color pickerColor = Color(0xFF808080);
|
||||||
Color currentColor = Color(0xFF808080);
|
Color currentColor = Color(0xFF808080);
|
||||||
|
|
||||||
int _counter = 0;
|
|
||||||
late StreamController<int> _events;
|
late StreamController<int> _events;
|
||||||
Timer? _timer;
|
|
||||||
|
|
||||||
final String tag = "[game_settings_page]";
|
final String tag = "[game_settings_page]";
|
||||||
|
|
||||||
|
@ -57,75 +55,9 @@ class _GameSettingsPageState extends State<GameSettingsPage> {
|
||||||
_events.add(10);
|
_events.add(10);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _startTimer() {
|
|
||||||
_counter = 10;
|
|
||||||
if (_timer != null) {
|
|
||||||
_timer!.cancel();
|
|
||||||
}
|
|
||||||
_timer = Timer.periodic(Duration(seconds: 1), (timer) {
|
|
||||||
(_counter > 0) ? _counter-- : _timer!.cancel();
|
|
||||||
_events.add(_counter);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
void _restore() async {
|
void _restore() async {
|
||||||
final profile = await Settings.instance();
|
final settings = await Settings.instance();
|
||||||
await profile.restore();
|
await settings.restore();
|
||||||
}
|
|
||||||
|
|
||||||
void showCountdownDialog(BuildContext ctx) {
|
|
||||||
var alert = AlertDialog(
|
|
||||||
content: StreamBuilder<int>(
|
|
||||||
stream: _events.stream,
|
|
||||||
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
|
|
||||||
print("Count down: " + snapshot.data.toString());
|
|
||||||
|
|
||||||
if (snapshot.data == 0) {
|
|
||||||
_restore();
|
|
||||||
if (Platform.isAndroid) {
|
|
||||||
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
|
|
||||||
} else {}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Container(
|
|
||||||
height: 128,
|
|
||||||
child: Column(
|
|
||||||
crossAxisAlignment: CrossAxisAlignment.center,
|
|
||||||
children: <Widget>[
|
|
||||||
Text(
|
|
||||||
snapshot.data != null
|
|
||||||
? '${snapshot.data.toString()}'
|
|
||||||
: "10",
|
|
||||||
style: TextStyle(
|
|
||||||
fontSize: 64,
|
|
||||||
)),
|
|
||||||
SizedBox(
|
|
||||||
height: 20,
|
|
||||||
),
|
|
||||||
InkWell(
|
|
||||||
onTap: () {
|
|
||||||
Navigator.of(context).pop();
|
|
||||||
},
|
|
||||||
child: Container(
|
|
||||||
child: Center(
|
|
||||||
child: Text(
|
|
||||||
S.of(ctx).cancel,
|
|
||||||
style: TextStyle(
|
|
||||||
color: Colors.black,
|
|
||||||
fontSize: 16,
|
|
||||||
fontWeight: FontWeight.bold),
|
|
||||||
)),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}));
|
|
||||||
showDialog(
|
|
||||||
context: ctx,
|
|
||||||
builder: (BuildContext c) {
|
|
||||||
return alert;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SliderTheme _skillLevelSliderTheme(context, setState) {
|
SliderTheme _skillLevelSliderTheme(context, setState) {
|
||||||
|
@ -174,8 +106,7 @@ class _GameSettingsPageState extends State<GameSettingsPage> {
|
||||||
confirm() async {
|
confirm() async {
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
if (Platform.isAndroid) {
|
if (Platform.isAndroid) {
|
||||||
_startTimer();
|
showCountdownDialog(context, 10, _events, _restore);
|
||||||
showCountdownDialog(context);
|
|
||||||
} else {
|
} else {
|
||||||
_restore();
|
_restore();
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
|
Loading…
Reference in New Issue