create a new app and client for advanced options
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:stream_chat_flutter/stream_chat_flutter.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 'main.dart';
|
||||||
import 'notifications_service.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 _formKey = GlobalKey<FormState>();
|
||||||
|
|
||||||
final TextEditingController _apiKeyController = TextEditingController();
|
final TextEditingController _apiKeyController = TextEditingController();
|
||||||
|
|
||||||
final TextEditingController _userIdController = TextEditingController();
|
final TextEditingController _userIdController = TextEditingController();
|
||||||
|
|
||||||
final TextEditingController _userTokenController = TextEditingController();
|
final TextEditingController _userTokenController = TextEditingController();
|
||||||
|
|
||||||
final TextEditingController _usernameController = TextEditingController();
|
final TextEditingController _usernameController = TextEditingController();
|
||||||
|
|
||||||
|
bool loading = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
@@ -36,169 +49,229 @@ class AdvancedOptionsPage extends StatelessWidget {
|
|||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
body: Padding(
|
body: Builder(
|
||||||
padding: const EdgeInsets.symmetric(
|
builder: (context) {
|
||||||
horizontal: 16,
|
return Padding(
|
||||||
vertical: 16,
|
padding: const EdgeInsets.symmetric(
|
||||||
),
|
horizontal: 16,
|
||||||
child: Form(
|
vertical: 16,
|
||||||
key: _formKey,
|
),
|
||||||
onChanged: () {},
|
child: Form(
|
||||||
child: Column(
|
key: _formKey,
|
||||||
children: [
|
child: Column(
|
||||||
TextFormField(
|
children: [
|
||||||
controller: _apiKeyController,
|
TextFormField(
|
||||||
validator: (value) {
|
controller: _apiKeyController,
|
||||||
if (value.isEmpty) {
|
validator: (value) {
|
||||||
return 'Please enter the Chat API Key';
|
if (value.isEmpty) {
|
||||||
}
|
return 'Please enter the Chat API Key';
|
||||||
return null;
|
}
|
||||||
},
|
return null;
|
||||||
decoration: InputDecoration(
|
},
|
||||||
labelStyle: TextStyle(
|
decoration: InputDecoration(
|
||||||
fontSize: 14,
|
labelStyle: TextStyle(
|
||||||
color: Colors.black.withOpacity(.5),
|
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(
|
Padding(
|
||||||
borderRadius: BorderRadius.circular(8),
|
padding: const EdgeInsets.only(top: 8.0),
|
||||||
borderSide: BorderSide.none,
|
child: TextFormField(
|
||||||
),
|
controller: _userIdController,
|
||||||
fillColor: Color(0xffF5F5F5),
|
validator: (value) {
|
||||||
filled: true,
|
if (value.isEmpty) {
|
||||||
labelText: 'Chat API Key',
|
return 'Please enter the User ID';
|
||||||
),
|
}
|
||||||
textInputAction: TextInputAction.next,
|
return null;
|
||||||
),
|
},
|
||||||
Padding(
|
textInputAction: TextInputAction.next,
|
||||||
padding: const EdgeInsets.only(top: 8.0),
|
decoration: InputDecoration(
|
||||||
child: TextFormField(
|
labelStyle: TextStyle(
|
||||||
controller: _userIdController,
|
fontSize: 14,
|
||||||
validator: (value) {
|
color: Colors.black.withOpacity(.5),
|
||||||
if (value.isEmpty) {
|
),
|
||||||
return 'Please enter the User ID';
|
border: UnderlineInputBorder(
|
||||||
}
|
borderRadius: BorderRadius.circular(8),
|
||||||
return null;
|
borderSide: BorderSide.none,
|
||||||
},
|
),
|
||||||
textInputAction: TextInputAction.next,
|
fillColor: Color(0xffF5F5F5),
|
||||||
decoration: InputDecoration(
|
filled: true,
|
||||||
labelStyle: TextStyle(
|
labelText: 'User ID',
|
||||||
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,
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
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;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,6 +98,24 @@ class ChooseUserPage extends StatelessWidget {
|
|||||||
final user = entry.value;
|
final user = entry.value;
|
||||||
return ListTile(
|
return ListTile(
|
||||||
onTap: () async {
|
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;
|
final client = StreamChat.of(context).client;
|
||||||
|
|
||||||
await client.setUser(
|
await client.setUser(
|
||||||
@@ -111,6 +129,7 @@ class ChooseUserPage extends StatelessWidget {
|
|||||||
initNotifications(client);
|
initNotifications(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Navigator.pop(context);
|
||||||
await Navigator.pushReplacement(
|
await Navigator.pushReplacement(
|
||||||
context,
|
context,
|
||||||
MaterialPageRoute(
|
MaterialPageRoute(
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
name: example
|
name: example
|
||||||
description: A new Flutter project.
|
description: A new Flutter project.
|
||||||
|
|
||||||
version: 1.0.45+47
|
version: 1.0.46+48
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: ">=2.2.2 <3.0.0"
|
sdk: ">=2.2.2 <3.0.0"
|
||||||
|
|||||||
Reference in New Issue
Block a user