Flutter 零件和导出-dart的用途是什么?
我是飞镖和飞镖的初学者。我在GitHub上看到了很多使用part和export关键字的示例。我在谷歌上搜索过,但还是不太清楚。我已经看过答案了 不过,我还是不明白。也许有点长,我不是以英语为母语的人Flutter 零件和导出-dart的用途是什么?,flutter,dart,Flutter,Dart,我是飞镖和飞镖的初学者。我在GitHub上看到了很多使用part和export关键字的示例。我在谷歌上搜索过,但还是不太清楚。我已经看过答案了 不过,我还是不明白。也许有点长,我不是以英语为母语的人 为什么这些关键词会被使用 他们做什么 下面是一个由3个文件组成的简单代码示例。两个文件组成库,一个使用库。所有功能的说明都包含在整个文件的注释中 图书馆 // declare a name for this library to reference from parts // this is not
下面是一个由3个文件组成的简单代码示例。两个文件组成库,一个使用库。所有功能的说明都包含在整个文件的注释中 图书馆
// declare a name for this library to reference from parts
// this is not necessary if we do not need to reference elsewhere
// NOTE: generally, a Dart file is a Library
library counter;
// export adds contents of another Library to this Library's namespace
// here we are adding all content (accessible from outside the library) from
// the material library
// NOTE: this is intended for related libraries
// this arbitrary usage is simply for demonstration purposes
export 'package:flutter/material.dart';
// for finer control, we can use the 'show' directive
// try this to see the effects of selected export
// export 'package:flutter/material.dart' show StatefulWidget, State;
// include any files containing parts of this library
part 'library_part.dart';
class Counter extends AbstractCounter {
// we can access library private variable _count even though it is in a different
// file because we made it part of the same library
reset() => _count = 0;
}
库_零件.dart
// declare this file as part of the counter library
part of counter;
abstract class AbstractCounter {
// this is a library private variable (_varName)
// if this file were not made part of the counter library, this variable
// would not be accessible to the Counter class in library_main.dart
// this is an important concept to understand about Dart's library privacy
// it differs from class based privacy, like that used in Java
int _count = 0;
get count => _count;
increment() => ++_count;
}
library\u示例.dart
// note that 'material.dart' does not need to be imported
// because it has been exported with the counter library
import 'library_main.dart';
class LibraryExample extends StatefulWidget {
@override
_LibraryExampleState createState() => _LibraryExampleState();
}
class _LibraryExampleState extends State<LibraryExample> {
final _counter = Counter();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Library Example'),
),
body: Center(
child: Text('You have pressed the button ${_counter.count} times.'),
),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
FloatingActionButton(
child: Text('Reset'),
onPressed: () => setState(() => _counter.reset()),
),
SizedBox(width: 10),
FloatingActionButton(
child: Icon(Icons.add),
onPressed: () => setState(() => _counter.increment()),
),
],
),
);
}
}
//请注意,不需要导入“material.dart”
//因为它已与计数器库一起导出
导入“library_main.dart”;
类库示例扩展StatefulWidget{
@凌驾
_LibraryExampleState createState()=>\u LibraryExampleState();
}
类_LibraryExampleState扩展状态{
最终计数器=计数器();
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“库示例”),
),
正文:中(
child:Text('您已按下按钮${u counter.count}次。'),
),
浮动操作按钮:行(
mainAxisAlignment:mainAxisAlignment.end,
儿童:[
浮动操作按钮(
子项:文本(“重置”),
按下时:()=>设置状态(()=>_counter.reset()),
),
尺寸箱(宽度:10),
浮动操作按钮(
子:图标(Icons.add),
按下时:()=>设置状态(()=>_counter.increment()),
),
],
),
);
}
}
这是否回答了您的问题?我不是以英语为母语的人。所以simple engish只帮助meSo你是说一个简单的dart文件可以称为库,导出就会导入库。那个么第二部分呢?我已经更新了我的答案,以便更好地解决每个关键词。你们能举一个简单的导出示例吗。那真的很有帮助没问题。查看更新的答案。我已经包括了一个简单的实现和大量的评论。希望能有帮助。