send page qr code

This commit is contained in:
Mike Schwörer 2024-02-18 16:23:10 +01:00
parent 463e7ee287
commit 1286a5d848
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
15 changed files with 300 additions and 39 deletions

View File

@ -1,4 +1,7 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:simplecloudnotifier/models/user.dart';
class APIClient {
static const String _base = 'https://simplecloudnotifier.de/api/v2';
@ -9,4 +12,15 @@ class APIClient {
return (response.statusCode == 200);
}
static Future<User> getUser(String uid, String tok) async {
final uri = Uri.parse('$_base/users/$uid');
final response = await http.get(uri, headers: {'Authorization': 'SCN $tok'});
if (response.statusCode != 200) {
throw Exception('API request failed');
}
return User.fromJson(jsonDecode(response.body));
}
}

View File

@ -6,7 +6,7 @@ class FABBottomAppBarItem {
String text;
}
class FABBottomAppBar extends StatefulWidget {
class FABBottomAppBar extends StatelessWidget {
FABBottomAppBar({
super.key,
required this.items,
@ -18,6 +18,7 @@ class FABBottomAppBar extends StatefulWidget {
this.selectedColor,
this.notchedShape,
this.onTabSelected,
this.selectedIndex = 0,
}) {
assert(items.length == 2 || items.length == 4);
}
@ -31,26 +32,13 @@ class FABBottomAppBar extends StatefulWidget {
final Color? selectedColor;
final NotchedShape? notchedShape;
final ValueChanged<int>? onTabSelected;
@override
State<StatefulWidget> createState() => FABBottomAppBarState();
}
class FABBottomAppBarState extends State<FABBottomAppBar> {
int _selectedIndex = 0;
_updateIndex(int index) {
if (widget.onTabSelected != null) widget.onTabSelected!(index);
setState(() {
_selectedIndex = index;
});
}
final int selectedIndex;
@override
Widget build(BuildContext context) {
List<Widget> items = List.generate(widget.items.length, (int index) {
List<Widget> items = List.generate(this.items.length, (int index) {
return _buildTabItem(
item: widget.items[index],
item: this.items[index],
index: index,
onPressed: _updateIndex,
);
@ -58,8 +46,8 @@ class FABBottomAppBarState extends State<FABBottomAppBar> {
items.insert(items.length >> 1, _buildMiddleTabItem());
return BottomAppBar(
shape: widget.notchedShape,
color: widget.backgroundColor,
shape: notchedShape,
color: backgroundColor,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceAround,
@ -71,15 +59,15 @@ class FABBottomAppBarState extends State<FABBottomAppBar> {
Widget _buildMiddleTabItem() {
return Expanded(
child: SizedBox(
height: widget.height,
height: height,
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(height: widget.iconSize),
SizedBox(height: iconSize),
Text(
widget.centerItemText ?? '',
style: TextStyle(color: widget.color),
centerItemText ?? '',
style: TextStyle(color: color),
),
],
),
@ -88,10 +76,10 @@ class FABBottomAppBarState extends State<FABBottomAppBar> {
}
Widget _buildTabItem({required FABBottomAppBarItem item, required int index, required ValueChanged<int> onPressed}) {
Color color = (_selectedIndex == index ? widget.selectedColor : widget.color) ?? Colors.black;
Color color = (selectedIndex == index ? selectedColor : this.color) ?? Colors.black;
return Expanded(
child: SizedBox(
height: widget.height,
height: height,
child: Material(
type: MaterialType.transparency,
child: InkWell(
@ -100,7 +88,7 @@ class FABBottomAppBarState extends State<FABBottomAppBar> {
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(item.iconData, color: color, size: widget.iconSize),
Icon(item.iconData, color: color, size: iconSize),
Text(
item.text,
style: TextStyle(color: color),
@ -112,4 +100,8 @@ class FABBottomAppBarState extends State<FABBottomAppBar> {
),
);
}
void _updateIndex(int index) {
if (onTabSelected != null) onTabSelected!(index);
}
}

View File

@ -3,7 +3,7 @@ import 'package:flutter/material.dart';
// https://stackoverflow.com/questions/46480221/flutter-floating-action-button-with-speed-dail
class FabWithIcons extends StatefulWidget {
FabWithIcons({required this.icons, required this.onIconTapped});
FabWithIcons({super.key, required this.icons, required this.onIconTapped});
final List<IconData> icons;
ValueChanged<int> onIconTapped;
@override
@ -68,8 +68,8 @@ class FabWithIconsState extends State<FabWithIcons> with TickerProviderStateMixi
}
},
tooltip: 'Increment',
child: Icon(Icons.add),
elevation: 2.0,
child: const Icon(Icons.add),
);
}

View File

@ -22,6 +22,8 @@ class SCNApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
Provider.of<UserAccount>(context); // ensure UserAccount is loaded
return Consumer<AppTheme>(
builder: (context, appTheme, child) => MaterialApp(
title: 'SimpleCloudNotifier',

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';
import 'package:simplecloudnotifier/pages/send/root.dart';
import 'bottom_fab/fab_bottom_app_bar.dart';
import 'pages/account/root.dart';
@ -15,13 +16,14 @@ class SCNNavLayout extends StatefulWidget {
}
class _SCNNavLayoutState extends State<SCNNavLayout> {
int _selectedIndex = 0;
int _selectedIndex = 0; // 4 == FAB
static const List<Widget> _subPages = <Widget>[
MessageListPage(title: 'Messages'),
MessageListPage(title: 'Page 2'),
AccountRootPage(),
MessageListPage(title: 'Page 4'),
SendRootPage(),
];
void _onItemTapped(int index) {
@ -30,9 +32,9 @@ class _SCNNavLayoutState extends State<SCNNavLayout> {
});
}
void _onFABTapped(int index) {
void _onFABTapped() {
setState(() {
//TODO
_selectedIndex = 4;
});
}
@ -49,7 +51,7 @@ class _SCNNavLayoutState extends State<SCNNavLayout> {
Widget _buildFAB(BuildContext context) {
return FloatingActionButton(
onPressed: () {},
onPressed: _onFABTapped,
tooltip: 'Increment',
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(17))),
elevation: 2.0,
@ -59,6 +61,7 @@ class _SCNNavLayoutState extends State<SCNNavLayout> {
Widget _buildNavBar(BuildContext context) {
return FABBottomAppBar(
selectedIndex: _selectedIndex,
onTabSelected: _onItemTapped,
color: Theme.of(context).disabledColor,
selectedColor: Theme.of(context).primaryColorDark,
@ -105,11 +108,6 @@ class _SCNNavLayoutState extends State<SCNNavLayout> {
tooltip: 'Search',
onPressed: () {},
),
IconButton(
icon: const Icon(FontAwesomeIcons.solidQrcode),
tooltip: 'Show Account QR Code',
onPressed: () {},
),
],
backgroundColor: Theme.of(context).secondaryHeaderColor,
);

View File

@ -81,7 +81,7 @@ class _AccountLoginPageState extends State<AccountLoginPage> {
if (verified) {
msgr.showSnackBar(
const SnackBar(
content: Text('Data ok'), //TODO toast
content: Text('Data ok'),
),
);
prov.setToken(KeyTokenAuth(userId: uid, token: tok));
@ -90,7 +90,7 @@ class _AccountLoginPageState extends State<AccountLoginPage> {
} else {
msgr.showSnackBar(
const SnackBar(
content: Text('Failed to verify token'), //TODO toast
content: Text('Failed to verify token'),
),
);
}

View File

@ -0,0 +1,143 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:url_launcher/url_launcher.dart';
import '../../state/user_account.dart';
class SendRootPage extends StatefulWidget {
const SendRootPage({super.key});
@override
State<SendRootPage> createState() => _SendRootPageState();
}
class _SendRootPageState extends State<SendRootPage> {
late TextEditingController _msgTitle;
late TextEditingController _msgContent;
@override
void initState() {
super.initState();
_msgTitle = TextEditingController();
_msgContent = TextEditingController();
}
@override
void dispose() {
_msgTitle.dispose();
_msgContent.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Consumer<UserAccount>(
builder: (context, acc, child) {
return Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
_buildQRCode(context, acc),
const SizedBox(height: 16),
FractionallySizedBox(
widthFactor: 1.0,
child: TextField(
controller: _msgTitle,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Title',
),
),
),
const SizedBox(height: 16),
FractionallySizedBox(
widthFactor: 1.0,
child: TextField(
controller: _msgContent,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Text',
),
),
),
const SizedBox(height: 16),
ElevatedButton(
style: ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20)),
onPressed: _send,
child: const Text('Send'),
),
],
),
);
},
);
}
void _send() {
//...
}
_buildQRCode(BuildContext context, UserAccount acc) {
if (acc.auth == null) {
return const Placeholder();
}
if (acc.user == null) {
return FutureBuilder(
future: acc.loadUser(false),
builder: ((context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
}
var url = 'https://simplecloudnotifier.com?preset_user_id=${acc.user!.userID}&preset_user_key=TODO'; // TODO get send-only key
return GestureDetector(
onTap: () {
_openWeb(url);
},
child: QrImageView(
data: url,
version: QrVersions.auto,
size: 400.0,
),
);
}
return const SizedBox(
width: 400.0,
height: 400.0,
child: Center(child: CircularProgressIndicator()),
);
}),
);
}
var url = 'https://simplecloudnotifier.com?preset_user_id=${acc.user!.userID}&preset_user_key=TODO'; // TODO get send-only key
return GestureDetector(
onTap: () {
_openWeb(url);
},
child: QrImageView(
data: url,
version: QrVersions.auto,
size: 400.0,
),
);
}
void _openWeb(String url) async {
try {
final Uri uri = Uri.parse(url);
if (await canLaunchUrl(uri)) {
await launchUrl(uri);
} else {
// TODO ("Cannot open URL");
}
} catch (e) {
// TODO ('Cannot open URL');
}
}
}

View File

@ -1,5 +1,6 @@
import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:simplecloudnotifier/api/api_client.dart';
import '../models/key_token_auth.dart';
import '../models/user.dart';
@ -60,4 +61,22 @@ class UserAccount extends ChangeNotifier {
await prefs.setString('auth.token', _auth!.token);
}
}
loadUser(bool force) async {
if (!force && _user != null) {
return _user;
}
if (_auth == null) {
throw Exception('Not authenticated');
}
final user = await APIClient.getUser(_auth!.userId, _auth!.token);
setUser(user);
await save();
return user;
}
}

View File

@ -6,6 +6,10 @@
#include "generated_plugin_registrant.h"
#include <url_launcher_linux/url_launcher_plugin.h>
void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) url_launcher_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "UrlLauncherPlugin");
url_launcher_plugin_register_with_registrar(url_launcher_linux_registrar);
}

View File

@ -3,6 +3,7 @@
#
list(APPEND FLUTTER_PLUGIN_LIST
url_launcher_linux
)
list(APPEND FLUTTER_FFI_PLUGIN_LIST

View File

@ -6,7 +6,9 @@ import FlutterMacOS
import Foundation
import shared_preferences_foundation
import url_launcher_macos
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
}

View File

@ -215,6 +215,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "6.1.1"
qr:
dependency: transitive
description:
name: qr
sha256: "64957a3930367bf97cc211a5af99551d630f2f4625e38af10edd6b19131b64b3"
url: "https://pub.dev"
source: hosted
version: "3.0.1"
qr_flutter:
dependency: "direct main"
description:
name: qr_flutter
sha256: "5095f0fc6e3f71d08adef8feccc8cea4f12eec18a2e31c2e8d82cb6019f4b097"
url: "https://pub.dev"
source: hosted
version: "4.1.0"
shared_preferences:
dependency: "direct main"
description:
@ -332,6 +348,70 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.2"
url_launcher:
dependency: "direct main"
description:
name: url_launcher
sha256: c512655380d241a337521703af62d2c122bf7b77a46ff7dd750092aa9433499c
url: "https://pub.dev"
source: hosted
version: "6.2.4"
url_launcher_android:
dependency: transitive
description:
name: url_launcher_android
sha256: d4ed0711849dd8e33eb2dd69c25db0d0d3fdc37e0a62e629fe32f57a22db2745
url: "https://pub.dev"
source: hosted
version: "6.3.0"
url_launcher_ios:
dependency: transitive
description:
name: url_launcher_ios
sha256: "75bb6fe3f60070407704282a2d295630cab232991eb52542b18347a8a941df03"
url: "https://pub.dev"
source: hosted
version: "6.2.4"
url_launcher_linux:
dependency: transitive
description:
name: url_launcher_linux
sha256: ab360eb661f8879369acac07b6bb3ff09d9471155357da8443fd5d3cf7363811
url: "https://pub.dev"
source: hosted
version: "3.1.1"
url_launcher_macos:
dependency: transitive
description:
name: url_launcher_macos
sha256: b7244901ea3cf489c5335bdacda07264a6e960b1c1b1a9f91e4bc371d9e68234
url: "https://pub.dev"
source: hosted
version: "3.1.0"
url_launcher_platform_interface:
dependency: transitive
description:
name: url_launcher_platform_interface
sha256: "552f8a1e663569be95a8190206a38187b531910283c3e982193e4f2733f01029"
url: "https://pub.dev"
source: hosted
version: "2.3.2"
url_launcher_web:
dependency: transitive
description:
name: url_launcher_web
sha256: fff0932192afeedf63cdd50ecbb1bc825d31aed259f02bb8dba0f3b729a5e88b
url: "https://pub.dev"
source: hosted
version: "2.2.3"
url_launcher_windows:
dependency: transitive
description:
name: url_launcher_windows
sha256: ecf9725510600aa2bb6d7ddabe16357691b6d2805f66216a97d1b881e21beff7
url: "https://pub.dev"
source: hosted
version: "3.1.1"
vector_math:
dependency: transitive
description:

View File

@ -39,6 +39,8 @@ dependencies:
http: ^1.2.0
provider: ^6.1.1
shared_preferences: ^2.2.2
qr_flutter: ^4.1.0
url_launcher: ^6.2.4
dependency_overrides:

View File

@ -6,6 +6,9 @@
#include "generated_plugin_registrant.h"
#include <url_launcher_windows/url_launcher_windows.h>
void RegisterPlugins(flutter::PluginRegistry* registry) {
UrlLauncherWindowsRegisterWithRegistrar(
registry->GetRegistrarForPlugin("UrlLauncherWindows"));
}

View File

@ -3,6 +3,7 @@
#
list(APPEND FLUTTER_PLUGIN_LIST
url_launcher_windows
)
list(APPEND FLUTTER_FFI_PLUGIN_LIST