编程知识 cdmana.com

Flutter integrated cloud SDK

flutter Integrated cloud sdk

Preface

1. Integrate flutter Melting clouds sdk , Need a stable flutter Environmental Science , Can create and run projects normally .
2. Prepare to apply for developer account on rongyun official website in the early stage
By managing the backstage " essential information "->"App Key" obtain AppKey
3. By managing the backstage "IM service "—>"API call "->" Customer service "->" obtain Token", Through users id obtain IMToken
4. I know you can't cheat people without pictures . Map first , Let's take a look at the final effect .
BsvRVU.jpg

Integrate sdk

  1. rely on IM Flutter plugin In the project pubspec.yaml The following dependency is written in .
dependencies:
  flutter:
    sdk: flutter

rongcloud_im_plugin: ^4.0.3
  1. Then execute it on the project path flutter packages get To download Flutter Plugin.
  2. We write 2 individual button and 2 individual text , They are used to realize init and connect Events and status of .

4. initialization SDK

RongIMClient.init(RongAppKey);

5. Connect IM

RongIMClient.connect(RongIMToken, (int code, String userId) {
  print('connect result ' + code.toString());
  EventBus.instance.commit(EventKeys.UpdateNotificationQuietStatus, {});
if (code == 31004 || code == 12) {
  Navigator.of(context).pushAndRemoveUntil(new MaterialPageRoute(builder: (context) => new LoginPage()), (route) => route == null);
} else if (code == 0) {
  print("connect userId" + userId);
  //  After the connection is successful, open the database 
  // _initUserInfoCache();
}

All code reference

import 'package:flutter/material.dart';
import 'package:rongcloud_im_plugin/rongcloud_im_plugin.dart' as prefix;

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var _isInit ;
  var _isConnect ;
  void _init() {
    setState(() {
      prefix.RongIMClient.init("pvxdm17jpof6r");
      _isInit=" Already initialized ";
    });
  }
  void _connect() {
    setState(() {
      prefix.RongIMClient.connect("rbdI/5jrPxR4aQ2078HhWnHte7+VrAhsnSjOcYQ3SKOCXhodQlcZYZ5acv4syCtN0dsYRNvxZh44fo4VR5s+6A==", (code, userId){
        if(code == 0 ){
          _isConnect=" Successful connection ";
        }else{
          _isConnect=" The connection fails ";
        }
      });
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(

          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              '$_isInit',
              style: Theme.of(context).textTheme.headline4,
            ),
            Text(
              '$_isConnect',
              style: Theme.of(context).textTheme.headline4,
            ),
            MaterialButton(
              minWidth: 250.0,
              onPressed: () {_init();},
              colorBrightness: Brightness.dark,
              color: Colors.deepPurpleAccent,
              elevation: 20.0,
              splashColor: Colors.green,
              //highlightColor: Colors.red,
              highlightElevation: 1.0,
              child: Text(" initialization "),
            ),
            MaterialButton(
              minWidth: 250.0,
              onPressed: () {_connect();},
              colorBrightness: Brightness.dark,
              color: Colors.deepPurpleAccent,
              elevation: 20.0,
              splashColor: Colors.green,
              //highlightColor: Colors.red,
              highlightElevation: 1.0,
              child: Text(" Connect "),
            ),
          ],
        ),
      ),
    );
  }
}

版权声明
本文为[Tai Chi]所创,转载请带上原文链接,感谢

Scroll to Top