0
目前我是自我飛鏢,主要是應用程序開發與撲。我遇到了將元素存儲到字符串數組中,從另一個類獲取數組併成功顯示的問題。我的目標是建立一個簡單的應用程序,提出3個問題並以非常有趣的方式顯示它們!將元素存入數組
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)
)
]
)
)
)
);
}}