mirror of
https://github.com/simon-ding/polaris.git
synced 2026-06-08 02:57:38 +08:00
add setting, alertbox, etc
This commit is contained in:
@@ -29,5 +29,5 @@ func (s *Server) GetSetting(c *gin.Context) (interface{}, error) {
|
|||||||
}
|
}
|
||||||
v := s.db.GetSetting(q)
|
v := s.db.GetSetting(q)
|
||||||
log.Infof("get value for key %v: %v", q, v)
|
log.Infof("get value for key %v: %v", q, v)
|
||||||
return v, nil
|
return gin.H{q: v}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
class APIs {
|
class APIs {
|
||||||
static const _baseUrl = "http://127.0.0.1:8080";
|
static const _baseUrl = "http://127.0.0.1:8080";
|
||||||
static const searchUrl = "$_baseUrl/api/v1/tv/search";
|
static const searchUrl = "$_baseUrl/api/v1/tv/search";
|
||||||
|
static const settingsUrl = "$_baseUrl/api/v1/setting/do";
|
||||||
|
|
||||||
static const tmdbImgBaseUrl = "https://image.tmdb.org/t/p/w500/";
|
static const tmdbImgBaseUrl = "https://image.tmdb.org/t/p/w500/";
|
||||||
|
|
||||||
|
static const tmdbApiKey = "tmdb_api_key";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:ui/navdrawer.dart';
|
import 'package:ui/navdrawer.dart';
|
||||||
import 'package:ui/search.dart';
|
import 'package:ui/search.dart';
|
||||||
|
import 'package:ui/system_settings.dart';
|
||||||
import 'package:ui/weclome.dart';
|
import 'package:ui/weclome.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
@@ -41,19 +42,23 @@ class MyApp extends StatelessWidget {
|
|||||||
},
|
},
|
||||||
routes: [
|
routes: [
|
||||||
GoRoute(
|
GoRoute(
|
||||||
path: '/',
|
path: WelcomePage.route,
|
||||||
builder: (context, state) => WelcomePage(),
|
builder: (context, state) => WelcomePage(),
|
||||||
),
|
),
|
||||||
GoRoute(
|
GoRoute(
|
||||||
path: "/search",
|
path: SearchPage.route,
|
||||||
builder: (context, state) => const SearchPage(),
|
builder: (context, state) => const SearchPage(),
|
||||||
|
),
|
||||||
|
GoRoute(
|
||||||
|
path: SystemSettingsPage.route,
|
||||||
|
builder: (context, state) => SystemSettingsPage(),
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
final _router = GoRouter(
|
final _router = GoRouter(
|
||||||
navigatorKey: _rootNavigatorKey,
|
navigatorKey: _rootNavigatorKey,
|
||||||
initialLocation: '/',
|
initialLocation: WelcomePage.route,
|
||||||
routes: [
|
routes: [
|
||||||
_shellRoute,
|
_shellRoute,
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
|
import 'package:ui/search.dart';
|
||||||
|
import 'package:ui/system_settings.dart';
|
||||||
|
import 'package:ui/weclome.dart';
|
||||||
|
|
||||||
class NavDrawer extends StatefulWidget {
|
class NavDrawer extends StatefulWidget {
|
||||||
@override
|
@override
|
||||||
@@ -27,9 +30,11 @@ class _NavDrawerState extends State<NavDrawer> {
|
|||||||
_counter = value;
|
_counter = value;
|
||||||
});
|
});
|
||||||
if (value == 0) {
|
if (value == 0) {
|
||||||
context.go('/');
|
context.go(WelcomePage.route);
|
||||||
} else if (value == 1) {
|
} else if (value == 1) {
|
||||||
context.go("/search");
|
context.go(SearchPage.route);
|
||||||
|
} else if (value == 2) {
|
||||||
|
context.go(SystemSettingsPage.route);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
extended: MediaQuery.of(context).size.width >= 850,
|
extended: MediaQuery.of(context).size.width >= 850,
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
import 'package:ui/APIs.dart';
|
import 'package:ui/APIs.dart';
|
||||||
|
import 'package:ui/server_response.dart';
|
||||||
|
import 'package:ui/utils.dart';
|
||||||
|
|
||||||
class SearchPage extends StatefulWidget {
|
class SearchPage extends StatefulWidget {
|
||||||
const SearchPage({super.key});
|
const SearchPage({super.key});
|
||||||
|
|
||||||
|
static const route = "/search";
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<StatefulWidget> createState() {
|
State<StatefulWidget> createState() {
|
||||||
return _SearchPageState();
|
return _SearchPageState();
|
||||||
@@ -14,15 +18,19 @@ class SearchPage extends StatefulWidget {
|
|||||||
class _SearchPageState extends State<SearchPage> {
|
class _SearchPageState extends State<SearchPage> {
|
||||||
List<dynamic> list = List.empty();
|
List<dynamic> list = List.empty();
|
||||||
|
|
||||||
void _queryResults(String q) async {
|
void _queryResults(BuildContext context, String q) async {
|
||||||
final dio = Dio();
|
final dio = Dio();
|
||||||
var resp = await dio.get(APIs.searchUrl, queryParameters: {"query": q});
|
var resp = await dio.get(APIs.searchUrl, queryParameters: {"query": q});
|
||||||
//var dy = jsonDecode(resp.data.toString());
|
//var dy = jsonDecode(resp.data.toString());
|
||||||
|
|
||||||
print("search page results: ${resp.data}");
|
print("search page results: ${resp.data}");
|
||||||
|
var rsp = ServerResponse.fromJson(resp.data as Map<String, dynamic>);
|
||||||
|
if (rsp.code != 0 && context.mounted) {
|
||||||
|
Utils.showAlertDialog(context, rsp.message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var rsp = resp.data as Map<String, dynamic>;
|
var data = rsp.data as Map<String, dynamic>;
|
||||||
var data = rsp["data"] as Map<String, dynamic>;
|
|
||||||
var results = data["results"] as List<dynamic>;
|
var results = data["results"] as List<dynamic>;
|
||||||
|
|
||||||
setState(() {
|
setState(() {
|
||||||
@@ -80,7 +88,7 @@ class _SearchPageState extends State<SearchPage> {
|
|||||||
children: [
|
children: [
|
||||||
TextField(
|
TextField(
|
||||||
autofocus: true,
|
autofocus: true,
|
||||||
onSubmitted: (value) => _queryResults(value),
|
onSubmitted: (value) => _queryResults(context,value),
|
||||||
decoration: const InputDecoration(
|
decoration: const InputDecoration(
|
||||||
labelText: "搜索",
|
labelText: "搜索",
|
||||||
hintText: "搜索剧集名称",
|
hintText: "搜索剧集名称",
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
class ServerResponse {
|
||||||
|
final int code;
|
||||||
|
final String message;
|
||||||
|
final dynamic data;
|
||||||
|
ServerResponse(this.code, this.message, this.data);
|
||||||
|
|
||||||
|
ServerResponse.fromJson(Map<String, dynamic> json)
|
||||||
|
: code = json["code"] as int,
|
||||||
|
message = json["message"] as String,
|
||||||
|
data = json["data"];
|
||||||
|
}
|
||||||
|
|||||||
93
ui/lib/system_settings.dart
Normal file
93
ui/lib/system_settings.dart
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
31
ui/lib/utils.dart
Normal file
31
ui/lib/utils.dart
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
class Utils {
|
||||||
|
static Future<void> showAlertDialog(BuildContext context, String msg) async {
|
||||||
|
|
||||||
|
return showDialog<void>(
|
||||||
|
context: context,
|
||||||
|
barrierDismissible: true, // user must tap button!
|
||||||
|
builder: (BuildContext context) {
|
||||||
|
return AlertDialog(
|
||||||
|
title: const Text('警告 ⚠️'),
|
||||||
|
content: SingleChildScrollView(
|
||||||
|
child: ListBody(
|
||||||
|
children: <Widget>[
|
||||||
|
Text(msg),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
actions: <Widget>[
|
||||||
|
TextButton(
|
||||||
|
child: const Text('确定'),
|
||||||
|
onPressed: () {
|
||||||
|
Navigator.of(context).pop();
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ import 'package:ui/APIs.dart';
|
|||||||
|
|
||||||
class WelcomePage extends StatefulWidget {
|
class WelcomePage extends StatefulWidget {
|
||||||
const WelcomePage({super.key});
|
const WelcomePage({super.key});
|
||||||
|
static const route = "/welcome";
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<StatefulWidget> createState() {
|
State<StatefulWidget> createState() {
|
||||||
|
|||||||
Reference in New Issue
Block a user