導航參數的傳遞和接收-Flutter

導航參數的傳遞和接收

  • 效果
  • 源代碼
import 'package:flutter/material.dart';
void main()=>runApp(MaterialApp(
  title: "導航參數的傳遞和接收",
  home: First(),
));

class First extends StatelessWidget{
  //生成0-19的列表
  List<int> i = new List.generate(20, (m)=>m);
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("導航參數的傳遞和接收"),
      ),
      //若干列表用builder方法生成
      body: ListView.builder(
        //列表的數量
        itemCount: 20,
        //列表生成(index從0到19)
        itemBuilder: (context,index){
          //每一個列表都是一個ListTile
          return ListTile(
            title: Text("The square of ${i[index]}"),
            //點擊ListTile時
            onTap: (){
              Navigator.push(context, MaterialPageRoute(
                //用Second類的構造函數傳遞參數
                builder: (context)=> new Second(temp: i[index],)
              ));
            },
          );
        },
      ),
    );
  }
}

class Second extends StatelessWidget{
  //創建臨時int對象接收參數
  int temp;
  //Second用來接收參數的構造函數
  Second({Key key,this.temp}):super(key: key);
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("The square of ${temp}"),
      ),
      body: Center(child: Text("${temp*temp}",style: TextStyle(fontSize: 30.0),)),
    );
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章