Вы можете скопировать и вставить полный код ниже
Некоторые части вашего кода необходимо изменить, вы можете проверить полный код для деталей
фрагмент кода
List<Widget> messages = [];
List<String> messagesTo = [];
Widget chats = Text('Welcome');
onPressed: () {
messagesTo.add(myController.text);
Future.delayed(Duration(seconds: 1), () {
setState(() {
messages = [];
messagesTo.forEach((content) =>
messages.add(Card(child: Text(content))));
});
});
}
...
messages.length == 0
? chats
: Container(
height: 200,
child: ListView(shrinkWrap: true, children: messages)),
рабочая демоверсия
полный код
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final myController = TextEditingController();
List<Widget> messages = [];
List<String> messagesTo = [];
Widget chats = Text('Welcome');
Widget textField(String hint, TextInputType type) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: CupertinoTextField(
minLines: 1,
maxLines: null,
controller: myController,
textCapitalization: TextCapitalization.sentences,
placeholder: hint,
placeholderStyle: TextStyle(color: Colors.blue),
keyboardType: type,
style: TextStyle(color: Colors.red),
),
);
}
@override
void dispose() {
myController.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: true,
//resizeToAvoidBottomPadding: true,
appBar: AppBar(
title: Text(widget.title),
),
body: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
messages.length == 0
? chats
: Container(
height: 200,
child: ListView(shrinkWrap: true, children: messages)),
Container(
height: 50,
child: textField('Type a message', TextInputType.multiline)),
IconButton(
icon: Icon(
Icons.send,
color: Colors.yellow,
),
onPressed: () {
messagesTo.add(myController.text);
Future.delayed(Duration(seconds: 1), () {
setState(() {
messages = [];
messagesTo.forEach((content) =>
messages.add(Card(child: Text(content))));
});
});
}),
],
),
),
);
}
}