-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
pet_list_screen.dart
103 lines (94 loc) · 3.1 KB
/
pet_list_screen.dart
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
// 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:go_router/go_router.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 {
const PetListScreen({super.key});
@override
State<PetListScreen> createState() => _PetListScreenState();
}
class _PetListScreenState extends State<PetListScreen> {
PetListModel petListModel = PetListModel(petList: []);
final scaffoldKey = GlobalKey<ScaffoldState>();
@override
void didChangeDependencies() {
super.didChangeDependencies();
// Receives a string of json object from the platform and converts it
// to PetModel.
final scaffoldMessenger = ScaffoldMessenger.of(context);
const BasicMessageChannel<String?>('stringCodecDemo', StringCodec())
.setMessageHandler((message) async {
if (message == null) {
scaffoldMessenger.showSnackBar(
const SnackBar(
content: Text('An error occurred while adding pet details.'),
),
);
} else {
setState(() {
petListModel = PetListModel.fromJson(message);
});
}
return null;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: scaffoldKey,
appBar: AppBar(
title: const Text('Pet List'),
),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.add),
onPressed: () {
context.go('/petListScreen/addPetDetails');
},
),
body: petListModel.petList.isEmpty
? const Center(child: Text('Enter Pet Details'))
: BuildPetList(petListModel.petList),
);
}
}
/// Shows list of [PetDetails].
class BuildPetList extends StatelessWidget {
final List<PetDetails> petList;
const BuildPetList(this.petList, {super.key});
@override
Widget build(BuildContext context) {
return ListView.builder(
padding: const 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: const Icon(Icons.delete),
onPressed: () async {
final scaffoldMessenger = ScaffoldMessenger.of(context);
try {
await PetListMessageChannel.removePet(index);
scaffoldMessenger.showSnackBar(
const SnackBar(content: Text('Removed successfully!')),
);
} catch (error) {
scaffoldMessenger.showSnackBar(SnackBar(
content: Text((error as PlatformException).message!),
));
}
},
),
);
},
);
}
}