flutter - 在颤动中调用另一个函数飞镖进入页面
问题描述
我有一个带有应用栏标题的空白页面和一个称为上传页面的功能飞镖。如何将此函数调用到页面?如何显示带有上传功能的页面?
另一个问题是,flutter 可以调用一个按钮来从文件资源管理器中选择文件。单击按钮后,它将导航到文件目录,以便我可以将文件上传到页面,页面将显示我上传的文件。
[空白页][2]
上传页面.dart
import 'package:file_picker/file_picker.dart';
import 'package:flutter/services.dart';
import 'package:flutter_auth/Screens/Login/components/body.dart';
main() => runApp(UploadPage());
class UploadPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _App();
}
}
class _App extends State<UploadPage> {
String _fileName;
String _path;
Map<String, String> _paths;
String _extension;
bool _loadingPath = false;
bool _multiPick = false;
bool _hasValidMime = false;
FileType _pickingType;
TextEditingController _controller = new TextEditingController();
@override
void initState() {
super.initState();
_controller.addListener(() => _extension = _controller.text);
}
void _openFileExplorer() async {
if (_pickingType != FileType.custom || _hasValidMime) {
setState(() => _loadingPath = true);
try {
if (_multiPick) {
_path = null;
_paths = await FilePicker.getMultiFilePath(
type: _pickingType, allowedExtensions: [_extension]);
} else {
_paths = null;
_paths = await FilePicker.getMultiFilePath(
type: _pickingType, allowedExtensions: [_extension]);
}
} on PlatformException catch (e) {
print("Unsupported operation" + e.toString());
}
if (!mounted) return;
setState(() {
_loadingPath = false;
_fileName = _path != null
? _path.split('/').last
: _paths != null
? _paths.keys.toString()
: '...';
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("FilePickerApp"),
),
body: Container(
child: Center(
child: Column(
children: <Widget>[
new Padding(
padding: const EdgeInsets.only(top: 50.0, bottom: 20.0),
child: new RaisedButton(
onPressed: () => _openFileExplorer(),
child: new Text("Open file picker"),
),
),
new Builder(
builder: (BuildContext context) => _loadingPath
? Padding(
padding: const EdgeInsets.only(bottom: 10.0),
child: const CircularProgressIndicator())
: _path != null || _paths != null
? new Container(
padding: const EdgeInsets.only(bottom: 30.0),
height: MediaQuery.of(context).size.height * 0.50,
child: new Scrollbar(
child: new ListView.separated(
itemCount: _paths != null && _paths.isNotEmpty
? _paths.length
: 1,
itemBuilder: (BuildContext context, int index) {
final bool isMultiPath =
_paths != null && _paths.isNotEmpty;
final String name = 'File $index: ' +
(isMultiPath
? _paths.keys.toList()[index]
: _fileName ?? '...');
final path = isMultiPath
? _paths.values.toList()[index].toString()
: _path;
return new ListTile(
title: new Text(
name,
),
subtitle: new Text(path),
);
},
separatorBuilder:
(BuildContext context, int index) =>
new Divider(),
)),
)
: new Container(),
),
],
)),
),
),
);
}
}
[1]: https://i.stack.imgur.com/yda1c.png
[2]: https://i.stack.imgur.com/OMb0P.png
解决方案
推荐阅读
- sql - PostgreSQL 连接查询问题
- r - 跟踪患者何时可能没有药物
- reactjs - 在 react-native 暂停后重新启动时,计时器会提前几秒钟
- html - 如何修复扩展正文元素的网格布局,我的意思是网格宽度变得大于正文宽度
- asp-classic - 我需要捕获用户在经典 ASP 页面上花费的时间
- hive - 编译语句时出错:FAILED: ParseException line 6:27 cannot identify input near '
'' '' ' - javascript - 有条件地禁用复选框
- python - 我在使用 Visual Studio Code 时出现错误,在键入时它不显示错误
- gcc - cygwin32 或 cygwin64 gcc 现在支持 asan 吗?
- r - R:如何在圆形布局中手动设置ggraph中节点的位置?