pikapika/lib/screens/GamesScreen.dart

264 lines
8.8 KiB
Dart
Raw Permalink Normal View History

2021-09-29 23:57:09 +00:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
2021-11-11 03:00:38 +00:00
import 'package:pikapika/basic/Entities.dart';
import 'package:pikapika/basic/Method.dart';
import 'package:pikapika/screens/components/ContentBuilder.dart';
2021-09-29 23:57:09 +00:00
import 'GameInfoScreen.dart';
import 'components/Images.dart';
2022-03-19 04:12:27 +00:00
import 'components/RightClickPop.dart';
2021-09-29 23:57:09 +00:00
2021-10-26 11:04:23 +00:00
// 游戏列表
2021-09-29 23:57:09 +00:00
class GamesScreen extends StatefulWidget {
2022-03-17 03:31:25 +00:00
const GamesScreen({Key? key}) : super(key: key);
2021-09-29 23:57:09 +00:00
@override
State<StatefulWidget> createState() => _GamesScreenState();
}
class _GamesScreenState extends State<GamesScreen> {
int _currentPage = 1;
late Future<GamePage> _future = _loadPage();
Future<GamePage> _loadPage() {
return method.games(_currentPage);
}
void _onPageChange(int number) {
setState(() {
_currentPage = number;
_future = _loadPage();
});
}
@override
2022-03-19 04:12:27 +00:00
Widget build(BuildContext context){
2022-03-25 14:57:30 +00:00
return rightClickPop(
child: buildScreen(context),
context: context,
canPop: true,
);
2022-03-19 04:12:27 +00:00
}
Widget buildScreen(BuildContext context) {
2021-09-29 23:57:09 +00:00
return Scaffold(
appBar: AppBar(
2022-03-19 04:12:27 +00:00
title: const Text('游戏'),
2021-09-29 23:57:09 +00:00
),
body: ContentBuilder(
future: _future,
onRefresh: _loadPage,
successBuilder:
(BuildContext context, AsyncSnapshot<GamePage> snapshot) {
var page = snapshot.data!;
List<Wrap> wraps = [];
GameCard? gameCard;
2022-03-17 03:31:25 +00:00
for (var element in page.docs) {
2021-09-29 23:57:09 +00:00
if (gameCard == null) {
gameCard = GameCard(element);
} else {
wraps.add(Wrap(
2022-03-17 09:52:51 +00:00
children: [GameCard(element), gameCard],
2021-09-29 23:57:09 +00:00
alignment: WrapAlignment.center,
));
gameCard = null;
}
2022-03-17 03:31:25 +00:00
}
2021-09-29 23:57:09 +00:00
if (gameCard != null) {
wraps.add(Wrap(
2022-03-17 09:52:51 +00:00
children: [gameCard],
2021-09-29 23:57:09 +00:00
alignment: WrapAlignment.center,
));
}
return Scaffold(
appBar: PreferredSize(
2022-03-19 04:12:27 +00:00
preferredSize: const Size.fromHeight(40),
2021-09-29 23:57:09 +00:00
child: Container(
2022-03-19 04:12:27 +00:00
padding: const EdgeInsets.only(left: 10, right: 10),
2021-09-29 23:57:09 +00:00
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
width: .5,
style: BorderStyle.solid,
color: Colors.grey[200]!,
),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
InkWell(
onTap: () {
_textEditController.clear();
showDialog(
context: context,
builder: (context) {
return AlertDialog(
content: Card(
2022-03-17 03:31:25 +00:00
child: TextField(
controller: _textEditController,
decoration: const InputDecoration(
labelText: "请输入页数:",
2021-09-29 23:57:09 +00:00
),
2022-03-17 03:31:25 +00:00
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(
RegExp(r'\d+')),
],
2021-09-29 23:57:09 +00:00
),
),
actions: <Widget>[
MaterialButton(
onPressed: () {
Navigator.pop(context);
},
2022-03-17 03:31:25 +00:00
child: const Text('取消'),
2021-09-29 23:57:09 +00:00
),
MaterialButton(
onPressed: () {
Navigator.pop(context);
var text = _textEditController.text;
2022-03-17 03:31:25 +00:00
if (text.isEmpty || text.length > 5) {
2021-09-29 23:57:09 +00:00
return;
}
var num = int.parse(text);
if (num == 0 || num > page.pages) {
return;
}
_onPageChange(num);
},
2022-03-17 03:31:25 +00:00
child: const Text('确定'),
2021-09-29 23:57:09 +00:00
),
],
);
},
);
},
child: Row(
children: [
Text("${page.page} / ${page.pages}"),
],
),
),
Row(
children: [
MaterialButton(
minWidth: 0,
onPressed: () {
if (page.page > 1) {
_onPageChange(page.page - 1);
}
},
2022-03-25 14:57:30 +00:00
child: const Text('上一页'),
2021-09-29 23:57:09 +00:00
),
MaterialButton(
minWidth: 0,
onPressed: () {
if (page.page < page.pages) {
_onPageChange(page.page + 1);
}
},
2022-03-25 14:57:30 +00:00
child: const Text('下一页'),
2021-09-29 23:57:09 +00:00
)
],
),
],
),
),
),
body: ListView(
children: [
...wraps,
...page.page < page.pages
? [
MaterialButton(
onPressed: () {
_onPageChange(page.page + 1);
},
child: Container(
2022-03-19 04:12:27 +00:00
padding: const EdgeInsets.only(top: 30, bottom: 30),
2022-03-25 14:57:30 +00:00
child: const Text('下一页'),
2021-09-29 23:57:09 +00:00
),
),
]
: [],
],
),
);
},
),
);
}
}
class GameCard extends StatelessWidget {
final GameSimple info;
2022-03-17 03:31:25 +00:00
const GameCard(this.info, {Key? key}) : super(key: key);
2021-09-29 23:57:09 +00:00
@override
Widget build(BuildContext context) {
var theme = Theme.of(context);
var textColor = theme.textTheme.bodyText1!.color!;
var categoriesStyle = TextStyle(
fontSize: 13,
color: textColor.withAlpha(0xCC),
);
return LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
// data.width/data.height = width/ ?
// data.width * ? = width * data.height
// ? = width * data.height / data.width
var size = MediaQuery.of(context).size;
var min = size.width < size.height ? size.width : size.height;
var imageWidth = (min - 45 - 40) / 2;
var imageHeight = imageWidth * 280 / 500;
return Card(
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => GameInfoScreen(info.id)),
);
},
child: Container(
2022-03-17 03:31:25 +00:00
padding: const EdgeInsets.all(10),
child: SizedBox(
2021-09-29 23:57:09 +00:00
width: imageWidth,
child: Column(
children: [
RemoteImage(
width: imageWidth,
height: imageHeight,
fileServer: info.icon.fileServer,
path: info.icon.path,
),
Text(
info.title + '\n',
maxLines: 1,
overflow: TextOverflow.ellipsis,
2022-03-25 14:57:30 +00:00
style: const TextStyle(height: 1.4),
strutStyle: const StrutStyle(height: 1.4),
2021-09-29 23:57:09 +00:00
),
Text(
info.publisher,
style: categoriesStyle,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
],
),
),
),
),
);
},
);
}
}
final TextEditingController _textEditController =
TextEditingController(text: '');