flutter - 如何在颤动中将变量从一个.dart文件导入另一个
问题描述
我正在尝试从 udacity 学习颤振,其中一项任务是将变量从 main.dart 文件导入到 category.dart 以创建新的小部件。
不幸的是,我无法将主 dart 文件中的 _categoryIcon 导入到 dart 类别中。(也是 _categoryName 和 _categoryColor)这里是 main.dart 文件:
import 'package:flutter/material.dart';
import 'package:unispero/category.dart';
const _categoryName = 'Cake';
const _categoryIcon = Icons.cake;
const _categoryColor = Colors.green;
/// The function that is called when main.dart is run.
void main() {
runApp(UnitConverterApp());
}
/// This widget is the root of our application.
/// Currently, we just show one widget in our app.
class UnitConverterApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Unit Converter',
home: Scaffold(
backgroundColor: Colors.green[100],
body: Center(
// TODO: Determine what properties you'll need to pass into the widget
child: Category(_categoryColor, _categoryIcon, _categoryName),
),
),
);
}
}
这是 category.dart:
import 'package:flutter/material.dart';
import 'package:unispero/main.dart';
class Category extends StatelessWidget {
/// Creates a [Category].
///
/// A [Category] saves the name of the Category (e.g. 'Length'), its color for
/// the UI, and the icon that represents it (e.g. a ruler).
// TODO: You'll need the name, color, and iconLocation from main.dart
const Category(_categoryColor, _categoryIcon, _categoryName);
/// Builds a custom widget that shows [Category] information.
///
/// This information includes the icon, name, and color for the [Category].
@override
// This `context` parameter describes the location of this widget in the
// widget tree. It can be used for obtaining Theme data from the nearest
// Theme ancestor in the tree. Below, we obtain the display1 text theme.
// See https://docs.flutter.io/flutter/material/Theme-class.html
Widget build(BuildContext context) {
// TODO: Build the custom widget here, referring to the Specs.
// const _categoryIcon = Icons.cake;
// IconData? _categoryIcon;
return Container(
// width: 50,
child: Row(children: <Widget>[
Icon(_categoryIcon),
]),
height: 100,
color: Colors.blueAccent,
);
}
}
抱歉,如果这是一个简单的问题,但我找不到答案
解决方案
您不能从另一个具有underscore
. 这些Underscore
字段只能在.dart
文件中访问。在这种情况下,您的main.dart
. 如果您仍想访问这些值,则必须getters
为它们创建。
get categoryName => _categoryName;
get categoryIcon => _categoryIcon;
get categoryColor => _categoryColor;
我看到的另一个问题是您的Category
构造函数。您传递了您的值,main.dart
但没有在Category
.
class Category extends StatelessWidget {
/// Creates a [Category].
///
/// A [Category] saves the name of the Category (e.g. 'Length'), its color for
/// the UI, and the icon that represents it (e.g. a ruler).
// TODO: You'll need the name, color, and iconLocation from main.dart
const Category(this._categoryColor, this._categoryIcon, this._categoryName);
final _categoryColor;
final _categoryIcon;
final _categoryName;
/// Builds a custom widget that shows [Category] information.
///
/// This information includes the icon, name, and color for the [Category].
@override
// This `context` parameter describes the location of this widget in the
// widget tree. It can be used for obtaining Theme data from the nearest
// Theme ancestor in the tree. Below, we obtain the display1 text theme.
// See https://docs.flutter.io/flutter/material/Theme-class.html
Widget build(BuildContext context) {
// TODO: Build the custom widget here, referring to the Specs.
// const _categoryIcon = Icons.cake;
// IconData? _categoryIcon;
return Container(
// width: 50,
child: Row(children: <Widget>[
Icon(_categoryIcon),
]),
height: 100,
color: Colors.blueAccent,
);
}
}
推荐阅读
- python - 用于制作和输出多个线性回归的循环
- javascript - 找不到命令 eslint --init,为什么?
- python - 如何在 Python 中使用 Pandas 连接 CSV 文件中的列
- javascript - 算法问题:唯一路径 III。在javascript中使用回溯模式并且不起作用
- sql - 是否有任何语句可以将行值转换为 oracle 中的同一行但不同的列?
- spring-boot - Gradle 构建失败:无法打开 cp_init 重新映射的类缓存
- oracle - 带有休眠序列的 Oracle db 序列
- postgresql - TypeORM postgres db,参数化查询添加引号和查询失败
- r - 对非有限值进行故障排除 (stat_smooth)
- r - 为什么我的 R 代码中出现 'y' 错误中的 NA/NaN/Inf?