首页 文章

Flutter Dart构造函数

提问于
浏览
6

在flutter Examples页面中,有一个名为“将数据发送到新屏幕”的项目 . 我有问题在第65行调用构造函数 .

Sending Data to a new screen

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

什么是超级(键:键)?请问我能解释整行吗?守则在这里......

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class Todo {
  final String title;
  final String description;

  Todo(this.title, this.description);
}

void main() {
  runApp(MaterialApp(
    title: 'Passing Data',
    home: TodosScreen(
      todos: List.generate(
        20,
            (i) => Todo(
          'Todo $i',
          'A description of what needs to be done for Todo $i',
        ),
      ),
    ),
  ));
}

class TodosScreen extends StatelessWidget {
  final List<Todo> todos;

  TodosScreen({Key key, @required this.todos}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Todos'),
      ),
      body: ListView.builder(
        itemCount: todos.length,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text(todos[index].title),
            // When a user taps on the ListTile, navigate to the DetailScreen.
            // Notice that we're not only creating a DetailScreen, we're
            // also passing the current todo through to it!
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => DetailScreen(todo: todos[index]),
                ),
              );
            },
          );
        },
      ),
    );
  }
}

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

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

  @override
  Widget build(BuildContext context) {
    // Use the Todo to create our UI
    return Scaffold(
      appBar: AppBar(
        title: Text("${todo.title}"),
      ),
      body: Padding(
        padding: EdgeInsets.all(16.0),
        child: Text('${todo.description}'),
      ),
    );
  }
}

2 回答

  • 7

    构造函数有两个命名参数 .
    默认情况下,命名参数是可选的 .
    @required 是Dart分析器识别的注释,如果在构建时调用时未传递,则会产生警告(它在运行时无效) .

    : 启动"initializer list",这是一个逗号分级的表达式列表,在超类的构造函数之前执行,因此也在构造函数体之前执行 .
    它通常用于使用断言检查参数值并使用计算值初始化最终字段 .
    一个限制是,表达式无法读取访问 this. (隐含地或明确地),因为在执行超级构造函数之前未完成对象初始化 .

    初始化程序中的最后一个元素是对超类的默认构造函数的隐式调用(如果省略),或者调用当前类的特定构造函数或超类(如果给定) .

    在您的问题的示例中,传递给构造函数的 key 参数被转发到超类的未命名构造函数的命名参数 key .

  • 4

    这是补充GünterZöchbauer解释的一个例子 . 它是Align小部件的构造函数 .

    class Align extends SingleChildRenderObjectWidget {
    
      // constructor
      const Align({
        Key key,                                                   // named parameter
        this.alignment = Alignment.center,                         // named parameter
        this.widthFactor,                                          // named parameter
        this.heightFactor,                                         // named parameter
        Widget child                                               // named parameter
      }) : assert(alignment != null),                              // initializer list
           assert(widthFactor == null || widthFactor >= 0.0),      // initializer list
           assert(heightFactor == null || heightFactor >= 0.0),    // initializer list
           super(key: key, child: child);                          // initializer list
    
      // class variables
      final AlignmentGeometry alignment;
      final double widthFactor;
      final double heightFactor;
    

    更多说明:

    没有 this. 前缀的

    • 参数是超类的变量 .
      this. 开头的
    • 参数是当前类中定义的变量 .

相关问题