【发布时间】:2020-01-16 23:10:01
【问题描述】:
对 Flutter 很陌生……从 Android SDK 迁移过来。如何以编程方式将相同的 Flutter Image 小部件从使用资产更改为文件?
尝试了两个具有相同功能的布局小部件,一个使用 Image.asset,另一个使用 Image.file,这项工作但效率不高,因为我使用了两个执行相同显示的小部件类,唯一的区别在于路径。与以下相同,但类名更改为 _RegisterUserAfter 并使用 Image.path。
class _RegisterUserState extends State<RegisterUser> {
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
appBar: AppBar(
title: Text('New User Registration'),
backgroundColor: Colors.black,
),
body: SingleChildScrollView(
child: Container(
padding: EdgeInsets.all(30.0),
child: Column(
children: <Widget>[
GestureDetector(
onTap: _onCamera,
child: Container(
width: 190,
height: 190,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: AssetImage(pathAsset),
fit: BoxFit.fill,
)),
)),
TextField(
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: 'Email',
)),
TextField(
decoration: InputDecoration(
labelText: 'Password',
),
obscureText: true,
),
TextField(
keyboardType: TextInputType.phone,
decoration: InputDecoration(
labelText: 'Phone',
)),
SizedBox(
height: 10,
),
MaterialButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0)),
minWidth: 300,
height: 50,
child: Text('Register'),
color: Colors.black,
textColor: Colors.white,
elevation: 15,
onPressed: _onRegister,
),
SizedBox(
height: 10,
),
GestureDetector(
onTap: _onBackPress,
child:
Text('Already Register', style: TextStyle(fontSize:
16))),
],
),
),
),
);
}
当 onCamera 方法捕获图像并存储在本地目录中时,图像将出现在同一个图像小部件上。或者我是否需要使用图像小部件一个用于资产,另一个用于文件?然后在文件可用时隐藏资产? 从纯 android-java 迁移到 dart 相当具有挑战性..需要一些指针..谢谢
【问题讨论】:
-
能否提供您的
_onCamera方法? -
我使用flutter.dev flutter.dev/docs/cookbook/plugins/picture-using-camera提供的示例