migrate to go_router

This commit is contained in:
Michael 2025-04-27 18:17:40 +03:00
parent e56cb2ec50
commit 5f8f39ab53
5 changed files with 277 additions and 245 deletions

View file

@ -18,146 +18,163 @@ class _AppPageState extends State<AppPage> {
super.initState(); super.initState();
_loadAppInfo(); _loadAppInfo();
} }
void _loadAppInfo() async { void _loadAppInfo() async {
var rq = await http.get( var rq = await http.get(Uri.https(
Uri.https( "backapi.rustore.ru",
"backapi.rustore.ru", "/applicationData/overallInfo/${widget.packageName}",
"/applicationData/overallInfo/${widget.packageName}", ));
) print(widget.packageName + "PN");
);
if (rq.statusCode != 200) { if (rq.statusCode != 200) {
showDialog( showDialog(
context: context, context: context,
builder: (context) => AlertDialog( builder: (context) => AlertDialog(
title: const Text("Что то пошло не так"), title: const Text("Что то пошло не так"),
content: Text("RuStore вернул код ${rq.statusCode}"), content: Text("RuStore вернул код ${rq.statusCode}"),
actions: [ actions: [
FilledButton( FilledButton(
onPressed: () { onPressed: () {
Navigator.of(context) Navigator.of(context)
..pop() ..pop()
..pop(); ..pop();
}, },
child: const Text("ок"), child: const Text("ок"),
) )
], ],
) ));
);
} }
setState(() { setState(() {
_appInfo = json.decode(utf8.decode(rq.bodyBytes))["body"]; _appInfo = json.decode(utf8.decode(rq.bodyBytes))["body"] ?? {};
}); });
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return _appInfo != null ? ListView( return Scaffold(
padding: const EdgeInsets.fromLTRB(15, 0, 15, 15), appBar: AppBar(),
children: [ body: _appInfo != null
Row( ? ListView(
crossAxisAlignment: CrossAxisAlignment.start, padding: const EdgeInsets.fromLTRB(15, 0, 15, 15),
children: [ children: [
ClipRRect( Row(
borderRadius: BorderRadius.circular(25), crossAxisAlignment: CrossAxisAlignment.start,
child: Image.network( children: [
_appInfo?["iconUrl"], ClipRRect(
width: 120, borderRadius: BorderRadius.circular(25),
), child: Image.network(
), _appInfo?["iconUrl"] ?? "",
const SizedBox(width: 15,), width: 120,
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
_appInfo?["appName"],
//overflow: TextOverflow.ellipsis,
style: const TextStyle(
fontSize: 21,
fontWeight: FontWeight.bold,
), ),
), ),
const SizedBox(height: 10,), const SizedBox(
Text( width: 15,
_appInfo?["companyName"],
overflow: TextOverflow.ellipsis,
softWrap: false,
), ),
], Expanded(
) child: Column(
), crossAxisAlignment: CrossAxisAlignment.start,
], children: [
), Text(
const SizedBox(height: 20,), _appInfo?["appName"] ?? "null",
FilledButton.icon( //overflow: TextOverflow.ellipsis,
style: FilledButton.styleFrom( style: const TextStyle(
minimumSize: const Size.fromHeight(40), fontSize: 21,
), fontWeight: FontWeight.bold,
onPressed: () async { ),
launchUrl(await getAppLink(_appInfo?["appId"], context)); ),
}, const SizedBox(
label: const Text("Скачать APK"), height: 10,
), ),
const SizedBox(height: 15), Text(
SizedBox( _appInfo?["companyName"] ?? "null",
height: 84, overflow: TextOverflow.ellipsis,
child: ListView( softWrap: false,
scrollDirection: Axis.horizontal, ),
children: [ ],
_InfoCard( )),
topText: "${(_appInfo?["fileSize"]/1024/1024).toStringAsFixed(1)} MB", ],
bottomText: "Размер", ),
), const SizedBox(
_InfoCard( height: 20,
topText: _appInfo?["versionName"], ),
bottomText: "Версия", FilledButton.icon(
), style: FilledButton.styleFrom(
_InfoCard( minimumSize: const Size.fromHeight(40),
topText: _appInfo?["minSdkVersion"].toString() ?? "", ),
bottomText: "minSdkVersion", onPressed: () async {
), launchUrl(await getAppLink(_appInfo?["appId"], context));
_InfoCard( },
topText: _appInfo?["ageRestriction"]["category"] ?? "?", label: const Text("Скачать APK"),
bottomText: "Возраст", ),
), const SizedBox(height: 15),
_InfoCard( SizedBox(
topText: _appInfo?["categories"][0] ?? "?", height: 84,
bottomText: "Категория", child: ListView(
) scrollDirection: Axis.horizontal,
], children: [
), _InfoCard(
), topText:
const SizedBox(height: 15), "${((_appInfo?["fileSize"] ?? 0) / 1024 / 1024).toStringAsFixed(1)} MB",
SizedBox( bottomText: "Размер",
height: 250, ),
child: ListView.builder( _InfoCard(
scrollDirection: Axis.horizontal, topText: _appInfo?["versionName"] ?? "0",
itemCount: _appInfo?["fileUrls"].length, bottomText: "Версия",
itemBuilder: (context, i) { ),
var file = _appInfo?["fileUrls"][i]; _InfoCard(
if (file["type"] != "SCREENSHOT" && file["orientation"] != "PORTRAIT") { topText: _appInfo?["minSdkVersion"].toString() ?? "",
return const SizedBox(); bottomText: "minSdkVersion",
} ),
//return Text("data${i}"); _InfoCard(
return Padding( topText:
padding: const EdgeInsets.all(5), _appInfo?["ageRestriction"]?["category"] ?? "?",
child: ClipRRect( bottomText: "Возраст",
borderRadius: BorderRadius.circular(15), ),
child: Image.network( _InfoCard(
file["fileUrl"], topText: _appInfo?["categories"]?[0] ?? "?",
bottomText: "Категория",
)
],
), ),
), ),
); const SizedBox(height: 15),
} SizedBox(
), height: 250,
), child: ListView.builder(
const SizedBox(height: 15), scrollDirection: Axis.horizontal,
const Text("Что нового?", style: TextStyle(fontSize: 21, fontWeight: FontWeight.bold),), itemCount: _appInfo?["fileUrls"]?.length ?? 0,
Text(_appInfo?["whatsNew"]), itemBuilder: (context, i) {
const SizedBox(height: 15), var file = _appInfo?["fileUrls"][i];
const Text("Описание", style: TextStyle(fontSize: 21, fontWeight: FontWeight.bold),), if (file["type"] != "SCREENSHOT" &&
Text(_appInfo?["fullDescription"]), file["orientation"] != "PORTRAIT") {
], return const SizedBox();
) : const LinearProgressIndicator(); }
//return Text("data${i}");
return Padding(
padding: const EdgeInsets.all(5),
child: ClipRRect(
borderRadius: BorderRadius.circular(15),
child: Image.network(
file["fileUrl"],
),
),
);
}),
),
const SizedBox(height: 15),
const Text(
"Что нового?",
style: TextStyle(fontSize: 21, fontWeight: FontWeight.bold),
),
Text(_appInfo?["whatsNew"] ?? "null"),
const SizedBox(height: 15),
const Text(
"Описание",
style: TextStyle(fontSize: 21, fontWeight: FontWeight.bold),
),
Text(_appInfo?["fullDescription"] ?? "null"),
],
)
: const LinearProgressIndicator());
} }
} }
@ -183,5 +200,4 @@ class _InfoCard extends StatelessWidget {
), ),
); );
} }
}
}

