Flutter学习之List列表,点击刷新,跳转,传参.

依照官方文档,手动写了一个列表的点击更新数据,跳转二级页面,传递参数

第一个类:ListDart

import 'package:flutter/material.dart';
import 'package:flutter_app/demo/SecondApp.dart';

void main() {
  runApp(new ListDart());
}

class ListDart extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Sample App',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new ListPage(),
    );
  }
}

class ListPage extends StatefulWidget {
  ListPage({Key key}) : super(key: key);

  @override
  State createState() {
    return new ListState();
  }

}

class Todo {
  String title;
}

class ListState extends State {
  List widgets = [];
  List todos = [];
  @override
  void initState() {
    super.initState();
    for (int i = 0; i < 10; i++) {
      widgets.add(getRow(i));
      Todo todo = new Todo();
      todo.title = i.toString();
      todos.add(todo);
    }
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(title: new Text('listDemo'),),
      body: new ListView.builder(
        itemCount: widgets.length,
        itemBuilder: (BuildContext context, int position) {
          return getRow(position);
        },
      ),
    );
  }

  Widget getRow(int i) {
    return new GestureDetector(
      child: new Padding(
          padding: new EdgeInsets.all(10.0),
          child: new Text("Row $i")),
      onTap: () {
        setState(() {
          widgets.add(getRow(widgets.length + 1));
          print('row $i');
          Navigator.push(
              context, new MaterialPageRoute(builder: (BuildContext context) {
//            return new SecondApp(); //不传值的跳转
            return new SecondApp(todo: todos[i]);//带传值的跳转
          }));
        });
      },
    );
  }
}

 

第二个类:SecondApp

import 'package:flutter/material.dart';
import 'package:flutter_app/list/ListDart.dart';

class SecondApp extends StatelessWidget {
  // Declare a field that holds the Todo
  final Todo todo;

  // In the constructor, require a Todo
  SecondApp({Key key, @required this.todo}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var title = "";
    if(null==todo || todo.title.isEmpty){
      title = "第二个界面";
    }else{
      title = todo.title;
    }
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("${title}"),
        backgroundColor: Colors.red,
      ),
      body: new Center(
        child: new RaisedButton(
            child: new Text('返回'),
            onPressed: () {
              Navigator.pop(context);
            }),
      ),
    );
  }
}

你可能感兴趣的:(Flutter学习之List列表,点击刷新,跳转,传参.)