2017-05-25 109 views
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) 
     ) 
     ] 
    ) 
    ) 
) 
); 

}}

回答

1

不需要調用Navigator.pushNamed,你應該叫Navigator.push。這將允許您將參數傳遞給您的ThirdPage構造函數。

Navigator.push(context, new MaterialPageRoute<Null>(
    builder: (context) => new ThirdPage(results: results); 
)); 

您應該將結果List存儲爲上ThirdPage最後的成員,訪問它在ThirdPageState作爲widget.results

通常,您應該沒有將ThirdPageState作爲成員變量存儲的SecondPageState。儘管一個州有可能獲得對另一個州的參考,但是您希望使用GlobalKey並呼叫currentState,並且如果可以的話,只需傳遞數據作爲構造函數參數就可維護得多。