Push message avec pavé numérique

Bonjour,

J’ai un problème, je dois envoyer un message via un topic, jusque-là c’est bon.
Maintenant, je dois adapter le code à un pavé numérique qui réalise la même dynamique et là je sèche :

Quelqu’un peut m’aider ?

Merci d’avance,

Laurent

Voici

Citation

le code :

// ignore_for_file: unused_element, sized_box_for_whitespace, sort_child_properties_last, prefer_interpolation_to_compose_strings, file_names, no_leading_underscores_for_local_identifiers, avoid_print

import ‹ dart:io › show Platform;
import ‹ package:call_nursing/widgets/num_pad.dart ›;
import ‹ package:flutter/material.dart ›;
import ‹ package:provider/provider.dart ›;
import ‹ package:call_nursing/mqtt/state/MQTTAppState.dart ›;
import ‹ package:call_nursing/mqtt/MQTTManager.dart ›;

class MQTTView extends StatefulWidget {
const MQTTView({Key? key}) : super(key: key);

@override
State createState() {
return _MQTTViewState();
}
}

class _MQTTViewState extends State {
final TextEditingController _hostTextController = TextEditingController();
final TextEditingController _messageTextController = TextEditingController();
final TextEditingController _messageTextController2 = TextEditingController();
final TextEditingController _topicTextController = TextEditingController();
final TextEditingController _topicTextController2 = TextEditingController();
late MQTTAppState currentAppState;
late MQTTManager manager;

@override
void initState() {
super.initState();

_hostTextController.addListener(_printLatestValue);
_messageTextController.addListener(_printLatestValue);
_topicTextController.addListener(_printLatestValue);

}

@override
void dispose() {
_hostTextController.dispose();
_messageTextController.dispose();
_messageTextController2.dispose();
_topicTextController.dispose();
_topicTextController2.dispose();
super.dispose();
}

_printLatestValue() {
print(« Second text field: ${_hostTextController.text} »);
print(« Second text field: ${_messageTextController.text} »);
print(« Second text field: ${_topicTextController.text} »);
}

@override
Widget build(BuildContext context) {
final MQTTAppState appState = Provider.of(context);
// Keep a reference to the app state.
currentAppState = appState;
final Scaffold scaffold =
Scaffold(resizeToAvoidBottomInset: false, body: _buildColumn());
return scaffold;
}

Widget _buildAppBar(BuildContext context) {
return AppBar(
title: const Center(child: Text(‹ APPEL INFIRMIER ›)),
backgroundColor: Colors.blueGrey,
);
}

Widget _buildColumn() {
return Column(
children: [
_buildAppBar(context),
_buildConnectionStateText(
_prepareStateMessageFrom(currentAppState.getAppConnectionState)),
_buildScrollableTextWith(currentAppState.getHistoryText),
//_buildNumericPad(),
_buildEditableColumn(),
],
);
}

Widget _buildNumericPad() {
final TextEditingController _myController = TextEditingController();
return Container(
margin: const EdgeInsets.only(left: 4, right: 4),
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(20),
child: SizedBox(
width: 200,
height: 10,
child: Center(
child: TextField(
controller: _myController,
textAlign: TextAlign.center,

            showCursor: false,
            style: const TextStyle(fontSize: 20),
            // Disable the default soft keybaord
            keyboardType: TextInputType.none,
          )),
        ),
      ),
      // implement the custom NumPad
      NumPad(
        buttonSize: 50,
        buttonColor: const Color.fromARGB(255, 9, 56, 225),
        iconColor: const Color.fromARGB(255, 10, 234, 59),
        controller: _myController,
        delete: () {
          _myController.text = _myController.text
              .substring(0, _myController.text.length - 1);
        },
        /*onSubmit: () {
          _buildSendButtonFrom;
        },*/
        // do something with the input numbers
        onSubmit: () {
          debugPrint('Your code: ${_myController.text}');
          /*showDialog(
              context: context,
              builder: (_) => AlertDialog(
                    title: const Text('Login'),
                    content: Text(
                      "You code is ${_myController.text}",
                      style: const TextStyle(fontSize: 25),
                    ),
                    actions: <Widget>[
                      Center(
                          child: TextButton(
                              onPressed: () => Navigator.pop(context, 'OK'),
                              style: ButtonStyle(
                                backgroundColor:
                                    MaterialStateProperty.all<Color>(
                                        Colors.greenAccent),
                                foregroundColor:
                                    MaterialStateProperty.all<Color>(
                                        Colors.white),
                              ),
                              child: const Text("Login"))),
                      Center(
                          child: TextButton(
                              onPressed: () => Navigator.pop(context, 'OK'),
                              style: ButtonStyle(
                                backgroundColor:
                                    MaterialStateProperty.all<Color>(
                                        Colors.redAccent),
                                foregroundColor:
                                    MaterialStateProperty.all<Color>(
                                        Colors.white),
                              ),
                              child: const Text("Cancel"))),
                    ],
                  ))*/
        },
      ),
      const SizedBox(
        width: 400,
        height: 50,
      ),
    ],
  ),
);

}