View file

@ -1,17 +1,44 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:openstore/app_page.dart';
import 'package:openstore/search_page.dart'; import 'package:openstore/search_page.dart';
import 'package:go_router/go_router.dart';
void main() { void main() {
runApp(const MyApp()); runApp(const MyApp());
} }
final _router = GoRouter(
routes: [
GoRoute(path: '/', builder: (context, state) => const HomePage(), routes: [
GoRoute(
path: '/app/:id',
builder: (context, state) =>
AppPage(packageName: state.pathParameters['id'] ?? ""),
),
GoRoute(
path: 'instruction',
builder: (context, state) {
print(state.uri.queryParameters["utm_campaign"]);
return AppPage(packageName: "ru.nspk.mirpay");
})
]),
GoRoute(
path: '/search',
builder: (context, state) =>
SearchPage(search: state.uri.queryParameters["q"] ?? "")),
GoRoute(
path: '/catalog/app/:id',
redirect: (_, state) => "/app/${state.pathParameters["id"]}"),
],
);
class MyApp extends StatelessWidget { class MyApp extends StatelessWidget {
const MyApp({super.key}); const MyApp({super.key});
// This widget is the root of your application. // This widget is the root of your application.
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp.router(
title: 'OpenStore', title: 'OpenStore',
theme: ThemeData( theme: ThemeData(
colorScheme: ColorScheme.fromSeed( colorScheme: ColorScheme.fromSeed(
@ -20,7 +47,7 @@ class MyApp extends StatelessWidget {
), ),
useMaterial3: true, useMaterial3: true,
), ),
home: const HomePage(), routerConfig: _router,
); );
} }
} }
@ -37,38 +64,28 @@ class HomePage extends StatelessWidget {
body: Center( body: Center(
child: Row( child: Row(
children: [ children: [
const SizedBox(width: 15,), const SizedBox(
width: 15,
),
Flexible( Flexible(
child: TextField( child: TextField(
onSubmitted: (value) { onSubmitted: (value) => context.push(
Navigator.of(context).push( Uri(path: '/search', queryParameters: {'q': value})
MaterialPageRoute( .toString()),
builder: (context) {
return Scaffold(
appBar: AppBar(
title: Text("Поиск: "+value),
),
body: SearchPage(search: value,),
);
}
)
);
},
textInputAction: TextInputAction.search, textInputAction: TextInputAction.search,
decoration: InputDecoration( decoration: InputDecoration(
prefixIcon: Icon(Icons.search), prefixIcon: Icon(Icons.search),
border: OutlineInputBorder() border: OutlineInputBorder()),
),
), ),
), ),
//const SizedBox(width: 5,), //const SizedBox(width: 5,),
//FilledButton(onPressed: (){}, child: Text("GO")), //FilledButton(onPressed: (){}, child: Text("GO")),
const SizedBox(width: 15,), const SizedBox(
width: 15,
),
], ],
), ),
), ),
); );
} }
}
}

