add setting, alertbox, etc

This commit is contained in:
Simon Ding
2024-07-05 14:45:42 +08:00
parent 321e36bb43
commit 6e50e84694
9 changed files with 167 additions and 10 deletions

View File

@@ -0,0 +1,93 @@
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:ui/APIs.dart';
import 'package:ui/server_response.dart';
import 'package:ui/utils.dart';
class SystemSettingsPage extends StatefulWidget {
static const route = "/systemsettings";
@override
State<StatefulWidget> createState() {
return _SystemSettingsPageState();
}
}
class _SystemSettingsPageState extends State<SystemSettingsPage> {
final GlobalKey _formKey = GlobalKey<FormState>();
final TextEditingController _tmdbApiKeyController = TextEditingController();
@override
Widget build(BuildContext context) {
_handleRefresh();
return Expanded(
child: RefreshIndicator(
onRefresh: _handleRefresh,
child: Form(
key: _formKey, //设置globalKey用于后面获取FormState
autovalidateMode: AutovalidateMode.onUserInteraction,
child: Column(
children: [
TextFormField(
autofocus: true,
controller: _tmdbApiKeyController,
decoration: const InputDecoration(
labelText: "TMDB Api Key",
icon: Icon(Icons.key),
),
// 校验用户名
validator: (v) {
return v!.trim().isNotEmpty ? null : "ApiKey 不能为空";
},
),
Padding(
padding: const EdgeInsets.only(top: 28.0),
child: Row(
children: <Widget>[
Center(
child: ElevatedButton(
child: const Padding(
padding: EdgeInsets.all(16.0),
child: Text("保存"),
),
onPressed: () {
// 通过_formKey.currentState 获取FormState后
// 调用validate()方法校验用户名密码是否合法,校验
// 通过后再提交数据。
if ((_formKey.currentState as FormState)
.validate()) {
_submitSettings(context,_tmdbApiKeyController.text);
}
},
),
),
],
),
)
],
),
)),
);
}
Future<void> _handleRefresh() async {
final dio = Dio();
var resp = await dio
.get(APIs.settingsUrl, queryParameters: {"key": APIs.tmdbApiKey});
var rrr = resp.data as Map<String, dynamic>;
var data = rrr["data"] as Map<String, dynamic>;
var key = data[APIs.tmdbApiKey] as String;
_tmdbApiKeyController.text = key;
// Fetch new data and update the UI
}
void _submitSettings(BuildContext context, String v) async {
var resp = await Dio().post(APIs.settingsUrl, data: {APIs.tmdbApiKey: v});
var sp = ServerResponse.fromJson(resp.data as Map<String, dynamic>);
if (sp.code != 0) {
if (context.mounted) {
Utils.showAlertDialog(context, sp.message);
}
}
}
}