1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-10 14:58:34 +00:00

[platform_channels] adds BasicMessageChannel Demo (#484)

This commit is contained in:
Ayush Bherwani
2020-07-11 05:41:52 +05:30
committed by GitHub
parent 9140559040
commit 03008a5d19
8 changed files with 426 additions and 5 deletions

View File

@@ -0,0 +1,86 @@
// Copyright 2020 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:platform_channels/src/pet_list_message_channel.dart';
/// Demonstrates how to use [BasicMessageChannel] to send a message to platform.
///
/// The widget uses [TextField] and [RadioListTile] to take the [PetDetails.breed] and
/// [PetDetails.petType] from the user respectively.
class AddPetDetails extends StatefulWidget {
@override
_AddPetDetailsState createState() => _AddPetDetailsState();
}
class _AddPetDetailsState extends State<AddPetDetails> {
final breedTextController = TextEditingController();
String petType = 'Dog';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Add Pet Details'),
actions: <Widget>[
IconButton(
icon: Icon(Icons.add),
onPressed: () {
PetListMessageChannel.addPetDetails(
PetDetails(
petType: petType,
breed: breedTextController.text,
),
);
Navigator.pop(context);
},
)
],
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
SizedBox(
height: 8,
),
TextField(
controller: breedTextController,
decoration: InputDecoration(
border: OutlineInputBorder(),
filled: true,
hintText: 'Breed of pet',
labelText: 'Breed',
),
),
SizedBox(
height: 8,
),
RadioListTile<String>(
title: const Text('Dog'),
value: 'Dog',
groupValue: petType,
onChanged: (value) {
setState(() {
petType = value;
});
},
),
RadioListTile<String>(
title: const Text('Cat'),
value: 'Cat',
groupValue: petType,
onChanged: (value) {
setState(() {
petType = value;
});
},
),
],
),
),
);
}
}

View File

@@ -0,0 +1,84 @@
// Copyright 2020 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:convert';
import 'package:flutter/services.dart';
/// This class includes two methods [addPetDetails] and [removePet] which are used
/// to add a new pet and remove a pet from the the list respectively.
class PetListMessageChannel {
static final _jsonMessageCodecChannel =
BasicMessageChannel<dynamic>('jsonMessageCodecDemo', JSONMessageCodec());
static final _binaryCodecChannel =
BasicMessageChannel('binaryCodecDemo', BinaryCodec());
/// Method to add a new pet to the list.
///
/// Demonstrates how to use [BasicMessageChannel] and [JSONMessageCodec] to
/// send more structured data to platform like a [Map] in this case.
static void addPetDetails(PetDetails petDetails) {
_jsonMessageCodecChannel.send(petDetails.toJson());
}
/// Method to remove a pet from the list.
///
/// Demonstrates how to use [BasicMessageChannel] and [BinaryCodec] to
/// send [ByteData] to platform. If the reply received is null, then
/// we will throw a [PlatformException].
static Future<void> removePet(int index) async {
final uInt8List = utf8.encoder.convert(index.toString());
final reply = await _binaryCodecChannel.send(uInt8List.buffer.asByteData());
if (reply == null) {
throw PlatformException(
code: 'INVALID INDEX',
message: 'Failed to delete pet details',
details: null,
);
}
}
}
/// A model class that provides [petList] which is received from platform.
class PetListModel {
PetListModel({
this.petList,
});
final List<PetDetails> petList;
/// Method that maps the incoming string of json object to List of [PetDetails].
factory PetListModel.fromJson(String jsonString) {
final jsonData = json.decode(jsonString) as Map<String, dynamic>;
return PetListModel(
petList: List.from((jsonData['petList'] as List).map<PetDetails>(
(dynamic petDetailsMap) => PetDetails.fromMap(
petDetailsMap as Map<String, dynamic>,
),
)),
);
}
}
/// A simple model that provides pet details like [petType] and [breed] of pet.
class PetDetails {
PetDetails({
this.petType,
this.breed,
});
final String petType;
final String breed;
factory PetDetails.fromMap(Map<String, dynamic> map) => PetDetails(
petType: map['petType'] as String,
breed: map['breed'] as String,
);
Map<String, String> toJson() => <String, String>{
'petType': petType,
'breed': breed,
};
}

View File

@@ -0,0 +1,91 @@
// Copyright 2020 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:platform_channels/src/pet_list_message_channel.dart';
/// Demonstrates how to use [BasicMessageChannel] to send & receive the platform
/// Message.
class PetListScreen extends StatefulWidget {
@override
_PetListScreenState createState() => _PetListScreenState();
}
class _PetListScreenState extends State<PetListScreen> {
PetListModel petListModel;
@override
void initState() {
super.initState();
// Receives a string of json object from the platform and converts it
// to PetModel.
BasicMessageChannel('stringCodecDemo', StringCodec())
.setMessageHandler((message) async {
setState(() {
petListModel = PetListModel.fromJson(message);
});
return;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Pet List'),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
Navigator.pushNamed(context, '/addPetDetails');
},
),
body: petListModel?.petList?.isEmpty ?? true
? Center(child: Text('Enter Pet Details'))
: BuildPetList(petListModel.petList),
);
}
}
/// Shows list of [PetDetails].
class BuildPetList extends StatelessWidget {
final List<PetDetails> petList;
BuildPetList(this.petList);
@override
Widget build(BuildContext context) {
return ListView.builder(
padding: EdgeInsets.all(8),
itemCount: petList.length,
itemBuilder: (context, index) {
return ListTile(
title: Text('Pet breed: ${petList[index].breed}'),
subtitle: Text(
'Pet type: ${petList[index].petType}',
),
trailing: IconButton(
icon: Icon(Icons.delete),
onPressed: () async {
try {
await PetListMessageChannel.removePet(index);
showSnackBar('Removed successfully!', context);
} catch (error) {
showSnackBar(error.message.toString(), context);
}
},
),
);
},
);
}
void showSnackBar(String message, BuildContext context) {
Scaffold.of(context).showSnackBar(SnackBar(
backgroundColor: Theme.of(context).primaryColor,
content: Text(message),
));
}
}