Reputation: 1
Currently I am self-leaning dart, mainly app development with flutter. I'm having the problem of storing elements into a string array, getting the array from another class and successfully display. My goal is just to build a simple app that ask 3 question and displays them in a very fun way!!
class SecondPage extends StatefulWidget
{
@override
SecondPageState createState() => new SecondPageState();
}
class SecondPageState extends State<SecondPage>
{
final TextEditingController controller = new TextEditingController();
int counter = 0;
var ask = ["What is your name?", "How old are you?", "What is your favorite hobby?"];
var results = ["","",""];
@override
Widget build(BuildContext context)
{
return new Scaffold(
appBar: new AppBar(title: new Text("Tell us about yourself"), backgroundColor: Colors.deepOrange),
body: new Container(
padding: new EdgeInsets.all(20.0),
child: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(ask[counter], style: new TextStyle(fontSize: 15.0, fontWeight: FontWeight.bold)),
new TextField(
decoration: new InputDecoration(
hintText: "Type Here"
),
onSubmitted: (String str)
{
setState(()
{
results[counter] = ask[counter]; //Assigning
if(counter < 2)
{
counter = counter + 1;
}
else
{
counter = 0;
}
});
},
),
new IconButton(
padding: new EdgeInsets.only(right: 50.0),
icon: new Icon(Icons.library_books, size: 70.0, color: Colors.blueAccent),
onPressed: () {Navigator.of(context).pushNamed("/ThirdPage");}
),
]
)
)
)
);
}
}
class ThirdPageState extends State<ThirdPage>
{
SecondPageState _second = new SecondPageState();
// ignore: unnecessary_getters_setters
SecondPageState get second => _second;
// ignore: unnecessary_getters_setters, avoid_return_types_on_setters
void set second(SecondPageState second) {
_second = second;
}
@override
Widget build(BuildContext context)
{
//_second.results[0] = "christopher";
return new Scaffold(
appBar: new AppBar(title: new Text("Your Biography"), backgroundColor: Colors.deepOrange),
body: new Container(
padding: new EdgeInsets.all(20.0),
child: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new MyCard(
tittle: new Text(second.results[0], style: new TextStyle(
fontSize: 25.0
)),
icon: new Icon(Icons.favorite, size: 40.0, color: Colors.redAccent)
),
new MyCard(
tittle: new Text(second.results[1], style: new TextStyle(
fontSize: 25.0
)),
icon: new Icon(Icons.local_pizza, size: 40.0, color: Colors.brown)
),
new MyCard(
tittle: new Text(second.results[2], style: new TextStyle(
fontSize: 25.0
)),
icon: new Icon(Icons.visibility, size: 40.0, color: Colors.blue)
)
]
)
)
)
);
} }
Upvotes: 0
Views: 1084
Reputation: 116848
Rather than calling Navigator.pushNamed
, you should call Navigator.push
. This will allow you to pass arguments to your ThirdPage
constructor.
Navigator.push(context, new MaterialPageRoute<Null>(
builder: (context) => new ThirdPage(results: results);
));
You should store the result List
as a final member on ThirdPage
and access it in ThirdPageState
as widget.results
.
Generally you should have not have SecondPageState
storing a direct reference to ThirdPageState
as a member variable. While it's possible for a State to get a reference to another state, you'd want to use GlobalKey
and call currentState
, and it's much more maintainable to just pass the data as constructor arguments if you can.
Upvotes: 1