Widget _buildEditableColumn() {
return Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
/_buildTextFieldWith(_hostTextController, ‹ Enter broker address ›,
currentAppState.getAppConnectionState),
const SizedBox(height: 10),
_buildTextFieldWith(
_topicTextController,
‹ Enter a topic to subscribe ›,
currentAppState.getAppConnectionState),
const SizedBox(height: 10),
/
/_buildTextFieldWith(_topicTextController2, ‹ Enter a topic to listen ›,
currentAppState.getAppConnectionState),
const SizedBox(height: 10),
/
_buildNumericPad(),
_buildPublishMessageRow(),
const SizedBox(height: 10),
_buildConnecteButtonFrom(currentAppState.getAppConnectionState)
],
),
);
}

Widget _buildPublishMessageRow() {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Expanded(
child: _buildTextFieldWith(_messageTextController, ‹ Enter a message ›,
currentAppState.getAppConnectionState),
),
_buildSendButtonFrom(currentAppState.getAppConnectionState),
],
);
}

Widget _buildConnectionStateText(String status) {
return Row(
children: [
Expanded(
child: Container(
color: Colors.deepOrangeAccent,
child: Text(status, textAlign: TextAlign.center)),
),
],
);
}

Widget _buildTextFieldWith(TextEditingController controller, String hintText,
MQTTAppConnectionState state) {
bool shouldEnable = false;
if (controller == _messageTextController &&
state == MQTTAppConnectionState.connected) {
shouldEnable = true;
} else if ((controller == _hostTextController &&
state == MQTTAppConnectionState.disconnected) ||
(controller == _topicTextController &&
state == MQTTAppConnectionState.disconnected)) {
shouldEnable = true;
}
return TextField(
enabled: shouldEnable,
controller: controller,
decoration: InputDecoration(
contentPadding:
const EdgeInsets.only(left: 0, bottom: 0, top: 0, right: 0),
labelText: hintText,
));
}

Widget _buildScrollableTextWith(String text) {
return Padding(
padding: const EdgeInsets.all(20.0),
child: Container(
width: 400,
height: 50,
child: SingleChildScrollView(
child: Text(text),
),
),
);
}

Widget _buildConnecteButtonFrom(MQTTAppConnectionState state) {
return Row(
children: [
Expanded(
// ignore: deprecated_member_use
child: RaisedButton(
color: Colors.lightBlueAccent,
child: const Text(‹ Connect ›),
onPressed: state == MQTTAppConnectionState.disconnected
? _configureAndConnect
: null, //
),
),
const SizedBox(
width: 10,
),
Expanded(
// ignore: deprecated_member_use
child: RaisedButton(
color: Colors.redAccent,
child: const Text(‹ Disconnect ›),
onPressed: state == MQTTAppConnectionState.connected
? _disconnect
: null, //
),
),
],
);
}

Widget _buildSendButtonFrom(MQTTAppConnectionState state) {
// ignore: deprecated_member_use
return IconButton(
icon: const Icon(
Icons.done_rounded,
color: Colors.green,
),
onPressed: state == MQTTAppConnectionState.connected
? () {
_publishMessage(_messageTextController.text);
}
: null, //
);
}

// Utility functions
String _prepareStateMessageFrom(MQTTAppConnectionState state) {
switch (state) {
case MQTTAppConnectionState.connected:
return ‹ Connected ›;
case MQTTAppConnectionState.connecting:
return ‹ Connecting ›;
case MQTTAppConnectionState.disconnected:
return ‹ Disconnected ›;
}
}

void _configureAndConnect() {
// ignore: flutter_style_todos
// ignore: todo
// TODO: Use UUID
String osPrefix = ‹ Flutter_iOS ›;
if (Platform.isAndroid) {
osPrefix = ‹ Flutter_Android ›;
}
manager = MQTTManager(
host: _hostTextController.text,
topic: _topicTextController.text,
topic2: _topicTextController.text,
identifier: osPrefix,
state: currentAppState);
manager.initializeMQTTClient();
manager.connect();
}

void _disconnect() {
manager.disconnect();
}

void _publishMessage(String text) {
String osPrefix = ‹ Flutter_iOS ›;
if (Platform.isAndroid) {
osPrefix = ‹ Flutter_Android ›;
}
final String message = osPrefix + ’ says: ’ + text;
manager.publish(message);
_messageTextController.clear();
}
}

Bonjour,

Dans ce genre de fonctionnement je crée une partie contrôleur qui va gérer le flux de donnée et son état à part avec des fonctions simple comme addData, remove, stream (voir StreamBuilder, Bloc).
Ensuite il suffit de connecter le stream pour afficher l’ensemble des topics quelque soit leur provenance. Séparer la logique du rendu.

Bonjour,

Merci pour votre réponse, je suis débutant avec Flutter donc pas simple.

Laurent