send page qr code
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
@@ -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),
|
||||
);
|
||||
}
|
||||
|
||||
|
@@ -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',
|
||||
|
@@ -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,
|
||||
);
|
||||
|
@@ -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'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
143
flutter/lib/pages/send/root.dart
Normal file
143
flutter/lib/pages/send/root.dart
Normal 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');
|
||||
}
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user