create a new app and client for advanced options

This commit is contained in:
Salvatore Giordano
2020-11-10 16:05:51 +01:00
parent e8cb8fa49d
commit b1401a5f0c
3 changed files with 253 additions and 161 deletions
+233 -160
View File
@@ -1,3 +1,5 @@
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:stream_chat_flutter/stream_chat_flutter.dart';
@@ -5,13 +7,24 @@ import 'package:stream_chat_flutter/stream_chat_flutter.dart';
import 'main.dart';
import 'notifications_service.dart';
class AdvancedOptionsPage extends StatelessWidget {
class AdvancedOptionsPage extends StatefulWidget {
@override
_AdvancedOptionsPageState createState() => _AdvancedOptionsPageState();
}
class _AdvancedOptionsPageState extends State<AdvancedOptionsPage> {
final _formKey = GlobalKey<FormState>();
final TextEditingController _apiKeyController = TextEditingController();
final TextEditingController _userIdController = TextEditingController();
final TextEditingController _userTokenController = TextEditingController();
final TextEditingController _usernameController = TextEditingController();
bool loading = false;
@override
Widget build(BuildContext context) {
return Scaffold(
@@ -36,169 +49,229 @@ class AdvancedOptionsPage extends StatelessWidget {
},
),
),
body: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 16,
),
child: Form(
key: _formKey,
onChanged: () {},
child: Column(
children: [
TextFormField(
controller: _apiKeyController,
validator: (value) {
if (value.isEmpty) {
return 'Please enter the Chat API Key';
}
return null;
},
decoration: InputDecoration(
labelStyle: TextStyle(
fontSize: 14,
color: Colors.black.withOpacity(.5),
body: Builder(
builder: (context) {
return Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 16,
),
child: Form(
key: _formKey,
child: Column(
children: [
TextFormField(
controller: _apiKeyController,
validator: (value) {
if (value.isEmpty) {
return 'Please enter the Chat API Key';
}
return null;
},
decoration: InputDecoration(
labelStyle: TextStyle(
fontSize: 14,
color: Colors.black.withOpacity(.5),
),
border: UnderlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide.none,
),
fillColor: Color(0xffF5F5F5),
filled: true,
labelText: 'Chat API Key',
),
textInputAction: TextInputAction.next,
),
border: UnderlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide.none,
),
fillColor: Color(0xffF5F5F5),
filled: true,
labelText: 'Chat API Key',
),
textInputAction: TextInputAction.next,
),
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: TextFormField(
controller: _userIdController,
validator: (value) {
if (value.isEmpty) {
return 'Please enter the User ID';
}
return null;
},
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelStyle: TextStyle(
fontSize: 14,
color: Colors.black.withOpacity(.5),
),
border: UnderlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide.none,
),
fillColor: Color(0xffF5F5F5),
filled: true,
labelText: 'User ID',
),
),
),
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: TextFormField(
controller: _userTokenController,
validator: (value) {
if (value.isEmpty) {
return 'Please enter the user token';
}
return null;
},
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelStyle: TextStyle(
fontSize: 14,
color: Colors.black.withOpacity(.5),
),
border: UnderlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide.none,
),
fillColor: Color(0xffF5F5F5),
filled: true,
labelText: 'User Token',
),
),
),
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: TextFormField(
controller: _usernameController,
textInputAction: TextInputAction.done,
decoration: InputDecoration(
labelStyle: TextStyle(
fontSize: 14,
color: Colors.black.withOpacity(.5),
),
border: UnderlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide.none,
),
fillColor: Color(0xffF5F5F5),
filled: true,
labelText: 'Username (optional)',
),
),
),
Expanded(
child: Align(
alignment: Alignment.bottomCenter,
child: FlatButton(
color: StreamChatTheme.of(context).accentColor,
minWidth: double.infinity,
height: 48,
child: Text(
'Login',
style: TextStyle(
color: Colors.white,
fontSize: 16,
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: TextFormField(
controller: _userIdController,
validator: (value) {
if (value.isEmpty) {
return 'Please enter the User ID';
}
return null;
},
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelStyle: TextStyle(
fontSize: 14,
color: Colors.black.withOpacity(.5),
),
border: UnderlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide.none,
),
fillColor: Color(0xffF5F5F5),
filled: true,
labelText: 'User ID',
),
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(26),
),
onPressed: () async {
if (_formKey.currentState.validate()) {
final apiKey = _apiKeyController.text;
final userId = _userIdController.text;
final userToken = _userTokenController.text;
final username = _usernameController.text;
final client = StreamChat.of(context).client;
client.apiKey = apiKey;
await client.setUser(
User(id: userId, extraData: {
'name': username,
}),
userToken,
);
if (!kIsWeb) {
initNotifications(client);
}
Navigator.pop(context);
await Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) {
return StreamChat(
client: client,
child: ChannelListPage(),
);
},
),
);
}
},
),
),
)
],
),
),
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: TextFormField(
controller: _userTokenController,
validator: (value) {
if (value.isEmpty) {
return 'Please enter the user token';
}
return null;
},
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelStyle: TextStyle(
fontSize: 14,
color: Colors.black.withOpacity(.5),
),
border: UnderlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide.none,
),
fillColor: Color(0xffF5F5F5),
filled: true,
labelText: 'User Token',
),
),
),
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: TextFormField(
controller: _usernameController,
textInputAction: TextInputAction.done,
decoration: InputDecoration(
labelStyle: TextStyle(
fontSize: 14,
color: Colors.black.withOpacity(.5),
),
border: UnderlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide.none,
),
fillColor: Color(0xffF5F5F5),
filled: true,
labelText: 'Username (optional)',
),
),
),
Expanded(
child: Align(
alignment: Alignment.bottomCenter,
child: FlatButton(
color: StreamChatTheme.of(context).accentColor,
minWidth: double.infinity,
height: 48,
child: Text(
'Login',
style: TextStyle(
color: Colors.white,
fontSize: 16,
),
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(26),
),
onPressed: () async {
if (loading) {
return;
}
loading = true;
if (_formKey.currentState.validate()) {
final apiKey = _apiKeyController.text;
final userId = _userIdController.text;
final userToken = _userTokenController.text;
final username = _usernameController.text;
showDialog(
barrierDismissible: false,
context: context,
builder: (context) => Center(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
color: Colors.white,
),
height: 100,
width: 100,
child: Center(
child: CircularProgressIndicator(),
),
),
),
);
print('CREATE CLIENT');
final client = Client(
apiKey,
logLevel: Level.INFO,
showLocalNotification:
(!kIsWeb && Platform.isAndroid)
? showLocalNotification
: null,
persistenceEnabled: true,
);
try {
await client.setUser(
User(id: userId, extraData: {
'name': username,
}),
userToken,
);
} catch (e) {
var errorText = 'Error connecting, retry';
if (e is Map) {
errorText = e['message'] ?? errorText;
}
Navigator.pop(context);
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text(errorText),
),
);
loading = false;
await client.disconnect();
return;
}
if (!kIsWeb) {
initNotifications(client);
}
Navigator.pop(context);
Navigator.pop(context);
await Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) {
return MaterialApp(
theme: ThemeData.light(),
darkTheme: ThemeData.dark(),
//TODO change to system once dark theme is implemented
themeMode: ThemeMode.light,
builder: (context, widget) {
return StreamChat(
child: widget,
client: client,
);
},
home: ChannelListPage(),
);
},
),
);
loading = false;
}
},
),
),
)
],
),
),
);
},
),
);
}
+19
View File
@@ -98,6 +98,24 @@ class ChooseUserPage extends StatelessWidget {
final user = entry.value;
return ListTile(
onTap: () async {
showDialog(
barrierDismissible: false,
context: context,
builder: (context) => Center(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
color: Colors.white,
),
height: 100,
width: 100,
child: Center(
child: CircularProgressIndicator(),
),
),
),
);
final client = StreamChat.of(context).client;
await client.setUser(
@@ -111,6 +129,7 @@ class ChooseUserPage extends StatelessWidget {
initNotifications(client);
}
Navigator.pop(context);
await Navigator.pushReplacement(
context,
MaterialPageRoute(
+1 -1
View File
@@ -1,7 +1,7 @@
name: example
description: A new Flutter project.
version: 1.0.45+47
version: 1.0.46+48
environment:
sdk: ">=2.2.2 <3.0.0"