View file

@ -2,6 +2,7 @@ import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:go_router/go_router.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:openstore/app_page.dart'; import 'package:openstore/app_page.dart';
import 'package:openstore/get_app_link.dart'; import 'package:openstore/get_app_link.dart';
@ -21,107 +22,96 @@ class _SearchPageState extends State<SearchPage> {
super.initState(); super.initState();
_loadResults(); _loadResults();
} }
void _loadResults() async { void _loadResults() async {
var rq = await http.get( var rq = await http
Uri.https( .get(Uri.https("backapi.rustore.ru", "/applicationData/apps", {
"backapi.rustore.ru", "pageSize": "20",
"/applicationData/apps", "query": widget.search,
{ }));
"pageSize": "20",
"query": widget.search,
}
)
);
if (rq.statusCode != 200) { if (rq.statusCode != 200) {
showDialog( showDialog(
context: context, context: context,
builder: (context) => AlertDialog( builder: (context) => AlertDialog(
title: const Text("Что то пошло не так"), title: const Text("Что то пошло не так"),
content: Text("RuStore вернул код ${rq.statusCode}"), content: Text("RuStore вернул код ${rq.statusCode}"),
actions: [ actions: [
FilledButton( FilledButton(
onPressed: () { onPressed: () {
Navigator.of(context) Navigator.of(context)
..pop() ..pop()
..pop(); ..pop();
}, },
child: const Text("ок"), child: const Text("ок"),
) )
], ],
) ));
);
} }
setState(() { setState(() {
_searchResults = json.decode(utf8.decode(rq.bodyBytes))["body"]["content"]; _searchResults =
json.decode(utf8.decode(rq.bodyBytes))["body"]["content"];
}); });
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (_searchResults == null) { return Scaffold(
return const LinearProgressIndicator(); appBar: AppBar(title: Text("Поиск")),
} body: _searchResults == null
return ListView.builder( ? LinearProgressIndicator()
itemCount: _searchResults?.length, : ListView.builder(
padding: const EdgeInsets.fromLTRB(15, 0, 15, 0), itemCount: _searchResults?.length,
itemBuilder: (context, i) { padding: const EdgeInsets.fromLTRB(15, 0, 15, 0),
var appInfo = _searchResults?[i]; itemBuilder: (context, i) {
return Card( var appInfo = _searchResults?[i];
clipBehavior: Clip.hardEdge, return Card(
child: InkWell( clipBehavior: Clip.hardEdge,
onTap: () async { child: InkWell(
Navigator.of(context).push( onTap: () =>
MaterialPageRoute(builder: (ctx) => context.push("/app/${appInfo["packageName"]}"),
Scaffold( child: Padding(
appBar: AppBar(), padding: const EdgeInsets.all(15),
body: AppPage(packageName: appInfo["packageName"]), child: Row(
) children: [
) ClipRRect(
); borderRadius: BorderRadius.circular(15),
}, child: Image.network(
child: Padding( width: 60, appInfo["iconUrl"]),
padding: const EdgeInsets.all(15), ),
child: Row( const SizedBox(
children: [ width: 15,
ClipRRect( ),
borderRadius: BorderRadius.circular(15), Expanded(
child: Image.network( child: Column(
width: 60, crossAxisAlignment: CrossAxisAlignment.start,
appInfo["iconUrl"] children: [
), Text(
), appInfo["appName"],
const SizedBox(width: 15,), style: const TextStyle(
Expanded( fontWeight: FontWeight.bold,
child: Column( ),
crossAxisAlignment: CrossAxisAlignment.start, overflow: TextOverflow.ellipsis,
children: [ ),
Text( Text(
appInfo["appName"], appInfo["packageName"],
style: const TextStyle( overflow: TextOverflow.ellipsis,
fontWeight: FontWeight.bold, )
],
),
),
IconButton.filledTonal(
onPressed: () async {
Clipboard.setData(ClipboardData(
text: (await getAppLink(
appInfo?["appId"], context))
.toString()));
},
icon: const Icon(Icons.link),
),
],
), ),
overflow: TextOverflow.ellipsis,
), ),
Text( ));
appInfo["packageName"], }));
overflow: TextOverflow.ellipsis,
)
],
),
),
IconButton.filledTonal(
onPressed: ()async{
Clipboard.setData(
ClipboardData(text: (await getAppLink(appInfo?["appId"], context)).toString())
);
},
icon: const Icon(Icons.link),
),
],
),
),
)
);
}
);
} }
} }

View file

@ -80,6 +80,14 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
go_router:
dependency: "direct main"
description:
name: go_router
sha256: "2b9ba6d4c247457c35a6622f1dee6aab6694a4e15237ff7c32320345044112b6"
url: "https://pub.dev"
source: hosted
version: "15.1.1"
http: http:
dependency: "direct main" dependency: "direct main"
description: description:

View file

@ -36,6 +36,7 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.6 cupertino_icons: ^1.0.6
http: ^1.2.1 http: ^1.2.1
go_router: ^15.1.1
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: