如何将聊天添加到应用程序?

时间:2020-04-11 12:57:10

标签: flutter dart

我试图将此聊天代码从教程添加到我的应用程序,然后对其进行自定义。我希望使用BottomNavigationBar的图标打开聊天,但无法弄清楚。 这是聊天室:

这就是我尝试使用它的方式:

int tabIndex = 0;
  List<Widget> widgetOptions = <Widget>[
    Items(),
    StartTheChat(),
    Profil(),
  ];

这是聊天室:

class StartTheChat extends StatelessWidget {
  void main() async {
    final client = Client(
      'b67pax5b2wdq',
      logLevel: Level.INFO,
    );

    await client.setUser(
      User(id: 'falling-mountain-7'),
      'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiZmFsbGluZy1tb3VudGFpbi03In0.AKgRXHMQQMz6vJAKszXdY8zMFfsAgkoUeZHlI-Szz9E',
    );
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Container(
        child: StreamChat(
          client: null,
          child: Chat(Client(
      'b67pax5b2wdq',
      logLevel: Level.INFO,
    ),
),
        ),
      ),
    );
  }
}

class Chat extends StatelessWidget {
  final Client client;

  Chat(this.client);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Container(
        child: StreamChat(
          client: client,
          child: ChannelListPage(),
        ),
      ),
    );
  }
}

class ChannelListPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ChannelListView(
        filter: {
          'members': {
            '\$in': [StreamChat.of(context).user.id],
          }
        },
        sort: [SortOption('last_message_at')],
        pagination: PaginationParams(
          limit: 20,
        ),
        channelWidget: ChannelPage(),
      ),
    );
  }
}

class ChannelPage extends StatelessWidget {
  const ChannelPage({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: ChannelHeader(),
      body: Column(
        children: <Widget>[
          Expanded(
            child: MessageListView(
              messageBuilder: _messageBuilder,
            ),
          ),
          MessageInput(),
        ],
      ),
    );
  }

  Widget _messageBuilder(context, message, index) {
    final isCurrentUser = StreamChat.of(context).user.id == message.user.id;
    final textAlign = isCurrentUser ? TextAlign.right : TextAlign.left;
    final color = isCurrentUser ? Colors.blueGrey : Colors.blue;

    return Padding(
      padding: EdgeInsets.all(20.0),
      child: Container(
        decoration: BoxDecoration(
          color: Colors.blue,
          //border: Border.all(color: color, width: 1),
          borderRadius: BorderRadius.all(
            Radius.circular(45.0),
          ),
        ),
        child: ListTile(
          title: Text(
            message.text,
            textAlign: textAlign,
          ),
          subtitle: Text(
            message.user.extraData['name'],
            textAlign: textAlign,
          ),
        ),
      ),
    );
  }
}

我得到的错误:

`Compiler message:
../../flutter/.pub-cache/hosted/pub.dartlang.org/stream_chat_flutter-0.1.22/lib/src/message_input.dart:646:24: Error: The getter 'MediaType' isn't defined for the class '_MessageInputState'.
 - '_MessageInputState' is from 'package:stream_chat_flutter/src/message_input.dart' ('../../flutter/.pub-cache/hosted/pub.dartlang.org/stream_chat_flutter-0.1.22/lib/src/message_input.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'MediaType'.
          contentType: MediaType.parse(lookupMimeType(filename)),
                       ^^^^^^^^^
../../flutter/.pub-cache/hosted/pub.dartlang.org/stream_chat_flutter-0.1.22/lib/src/message_input.dart:655:24: Error: The getter 'MediaType' isn't defined for the class '_MessageInputState'.
 - '_MessageInputState' is from 'package:stream_chat_flutter/src/message_input.dart' ('../../flutter/.pub-cache/hosted/pub.dartlang.org/stream_chat_flutter-0.1.22/lib/src/message_input.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'MediaType'.
          contentType: MediaType.parse(lookupMimeType(filename)),
                       ^^^^^^^^^
../../flutter/.pub-cache/hosted/pub.dartlang.org/stream_chat_flutter-0.1.22/lib/src/message_widget.dart:999:24: Error: The method '_buildErrorImage' isn't defined for the class '_MessageWidgetState'.
 - '_MessageWidgetState' is from 'package:stream_chat_flutter/src/message_widget.dart' ('../../flutter/.pub-cache/hosted/pub.dartlang.org/stream_chat_flutter-0.1.22/lib/src/message_widget.dart').
Try correcting the name to the name of an existing method, or defining a method named '_buildErrorImage'.
                return _buildErrorImage(attachment);
                       ^^^^^^^^^^^^^^^^
Target kernel_snapshot failed: Exception: Errors during snapshot creation: null
build failed.`

失败:构建失败,并出现异常。

  • 其中: 脚本'/home/c4t/flutter/packages/flutter_tools/gradle/flutter.gradle'行:780

  • 出了什么问题: 任务':app:compileFlutterBuildDebug'的执行失败。

    处理'命令'/ home / c4t / flutter / bin / flutter'完成,退出值非零1

  • 尝试: 使用--stacktrace选项运行以获取堆栈跟踪。使用--info或--debug选项运行,以获取更多日志输出。使用--scan运行以获取完整的见解。

  • https://help.gradle.org

  • 获得更多帮助

14秒内失败 Gradle任务assembleDebug失败,退出代码为1 退出(sigterm)

0 个答案:

没有答案