main.dart 7.2 KB
Newer Older
H
hdx 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'test flutter message channel',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const ContactPage(),
    );
  }
}

class ContactDetails {
  ContactDetails({
    required this.name,
    required this.phone,
  });

  final String name;
  final String phone;

  factory ContactDetails.fromMap(Map<String, dynamic> map) => ContactDetails(
    name: map['name'] as String,
    phone: map['phone'] as String,
  );

  Map<String, String> toJson() => <String, String>{
    'name': name,
    'phone': phone,
  };
}

class ContactListModel {
  ContactListModel({
    required this.contactList,
  });

  final List<ContactDetails> contactList;

  factory ContactListModel.fromJson(String jsonString) {
    final jsonData = json.decode(jsonString) as Map<String, dynamic>;
    return ContactListModel(
      contactList: List.from((jsonData['contacts'] as List).map<ContactDetails>(
            (dynamic contactDetailsMap) => ContactDetails.fromMap(
          contactDetailsMap as Map<String, dynamic>,
        ),
      )),
    );
  }

  String toJson() {
    String json = jsonEncode(contactList.map((i) => i.toJson()).toList()).toString();
    return json;
  }
}

class ContactsMessageChannel {
  static const MethodChannel _methodChannel = MethodChannel('methodChannelDemo');

  static const _jsonMessageCodecChannel =
  BasicMessageChannel<dynamic>('jsonMessageCodecDemo', JSONMessageCodec());

  static void addContactDetails(ContactDetails contactDetails) {
    _jsonMessageCodecChannel.send(contactDetails.toJson());
  }

  static Future<ContactListModel> getContacts(String type) async {
    final reply = await _jsonMessageCodecChannel.send('getContact$type');
    if (reply == null) {
      throw PlatformException(
        code: 'INVALID',
        message: 'Failed to get contact',
        details: null,
      );
    } else {
      return ContactListModel.fromJson(reply);
    }
  }

  static Future<int> setContacts(json, String type) async {
    final result = await _methodChannel .invokeMethod<int>('setContacts$type', {'contacts': json});
    return result!;
  }
}

class ContactPage extends StatefulWidget {
  const ContactPage({super.key});

  @override
  State<ContactPage> createState() => _ContactPageState();
}

class _ContactPageState extends State<ContactPage> {
  static const int invokeCount = 1000;

  ContactListModel contactListModel = ContactListModel(contactList: []);

  String dataType = '1000';
  String dataTypeText = '1';

  int getCount = 0;
  int getSetCount = 0;
  final List<String> timeList = [];

  _setDataType() async {
    setState(() {
      if (dataType == '1000') {
        dataType = '100';
        dataTypeText = '0.1';
      } else {
        dataType = '1000';
        dataTypeText = '1';
      }
      //dataTypeText = (dataType / 1000).toString();
    });
  }

  Future<void> _getContactData() async {
    // 记录开始时间
    var startDateTime = DateTime.now();

    // 循环调用 1000 次
    for (int i = 0; i < invokeCount; i++) {
      contactListModel = await ContactsMessageChannel.getContacts(dataType);
    }

    // 记录结束时间
    var endDateTime = DateTime.now();
    // 计算消耗的毫秒数
    var totalMilliseconds = endDateTime
        .difference(startDateTime)
        .inMilliseconds;

    getCount++;

    timeList.add("第 $getCount 次读取总耗时=$totalMilliseconds 毫秒");

    // 更新界面
    setState(() {});
  }

  Future<void> _getSetContactData() async {
    // 记录开始时间
    var startDateTime = DateTime.now();

    // 先从Java层读取 1KB 联系人,然后序列为JSON后在Java层还原
    for (int i = 0; i < invokeCount; i++) {
      contactListModel = await ContactsMessageChannel.getContacts(dataType);
      await ContactsMessageChannel.setContacts(contactListModel.toJson(), dataType);
    }

    // 记录结束时间
    var endDateTime = DateTime.now();
    // 计算消耗的毫秒数
    var totalMilliseconds = endDateTime
        .difference(startDateTime)
        .inMilliseconds;

    getSetCount++;

    timeList.add("第 $getSetCount 次读取+写入总耗时=$totalMilliseconds 毫秒");

    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme
            .of(context)
            .colorScheme
            .inversePrimary,
        title: const Text('test flutter message channel'),
      ),
      body: Flex(
        direction: Axis.horizontal,
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Flexible(
            flex: 1,
            child: BuildTimeListWidget(timeList),
          ),
          Flexible(
            flex: 1,
            child: contactListModel.contactList.isEmpty
                ? const Center(child: Text('no data'))
                : BuildContactListWidget(contactListModel.contactList),
          ),
          Flexible(
            flex: 1,
            child: ListView(
              children: <Widget>[
                Container(height: 10),
                GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  child: Text('数据长度约 $dataTypeText KB'),
                  onTap: () {
                    _setDataType();
                  },
                ),
                Container(height: 10),
                const Text('循环执行 1000 次'),
                Container(height: 15),
                ElevatedButton(onPressed: _getContactData,
                  child: const Text('读取原生对象数据'),
                ),
                ElevatedButton(onPressed: _getSetContactData,
                  child: const Text('读取并写回原生对象'),
                ),
              ],
            ),
          ),
        ],
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

class BuildContactListWidget extends StatelessWidget {
  final List<ContactDetails> contactList;

  const BuildContactListWidget(this.contactList, {super.key});

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      padding: const EdgeInsets.all(1),
      itemCount: contactList.length,
      itemBuilder: (context, index) {
        return ListTile(
          title: Text(contactList[index].name, style: const TextStyle(fontSize: 12),),
          subtitle: Text(contactList[index].phone, style: const TextStyle(fontSize: 12),),
        );
      },
    );
  }
}

class BuildTimeListWidget extends StatelessWidget {
  final List<String> timeList;

  const BuildTimeListWidget(this.timeList, {super.key});

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      padding: const EdgeInsets.all(2),
      itemCount: timeList.length,
      itemBuilder: (context, index) {
        return ListTile(
            title: Text(style: const TextStyle(fontSize: 12), timeList[index])
        );
      },
    );
  }
}