WIP example

This commit is contained in:
Neevash Ramdial
2021-01-27 06:35:30 -04:00
parent 6983b05a72
commit fa6e6aff17
2 changed files with 222 additions and 124 deletions
@@ -1,121 +1,249 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:stream_chat_flutter_core/stream_chat_flutter_core.dart';
void main() { Future<void> main() async {
runApp(MyApp()); /// Create a new instance of [Client] passing the apikey obtained from your
/// project dashboard.
final client = Client('b67pax5b2wdq');
/// Set the current user. In a production scenario, this should be done using
/// a backend to generate a user token using our server SDK.
/// Please see the following for more information:
/// https://getstream.io/chat/docs/ios_user_setup_and_tokens/
await client.setUser(
User(
id: 'cool-shadow-7',
extraData: {
'image':
'https://getstream.io/random_png/?id=cool-shadow-7&amp;name=Cool+shadow',
},
),
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiY29vbC1zaGFkb3ctNyJ9.gkOlCRb1qgy4joHPaxFwPOdXcGvSPvp6QY0S4mpRkVo',
);
runApp(
StreamExample(
client: client,
),
);
} }
// ignore: public_member_api_docs class StreamExample extends StatelessWidget {
class MyApp extends StatelessWidget { const StreamExample({
// This widget is the root of your application. Key key,
@required this.client,
}) : super(key: key);
final Client client;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
title: 'Flutter Demo', title: 'Stream Chat Dart Example',
theme: ThemeData( home: _HomeScreen(),
// This is the theme of your application. builder: (context, child) => StreamChatCore(
// client: client,
// Try running your application with "flutter run". You'll see the child: ChannelsBloc(child: child),
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
// This makes the visual density adapt to the platform that you run
// the app on. For desktop platforms, the controls will be smaller and
// closer together (more dense) than on mobile platforms.
visualDensity: VisualDensity.adaptivePlatformDensity,
), ),
home: MyHomePage(title: 'Flutter Demo Home Page'),
); );
} }
} }
// ignore: public_member_api_docs class _HomeScreen extends StatelessWidget {
class MyHomePage extends StatefulWidget {
// ignore: public_member_api_docs
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
// ignore: public_member_api_docs
final String title;
@override @override
_MyHomePageState createState() => _MyHomePageState(); Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Channels'),
),
body: ChannelListCore(
emptyBuilder: (BuildContext context) {
return Center(
child: Text('Looks like you are not in any channels'),
);
},
loadingBuilder: (BuildContext context) {
return Center(
child: SizedBox(
height: 100.0,
width: 100.0,
child: CircularProgressIndicator(),
),
);
},
errorBuilder: (Error error) {
return Center(
child:
Text('Oh no, something went wrong. Please check your config.'),
);
},
listBuilder: (BuildContext context, List<Channel> channels) =>
ListView.builder(
itemCount: channels.length,
itemBuilder: (BuildContext context, int index) {
final item = channels[index];
return ListTile(
title: Text(item.name),
subtitle: Text(item.state.lastMessage.text),
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => StreamChannel(
channel: item,
child: MessageScreen(),
),
),
);
},
);
},
),
),
);
}
} }
class _MyHomePageState extends State<MyHomePage> { class MessageScreen extends StatefulWidget {
int _counter = 0; @override
_MessageScreenState createState() => _MessageScreenState();
}
void _incrementCounter() { class _MessageScreenState extends State<MessageScreen> {
setState(() { TextEditingController _controller;
// This call to setState tells the Flutter framework that something has ScrollController _scrollController;
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed @override
// _counter without calling setState(), then the build method would not be void initState() {
// called again, and so nothing would appear to happen. super.initState();
_counter++; _controller = TextEditingController();
}); _scrollController = ScrollController();
}
@override
void dispose() {
_controller.dispose();
_scrollController.dispose();
super.dispose();
}
void _updateList() {
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
duration: const Duration(milliseconds: 200),
curve: Curves.easeOut,
);
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold( return Scaffold(
appBar: AppBar( body: Column(
// Here we take the value from the MyHomePage object that was created by children: [
// the App.build method, and use it to set our appbar title. Expanded(
title: Text(widget.title), child: MessageListCore(
), emptyBuilder: (BuildContext context) {
body: Center( return Center(
// Center is a layout widget. It takes a single child and positions it child: Text('Looks like you are not in any channels'),
// in the middle of the parent. );
child: Column( },
// Column is also a layout widget. It takes a list of children and loadingBuilder: (BuildContext context) {
// arranges them vertically. By default, it sizes itself to fit its return Center(
// children horizontally, and tries to be as tall as its parent. child: SizedBox(
// height: 100.0,
// Invoke "debug painting" (press "p" in the console, choose the width: 100.0,
// "Toggle Debug Paint" action from the Flutter Inspector in Android child: CircularProgressIndicator(),
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code) ),
// to see the wireframe for each widget. );
// },
// Column has various properties to control how it sizes itself and messageListBuilder:
// how it positions its children. Here we use mainAxisAlignment to (BuildContext context, List<Message> messages) {
// center the children vertically; the main axis here is the vertical return ListView.builder(
// axis because Columns are vertical (the cross axis would be controller: _scrollController,
// horizontal). itemCount: messages.length,
mainAxisAlignment: MainAxisAlignment.center, reverse: true,
children: <Widget>[ itemBuilder: (BuildContext context, int index) {
Text( final item = messages[index];
'You have pushed the button this many times:', final client = StreamChatCore.of(context).client;
if (item.user.id == client.uid) {
return Align(
alignment: Alignment.centerRight,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(item.text),
),
);
} else {
return Align(
alignment: Alignment.centerLeft,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(item.text),
),
);
}
},
);
},
), ),
Text( ),
'$_counter', Padding(
style: Theme.of(context).textTheme.headline4, padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Expanded(
child: TextField(
controller: _controller,
decoration: const InputDecoration(
hintText: 'Enter your message',
),
),
),
Material(
type: MaterialType.circle,
color: Colors.blue,
clipBehavior: Clip.hardEdge,
child: InkWell(
onTap: () async {
if (_controller.value.text.isNotEmpty) {
//TODO(Nash): Send Message
// await widget.channel.sendMessage(
// Message(text: _controller.value.text),
// );
_controller.clear();
_updateList();
}
},
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Center(
child: Icon(
Icons.send,
color: Colors.white,
),
),
),
),
)
],
), ),
], )
), ],
), ),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
); );
} }
} }
extension on Client {
String get uid => state.user.id;
}
extension on Channel {
String get name {
final _channelName = extraData['name'];
if (_channelName != null) {
return _channelName;
} else {
return cid;
}
}
}
@@ -1,30 +0,0 @@
// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:example/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}