flutter: Clear unused code

Note: Config.loadProfile is not await.
This commit is contained in:
Calcitem 2021-02-22 00:53:33 +08:00
parent e830513aa3
commit 110c0016e4
11 changed files with 5 additions and 770 deletions

View File

@ -28,8 +28,8 @@ import 'package:sanmill/generated/l10n.dart';
import 'package:sanmill/widgets/navigation_home_screen.dart';
import 'package:stack_trace/stack_trace.dart';
import 'common/config.dart';
import 'services/audios.dart';
import 'services/player.dart';
//import 'package:sentry_flutter/sentry_flutter.dart';
@ -127,7 +127,7 @@ class _SanmillAppState extends State<SanmillApp> {
void initState() {
super.initState();
Chain.capture(() {
Player.loadProfile();
Config.loadProfile();
});
}

View File

@ -1,78 +0,0 @@
/*
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:convert';
import 'package:uuid/uuid.dart';
import '../common/config.dart';
import '../common/profile.dart';
import 'ranks.dart';
class Player extends RankItem {
//
static Player _instance;
String _uuid;
static get shared => _instance;
static Future<Player> loadProfile() async {
if (_instance == null) {
_instance = Player();
await _instance._load();
}
return _instance;
}
_load() async {
final profile = await Profile.shared();
await Config.loadProfile();
_uuid = profile['player-uuid'];
if (_uuid == null) {
profile['player-uuid'] = _uuid = Uuid().v1();
} else {
//
final playerInfoJson = profile['_rank-player-info'] ?? '{}';
final values = jsonDecode(playerInfoJson);
name = values['name'] ?? 'Anonymous';
winCloudEngine = values['win_cloud_engine'] ?? 0;
winAi = values['win_ai'] ?? 0;
}
}
Player() : super.empty();
Future<void> increaseWinAi() async {
winAi++;
await saveAndUpload();
}
Future<void> saveAndUpload() async {
//
final profile = await Profile.shared();
profile['_rank-player-info'] = jsonEncode(toMap());
profile.commit();
await Ranks.mockUpload(uuid: _uuid, rank: this);
}
}

View File

@ -1,151 +0,0 @@
/*
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:convert';
import 'dart:io';
class RankItem {
//
String name;
int winCloudEngine, winAi;
RankItem(Map<String, dynamic> values) {
name = values['name'] ?? 'Anonymous';
winCloudEngine = values['win_cloud_engine'] ?? 0;
winAi = values['win_ai'] ?? 0;
}
RankItem.empty() {
name = 'Anonymous';
winCloudEngine = 0;
winAi = 0;
}
RankItem.mock() {
name = 'I am a hero';
winCloudEngine = 3;
winAi = 12;
}
Map<String, dynamic> toMap() => {
'name': name,
'win_cloud_engine': winCloudEngine,
'win_ai': winAi,
};
get score => winCloudEngine * 30 + winAi * 5;
}
class Ranks {
//
static const Host = 'api.calcitem.com';
static const Port = 3838;
static const QueryPath = '/ranks/';
static const UploadPath = '/ranks/upload';
static Future<List<RankItem>> load({pageIndex, int pageSize = 10}) async {
//
Uri url = Uri(
scheme: 'http',
host: Host,
port: Port,
path: QueryPath,
queryParameters: {
'page_index': '$pageIndex',
'page_size': '$pageSize',
},
);
final httpClient = HttpClient();
try {
final request = await httpClient.getUrl(url);
final response = await request.close();
final text = await response.transform(utf8.decoder).join();
final obj = jsonDecode(text);
if (obj is! List) {
print('Unexpected response: $text');
return null;
}
final array = obj as List;
final rankItems = List<RankItem>();
array.forEach((row) => rankItems.add(RankItem(row)));
return rankItems;
//
} catch (e) {
print('Error: $e');
} finally {
httpClient.close();
}
return null;
}
static Future<bool> upload({String uuid, RankItem rank}) async {
//
Uri url = Uri(
scheme: 'http',
host: Host,
port: Port,
path: UploadPath,
queryParameters: {
'uuid': uuid,
'name': rank.name,
'win_cloud_engine': '${rank.winCloudEngine}',
'win_ai': '${rank.winAi}',
});
final httpClient = HttpClient();
try {
final request = await httpClient.postUrl(url);
final response = await request.close();
final text = await response.transform(utf8.decoder).join();
print(text);
return true;
//
} catch (e) {
print('Error: $e');
} finally {
httpClient.close();
}
return false;
}
static Future<List<RankItem>> mockLoad({pageIndex, int pageSize = 10}) async {
return [
RankItem.mock(),
RankItem.mock(),
RankItem.mock(),
RankItem.mock(),
RankItem.mock(),
RankItem.mock()
];
}
static Future<bool> mockUpload({String uuid, RankItem rank}) async {
return true;
}
}

View File

@ -22,8 +22,6 @@ import 'package:sanmill/painting/board_painter.dart';
import 'package:sanmill/painting/pieces_painter.dart';
import 'package:sanmill/style/colors.dart';
import 'words_on_board.dart';
class Board extends StatelessWidget {
//
static const padding = 5.0;
@ -58,11 +56,8 @@ class Board extends StatelessWidget {
child: Container(
margin: EdgeInsets.symmetric(
vertical: padding,
horizontal: (width - padding * 2) / 6 / 2 +
padding -
WordsOnBoard.digitsFontSize / 2,
horizontal: (width - padding * 2) / 6 / 2 + padding,
),
//child: WordsOnBoard(),
),
),
);

View File

@ -1,103 +0,0 @@
/*
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 'package:flutter/material.dart';
import 'package:sanmill/generated/l10n.dart';
import 'package:sanmill/style/colors.dart';
class EditPage extends StatefulWidget {
//
final String title, initValue;
EditPage(this.title, {this.initValue});
@override
_EditPageState createState() => _EditPageState();
}
class _EditPageState extends State<EditPage> {
//
TextEditingController _textController;
FocusNode _commentFocus = FocusNode();
onSubmit(String input) {
Navigator.of(context).pop(input);
}
@override
void initState() {
//
_textController = TextEditingController();
_textController.text = widget.initValue;
Future.delayed(
Duration(milliseconds: 10),
() => FocusScope.of(context).requestFocus(_commentFocus),
);
super.initState();
}
@override
Widget build(BuildContext context) {
//
final inputBorder = OutlineInputBorder(
borderRadius: BorderRadius.circular(25),
borderSide: BorderSide(color: UIColors.secondaryColor),
);
return Scaffold(
appBar: AppBar(
title: Text(widget.title, style: TextStyle(fontFamily: '')),
actions: <Widget>[
TextButton(
child: Text(S.of(context).ok,
style: TextStyle(fontFamily: '', color: Colors.white)),
onPressed: () => onSubmit(_textController.text),
)
],
),
backgroundColor: UIColors.lightBackgroundColor,
body: Container(
margin: EdgeInsets.all(16),
child: Column(
children: <Widget>[
TextField(
controller: _textController,
decoration: InputDecoration(
contentPadding:
EdgeInsets.symmetric(vertical: 0, horizontal: 16),
enabledBorder: inputBorder,
focusedBorder: inputBorder,
),
style: TextStyle(
color: UIColors.primaryColor, fontSize: 16, fontFamily: ''),
onSubmitted: (input) => onSubmit(input),
focusNode: _commentFocus,
),
],
),
),
);
}
@override
void deactivate() {
FocusScope.of(context).requestFocus(FocusNode());
super.deactivate();
}
}

View File

@ -1,147 +0,0 @@
import 'package:flutter/material.dart';
import 'package:sanmill/style/app_theme.dart';
class FeedbackScreen extends StatefulWidget {
@override
_FeedbackScreenState createState() => _FeedbackScreenState();
}
class _FeedbackScreenState extends State<FeedbackScreen> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Container(
color: AppTheme.nearlyWhite,
child: SafeArea(
top: false,
child: Scaffold(
backgroundColor: AppTheme.nearlyWhite,
body: SingleChildScrollView(
child: SizedBox(
height: MediaQuery.of(context).size.height,
child: Column(
children: <Widget>[
Container(
padding: EdgeInsets.only(
top: MediaQuery.of(context).padding.top,
left: 16,
right: 16),
child: Image.asset('assets/images/feedbackImage.png'),
),
Container(
padding: const EdgeInsets.only(top: 8),
child: Text(
'Your FeedBack',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
Container(
padding: const EdgeInsets.only(top: 16),
child: const Text(
'Give your best time for this moment.',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 16,
),
),
),
_buildComposer(),
Padding(
padding: const EdgeInsets.only(top: 16),
child: Center(
child: Container(
width: 120,
height: 40,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius:
const BorderRadius.all(Radius.circular(4.0)),
boxShadow: <BoxShadow>[
BoxShadow(
color: Colors.grey.withOpacity(0.6),
offset: const Offset(4, 4),
blurRadius: 8.0),
],
),
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: () {
FocusScope.of(context).requestFocus(FocusNode());
},
child: Center(
child: Padding(
padding: const EdgeInsets.all(4.0),
child: Text(
'Send',
style: TextStyle(
fontWeight: FontWeight.w500,
color: Colors.white,
),
),
),
),
),
),
),
),
)
],
),
),
),
),
),
);
}
Widget _buildComposer() {
return Padding(
padding: const EdgeInsets.only(top: 16, left: 32, right: 32),
child: Container(
decoration: BoxDecoration(
color: AppTheme.white,
borderRadius: BorderRadius.circular(8),
boxShadow: <BoxShadow>[
BoxShadow(
color: Colors.grey.withOpacity(0.8),
offset: const Offset(4, 4),
blurRadius: 8),
],
),
child: ClipRRect(
borderRadius: BorderRadius.circular(25),
child: Container(
padding: const EdgeInsets.all(4.0),
constraints: const BoxConstraints(minHeight: 80, maxHeight: 160),
color: AppTheme.white,
child: SingleChildScrollView(
padding:
const EdgeInsets.only(left: 10, right: 10, top: 0, bottom: 0),
child: TextField(
maxLines: null,
onChanged: (String txt) {},
style: TextStyle(
fontFamily: AppTheme.fontName,
fontSize: 16,
color: AppTheme.dark_grey,
),
cursorColor: Colors.blue,
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Enter your feedback...'),
),
),
),
),
),
);
}
}

View File

@ -28,7 +28,6 @@ import 'package:sanmill/mill/game.dart';
import 'package:sanmill/mill/mill.dart';
import 'package:sanmill/mill/types.dart';
import 'package:sanmill/services/audios.dart';
import 'package:sanmill/services/player.dart';
import 'package:sanmill/style/colors.dart';
import 'package:sanmill/style/toast.dart';
@ -434,13 +433,6 @@ class _GamePageState extends State<GamePage> with RouteAware {
);
},
);
if (result == GameResult.win) {
if (widget.engineType == EngineType.humanVsCloud)
Player.shared.increaseWinCloudEngine();
else
Player.shared.increaseWinAi();
}
}
void calcScreenPaddingH() {

View File

@ -20,11 +20,8 @@ import 'package:flutter/material.dart';
import 'package:sanmill/common/config.dart';
import 'package:sanmill/generated/l10n.dart';
import 'package:sanmill/services/audios.dart';
import 'package:sanmill/services/player.dart';
import 'package:sanmill/style/colors.dart';
import 'edit_page.dart';
class GameSettingsPage extends StatefulWidget {
@override
_GameSettingsPageState createState() => _GameSettingsPageState();
@ -181,26 +178,6 @@ class _GameSettingsPageState extends State<GameSettingsPage> {
Config.save();
}
changeName() async {
//
final newName = await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => EditPage(S.of(context).playerName,
initValue: Player.shared.name)),
);
if (newName != null) nameChanged(newName);
}
nameChanged(String newName) async {
//
setState(() {
Player.shared.name = newName;
});
Player.shared.saveAndUpload();
}
@override
Widget build(BuildContext context) {
//
@ -310,27 +287,6 @@ class _GameSettingsPageState extends State<GameSettingsPage> {
),
),
const SizedBox(height: 16),
Text(S.of(context).leaderBoard, style: headerStyle),
Card(
color: UIColors.boardBackgroundColor,
margin: const EdgeInsets.symmetric(vertical: 10),
child: Column(
children: <Widget>[
ListTile(
title: Text(S.of(context).playerName, style: itemStyle),
trailing:
Row(mainAxisSize: MainAxisSize.min, children: <Widget>[
Text(Player.shared.name),
Icon(Icons.keyboard_arrow_right,
color: UIColors.secondaryColor),
]),
onTap: changeName,
),
],
),
),
const SizedBox(height: 16),
const SizedBox(height: 60.0),
],
),
),

View File

@ -1,181 +0,0 @@
/*
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 'package:flutter/material.dart';
import 'package:sanmill/engine/engine.dart';
import 'package:sanmill/generated/l10n.dart';
import 'package:sanmill/style/colors.dart';
import 'game_page.dart';
import 'game_settings_page.dart';
class MainMenu extends StatefulWidget {
@override
_MainMenuState createState() => _MainMenuState();
}
class _MainMenuState extends State<MainMenu> with TickerProviderStateMixin {
AnimationController inController, shadowController;
Animation inAnimation, shadowAnimation;
@override
void initState() {
super.initState();
inController = AnimationController(
duration: Duration(milliseconds: 600),
vsync: this,
);
inAnimation = CurvedAnimation(parent: inController, curve: Curves.bounceIn);
inAnimation = new Tween(begin: 1.6, end: 1.0).animate(inController);
shadowController = AnimationController(
duration: Duration(milliseconds: 1500),
vsync: this,
);
shadowAnimation =
new Tween(begin: 0.0, end: 12.0).animate(shadowController);
inController.addStatusListener((status) {
if (status == AnimationStatus.completed) shadowController.forward();
});
shadowController.addStatusListener((status) {
if (status == AnimationStatus.completed) shadowController.reverse();
});
/// TODO: use 'try...catch' to avoid exception -
/// 'setState() or markNeedsBuild() called during build.'
inAnimation.addListener(() {
setState(() {});
});
shadowAnimation.addListener(() {
setState(() {});
});
inController.forward();
}
navigateTo(Widget page) async {
await Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => page));
inController.reset();
shadowController.reset();
inController.forward();
}
@override
Widget build(BuildContext context) {
//
final appNameShadow = Shadow(
color: Color.fromARGB(0x99, 66, 0, 0),
offset: Offset(0, shadowAnimation.value / 2),
blurRadius: shadowAnimation.value,
);
final menuItemShadow = Shadow(
color: Color.fromARGB(0x7F, 0, 0, 0),
offset: Offset(0, shadowAnimation.value / 6),
blurRadius: shadowAnimation.value / 3,
);
final appNameStyle = TextStyle(
fontSize: 48,
color: Colors.black,
shadows: [appNameShadow],
);
final menuItemStyle = TextStyle(
fontSize: 28,
color: UIColors.primaryColor,
shadows: [menuItemShadow],
);
final menuItems = Center(
child: Column(
children: <Widget>[
Expanded(child: SizedBox()),
Transform.scale(
scale: inAnimation.value,
child: Text(S.of(context).appName,
style: appNameStyle, textAlign: TextAlign.center),
),
Expanded(child: SizedBox()),
TextButton(
child: Text(S.of(context).humanVsAi, style: menuItemStyle),
onPressed: () => navigateTo(GamePage(EngineType.humanVsAi)),
),
Expanded(child: SizedBox()),
TextButton(
child: Text(S.of(context).humanVsHuman, style: menuItemStyle),
onPressed: () => navigateTo(GamePage(EngineType.humanVsHuman)),
),
Expanded(child: SizedBox()),
TextButton(
child: Text(S.of(context).aiVsAi, style: menuItemStyle),
onPressed: () => navigateTo(GamePage(EngineType.aiVsAi)),
),
Expanded(child: SizedBox()),
TextButton(
child: Text(S.of(context).settings, style: menuItemStyle),
onPressed: () => Navigator.of(context).push(
MaterialPageRoute(builder: (context) => GameSettingsPage()),
),
),
Expanded(child: SizedBox()),
Text(S.of(context).gameWarning,
style: TextStyle(color: Colors.black54, fontSize: 11),
textAlign: TextAlign.center),
Expanded(child: SizedBox()),
Text(S.of(context).copyright,
style: TextStyle(color: Colors.black54, fontSize: 16)),
Expanded(child: SizedBox()),
],
),
);
return Scaffold(
backgroundColor: UIColors.lightBackgroundColor,
body: Stack(
children: <Widget>[
menuItems,
/*
Positioned(
top: SanmillApp.StatusBarHeight,
left: 10,
child: IconButton(
icon: Icon(Icons.menu, color: UIColors.primaryColor),
onPressed: () => Navigator.of(context).push(
MaterialPageRoute(builder: (context) => SettingsPage()),
),
),
),
*/
],
),
);
}
@override
void dispose() {
//
inController.dispose();
shadowController.dispose();
super.dispose();
}
}

View File

@ -185,7 +185,8 @@ class _RuleSettingsPageState extends State<RuleSettingsPage> {
return Scaffold(
backgroundColor: UIColors.lightBackgroundColor,
appBar: AppBar(centerTitle: true, title: Text(S.of(context).settings)),
appBar:
AppBar(centerTitle: true, title: Text(S.of(context).ruleSettings)),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(

View File

@ -1,49 +0,0 @@
/*
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 'package:flutter/material.dart';
import 'package:sanmill/style/colors.dart';
class WordsOnBoard extends StatelessWidget {
//
static const digitsFontSize = 18.0;
@override
Widget build(BuildContext context) {
final bChildren = <Widget>[], rChildren = <Widget>[];
for (var i = 0; i < 7; i++) {
if (i < 8) {
bChildren.add(Expanded(child: SizedBox()));
rChildren.add(Expanded(child: SizedBox()));
}
}
return DefaultTextStyle(
child: Column(
children: <Widget>[
Row(children: bChildren),
Expanded(child: SizedBox()),
Expanded(child: SizedBox()),
Row(children: rChildren),
],
),
style: TextStyle(color: UIColors.boardTipsColor),
);
}
}