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,13 +18,13 @@ 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,
@ -41,16 +41,19 @@ class _AppPageState extends State<AppPage> {
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(
appBar: AppBar(),
body: _appInfo != null
? ListView(
padding: const EdgeInsets.fromLTRB(15, 0, 15, 15), padding: const EdgeInsets.fromLTRB(15, 0, 15, 15),
children: [ children: [
Row( Row(
@ -59,35 +62,40 @@ class _AppPageState extends State<AppPage> {
ClipRRect( ClipRRect(
borderRadius: BorderRadius.circular(25), borderRadius: BorderRadius.circular(25),
child: Image.network( child: Image.network(
_appInfo?["iconUrl"], _appInfo?["iconUrl"] ?? "",
width: 120, width: 120,
), ),
), ),
const SizedBox(width: 15,), const SizedBox(
width: 15,
),
Expanded( Expanded(
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
Text( Text(
_appInfo?["appName"], _appInfo?["appName"] ?? "null",
//overflow: TextOverflow.ellipsis, //overflow: TextOverflow.ellipsis,
style: const TextStyle( style: const TextStyle(
fontSize: 21, fontSize: 21,
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
), ),
), ),
const SizedBox(height: 10,), const SizedBox(
height: 10,
),
Text( Text(
_appInfo?["companyName"], _appInfo?["companyName"] ?? "null",
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
softWrap: false, softWrap: false,
), ),
], ],
) )),
),
], ],
), ),
const SizedBox(height: 20,), const SizedBox(
height: 20,
),
FilledButton.icon( FilledButton.icon(
style: FilledButton.styleFrom( style: FilledButton.styleFrom(
minimumSize: const Size.fromHeight(40), minimumSize: const Size.fromHeight(40),
@ -104,11 +112,12 @@ class _AppPageState extends State<AppPage> {
scrollDirection: Axis.horizontal, scrollDirection: Axis.horizontal,
children: [ children: [
_InfoCard( _InfoCard(
topText: "${(_appInfo?["fileSize"]/1024/1024).toStringAsFixed(1)} MB", topText:
"${((_appInfo?["fileSize"] ?? 0) / 1024 / 1024).toStringAsFixed(1)} MB",
bottomText: "Размер", bottomText: "Размер",
), ),
_InfoCard( _InfoCard(
topText: _appInfo?["versionName"], topText: _appInfo?["versionName"] ?? "0",
bottomText: "Версия", bottomText: "Версия",
), ),
_InfoCard( _InfoCard(
@ -116,11 +125,12 @@ class _AppPageState extends State<AppPage> {
bottomText: "minSdkVersion", bottomText: "minSdkVersion",
), ),
_InfoCard( _InfoCard(
topText: _appInfo?["ageRestriction"]["category"] ?? "?", topText:
_appInfo?["ageRestriction"]?["category"] ?? "?",
bottomText: "Возраст", bottomText: "Возраст",
), ),
_InfoCard( _InfoCard(
topText: _appInfo?["categories"][0] ?? "?", topText: _appInfo?["categories"]?[0] ?? "?",
bottomText: "Категория", bottomText: "Категория",
) )
], ],
@ -131,10 +141,11 @@ class _AppPageState extends State<AppPage> {
height: 250, height: 250,
child: ListView.builder( child: ListView.builder(
scrollDirection: Axis.horizontal, scrollDirection: Axis.horizontal,
itemCount: _appInfo?["fileUrls"].length, itemCount: _appInfo?["fileUrls"]?.length ?? 0,
itemBuilder: (context, i) { itemBuilder: (context, i) {
var file = _appInfo?["fileUrls"][i]; var file = _appInfo?["fileUrls"][i];
if (file["type"] != "SCREENSHOT" && file["orientation"] != "PORTRAIT") { if (file["type"] != "SCREENSHOT" &&
file["orientation"] != "PORTRAIT") {
return const SizedBox(); return const SizedBox();
} }
//return Text("data${i}"); //return Text("data${i}");
@ -147,17 +158,23 @@ class _AppPageState extends State<AppPage> {
), ),
), ),
); );
} }),
),
), ),
const SizedBox(height: 15), const SizedBox(height: 15),
const Text("Что нового?", style: TextStyle(fontSize: 21, fontWeight: FontWeight.bold),), const Text(
Text(_appInfo?["whatsNew"]), "Что нового?",
style: TextStyle(fontSize: 21, fontWeight: FontWeight.bold),
),
Text(_appInfo?["whatsNew"] ?? "null"),
const SizedBox(height: 15), const SizedBox(height: 15),
const Text("Описание", style: TextStyle(fontSize: 21, fontWeight: FontWeight.bold),), const Text(
Text(_appInfo?["fullDescription"]), "Описание",
style: TextStyle(fontSize: 21, fontWeight: FontWeight.bold),
),
Text(_appInfo?["fullDescription"] ?? "null"),
], ],
) : const LinearProgressIndicator(); )
: 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,17 +22,13 @@ 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",
"/applicationData/apps",
{
"pageSize": "20", "pageSize": "20",
"query": widget.search, "query": widget.search,
} }));
)
);
if (rq.statusCode != 200) { if (rq.statusCode != 200) {
showDialog( showDialog(
context: context, context: context,
@ -48,19 +45,21 @@ class _SearchPageState extends State<SearchPage> {
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()
: ListView.builder(
itemCount: _searchResults?.length, itemCount: _searchResults?.length,
padding: const EdgeInsets.fromLTRB(15, 0, 15, 0), padding: const EdgeInsets.fromLTRB(15, 0, 15, 0),
itemBuilder: (context, i) { itemBuilder: (context, i) {
@ -68,16 +67,8 @@ class _SearchPageState extends State<SearchPage> {
return Card( return Card(
clipBehavior: Clip.hardEdge, clipBehavior: Clip.hardEdge,
child: InkWell( child: InkWell(
onTap: () async { onTap: () =>
Navigator.of(context).push( context.push("/app/${appInfo["packageName"]}"),
MaterialPageRoute(builder: (ctx) =>
Scaffold(
appBar: AppBar(),
body: AppPage(packageName: appInfo["packageName"]),
)
)
);
},
child: Padding( child: Padding(
padding: const EdgeInsets.all(15), padding: const EdgeInsets.all(15),
child: Row( child: Row(
@ -85,11 +76,11 @@ class _SearchPageState extends State<SearchPage> {
ClipRRect( ClipRRect(
borderRadius: BorderRadius.circular(15), borderRadius: BorderRadius.circular(15),
child: Image.network( child: Image.network(
width: 60, width: 60, appInfo["iconUrl"]),
appInfo["iconUrl"]
), ),
const SizedBox(
width: 15,
), ),
const SizedBox(width: 15,),
Expanded( Expanded(
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
@ -110,18 +101,17 @@ class _SearchPageState extends State<SearchPage> {
), ),
IconButton.filledTonal( IconButton.filledTonal(
onPressed: () async { onPressed: () async {
Clipboard.setData( Clipboard.setData(ClipboardData(
ClipboardData(text: (await getAppLink(appInfo?["appId"], context)).toString()) text: (await getAppLink(
); appInfo?["appId"], context))
.toString()));
}, },
icon: const Icon(Icons.link), 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: