twilio_chat_conversation 0.0.1 twilio_chat_conversation: ^0.0.1 copied to clipboard
A flutter plugin to initiate Twilio Conversations.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:provider/provider.dart';
import 'package:twilio_chat_conversation/twilio_chat_conversation.dart';
import 'package:twilio_chat_conversation_example/chat/bloc/chat_bloc.dart';
import 'package:twilio_chat_conversation_example/chat/common/providers/chats_provider.dart';
import 'package:twilio_chat_conversation_example/chat/common/providers/models_provider.dart';
import 'package:twilio_chat_conversation_example/chat/repository/chat_repository.dart';
import 'package:twilio_chat_conversation_example/chat/screens/home_screen.dart';
void main() {
runApp(const MyHomePage(title: 'Twilio Plugin',));
}
// class MyApp extends StatefulWidget {
// const MyApp({super.key});
//
// @override
// State<MyApp> createState() => _MyAppState();
// }
//
// class _MyAppState extends State<MyApp> {
// // String _platformVersion = 'Unknown';
// // final _twilioChatConversationPlugin = TwilioChatConversation();
//
// @override
// void initState() {
// super.initState();
// // initPlatformState();
// }
//
// // Platform messages are asynchronous, so we initialize in an async method.
// // Future<void> initPlatformState() async {
// // String platformVersion;
// // // Platform messages may fail, so we use a try/catch PlatformException.
// // // We also handle the message potentially returning null.
// // try {
// // platformVersion =
// // await _twilioChatConversationPlugin.getPlatformVersion() ?? 'Unknown platform version';
// // } on PlatformException {
// // platformVersion = 'Failed to get platform version.';
// // }
// //
// // // If the widget was removed from the tree while the asynchronous platform
// // // message was in flight, we want to discard the reply rather than calling
// // // setState to update our non-existent appearance.
// // if (!mounted) return;
// //
// // setState(() {
// // _platformVersion = platformVersion;
// // });
// // }
//
// @override
// Widget build(BuildContext context) {
// return MaterialApp(
// home: Scaffold(
// appBar: AppBar(
// title: const Text('Plugin example app'),
// ),
// body: Center(
// child: Text('Running on: $_platformVersion\n'),
// ),
// ),
// );
// }
// }
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String platformVersion = 'Unknown';
final _twilioChatConversationPlugin = TwilioChatConversation();
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion =
await _twilioChatConversationPlugin.getPlatformVersion() ?? 'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
this.platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
return MultiProvider(
providers: [
ChangeNotifierProvider(
create: (_) => ModelsProvider(),
),
ChangeNotifierProvider(
create: (_) => ChatProvider(),
),
],
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Running on: $platformVersion\n',
home: BlocProvider(
create: (context) => ChatBloc(
chatRepository: ChatRepositoryImpl(),
),
child: HomeScreen(platformVersion: platformVersion,),
),
),
);
}
}