[
Expanded(
flex: 9,
child: ListView.builder(
itemCount: msg.length,
padding: const EdgeInsets.only(top: 10, bottom: 10),
physics: const BouncingScrollPhysics(),
itemBuilder: (context, index) {
String data = msg[index]["data"] ?? "";
String dt = msg[index]["datetime"] ?? "";
String datetime = dt.substring(0, 19);
return Container(
decoration: const BoxDecoration(
color: Color.fromARGB(255, 211, 220, 215),
borderRadius: BorderRadius.all(Radius.circular(20))),
margin: const EdgeInsets.only(
top: 2.5, bottom: 2.5, left: 5, right: 5),
child: ListTile(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
title: Text(data),
trailing: Text(datetime,
style: const TextStyle(
fontSize: 12,
color: Color.fromARGB(255, 192, 48, 217))),
textColor: const Color.fromARGB(255, 227, 154, 7),
),
);
},
),
),
Expanded(
flex: 1,
child: Row(
children: [
const SizedBox(
width: 4,
),
Expanded(
flex: 1,
child: FloatingActionButton(
onPressed: () {
// ignore: avoid_print
print("Left Button");
},
child: const Icon(Icons.emoji_emotions_rounded))),
const SizedBox(
width: 4,
),
Expanded(
flex: 8,
child: TextField(
controller: myController,
decoration: const InputDecoration(
border: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(20.0))),
),
),
),
const SizedBox(
width: 4,
),
Expanded(
flex: 1,
child: FloatingActionButton(
onPressed: () {
widget.shiSock.send("main", myController.text);
myController.clear();
},
tooltip: 'send the content of text field to the server',
child: const Icon(Icons.send),
),
),
const SizedBox(
width: 4,
),
],
),
),
],
),
),
);
}
}">
import 'package:flutter/material.dart';
import 'package:shisock_flutter/shisock_flutter.dart';
// main function or the starting point
void main() {
runApp(MyApp());
}
// The main class for the flutter application and it is the root
// of you application.
class MyApp extends StatelessWidget {
// The declaration and initialisation of the shiSockClient object.
ShiSockClient client = ShiSockClient();
// If running on Android Emulator, 10.0.2.2 will the localhost
String address = "10.0.2.2";
int port = 8080;
// shiSockListener object which will be used later in the application,
// that's why it declared using late keyword.
late ShiSockListener shiSock;
// The construction of class. It is used to initialise the shiSock variable.
MyApp({Key? key}) : super(key: key) {
// Initialisation of shiSock variable using client' connect function.
// connect function return a shiSockListener object.
shiSock = client.connect(address, port);
}
// Root build function of the application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(shiSock: shiSock),
);
}
}
// A stateful widget class becouse there will the changes in the application UI
// on the fly.
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.shiSock}) : super(key: key);
final ShiSockListener shiSock;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
// A simple UI class.
class _MyHomePageState extends State<MyHomePage> {
final myController = TextEditingController();
List<Map<String, String>> msg = [];
void listener() {
widget.shiSock.listen("main", (data, sender) {
setState(() {
msg.add(data);
});
});
}
@override
Widget build(BuildContext context) {
listener();
return Scaffold(
appBar: AppBar(
title: const Text("shiSock Test"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
flex: 9,
child: ListView.builder(
itemCount: msg.length,
padding: const EdgeInsets.only(top: 10, bottom: 10),
physics: const BouncingScrollPhysics(),
itemBuilder: (context, index) {
String data = msg[index]["data"] ?? "";
String dt = msg[index]["datetime"] ?? "";
String datetime = dt.substring(0, 19);
return Container(
decoration: const BoxDecoration(
color: Color.fromARGB(255, 211, 220, 215),
borderRadius: BorderRadius.all(Radius.circular(20))),
margin: const EdgeInsets.only(
top: 2.5, bottom: 2.5, left: 5, right: 5),
child: ListTile(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
title: Text(data),
trailing: Text(datetime,
style: const TextStyle(
fontSize: 12,
color: Color.fromARGB(255, 192, 48, 217))),
textColor: const Color.fromARGB(255, 227, 154, 7),
),
);
},
),
),
Expanded(
flex: 1,
child: Row(
children: [
const SizedBox(
width: 4,
),
Expanded(
flex: 1,
child: FloatingActionButton(
onPressed: () {
// ignore: avoid_print
print("Left Button");
},
child: const Icon(Icons.emoji_emotions_rounded))),
const SizedBox(
width: 4,
),
Expanded(
flex: 8,
child: TextField(
controller: myController,
decoration: const InputDecoration(
border: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(20.0))),
),
),
),
const SizedBox(
width: 4,
),
Expanded(
flex: 1,
child: FloatingActionButton(
onPressed: () {
widget.shiSock.send("main", myController.text);
myController.clear();
},
tooltip: 'send the content of text field to the server',
child: const Icon(Icons.send),
),
),
const SizedBox(
width: 4,
),
],
),
),
],
),
),
);
}
}