0
I have two pages. On the first page I want to return a list. I want to insert in this list the values that come from the second page. So the flow is more or less the following: I enter the second page, send the value to the first page, save to an array type variable and display all values in a list.
The problem is that I can’t create the list. I can only display the last data I send instead of the list of everything I’ve sent.
Here’s the code:
Front page
import 'package:flutter/material.dart';
class PageTwo extends StatefulWidget {
final String descricao;
const PageTwo({Key key, this.descricao}) : super(key: key);
@override
_PageTwoState createState() => _PageTwoState();
}
class _PageTwoState extends State<PageTwo> {
final List<String> listDescricao = [];
@override
void initState() {
listDescricao.add(widget.descricao);
super.initState();
}
//List images items
Widget imgListItem() {
return ListView.builder(
itemCount: listDescricao.length,
itemBuilder: (BuildContext context, int index) {
return widget.descricao == null
? Container()
: Text(listDescricao[index]);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: imgListItem(),
);
}
}
Second page
import 'package:flutter/material.dart';
class PageOne extends StatefulWidget {
@override
_PageOneState createState() => _PageOneState();
}
class _PageOneState extends State<PageOne> {
final GlobalKey<ScaffoldState> _scaffoldkey = new GlobalKey<ScaffoldState>();
final inputDescricaoController = TextEditingController();
//send image
doSend() async {
String descricao = inputDescricaoController.text ??= "";
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (BuildContext context) => PageTwo(descricao: descricao)),
(Route<dynamic> route) => false);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
TextField(
controller: inputDescricaoController,
),
RaisedButton(
child: Column(
children: <Widget>[
Text(
'send',
),
],
),
onPressed: () async {
doSend();
},
),
]
)
);
}
}
Is that right? The "Second Page" is the
PageOne
? And how do you open thePageOne
? For in thePageTwo
there is no reference to open thePageOne
and it takes a String parameter so you would always have to open the other page to pass a new value...– Matheus Ribeiro
the second page is pageTwo itself. To access pageTwo has a button, but at the time of summarizing the code I forgot to put, but basically it is a button with a 'push' to pageTwo
– augusto francisco
Adjust your question then, please... The title you placed on the page does not match the class name and add the button also for better understanding. Click on the button Edit which is just above the comments.
– Matheus Ribeiro