我刚刚更新到 Dart2 和 Flutter sdk: '>=2.12.0 <3.0.0' 现在这个 if 语句中断了:
decoration: new BoxDecoration(
shape: BoxShape.circle,
color: Colors.blueAccent,
border: Border.all(
color: Colors.blueAccent,
width: 20.0,
style: BorderStyle.solid),
image: new DecorationImage(
fit: BoxFit.cover,
image: myMarkerThumb != 'noImage'
? NetworkImage(myMarkerThumb)
: AssetImage('assets/images/noImageAvailable.png'),
),
),
Run Code Online (Sandbox Code Playgroud)
不能将参数类型“Object”分配给参数类型“ImageProvider”。),
我只是从颤振开始,不知道去哪里找别的。
根据Flutter 文档,我尝试使用 DecoratedBox 加载全屏图像作为容器的背景图像。
我的 pubspec.yaml 包含嵌入式资产的相关定义:
flutter:
uses-material-design: true
assets:
- assets/background.png
Run Code Online (Sandbox Code Playgroud)
并且 widget.dart 尝试按照规定填充新容器的背景:
@override
Widget build(BuildContext context) {
return new Container(
decoration: new BoxDecoration(
color: Colors.purple,
image : new DecorationImage(
image: new ExactAssetImage('assets/background.png'),
fit: BoxFit.cover,
),
),
),
}
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
Unable to load asset: assets/background.png
Image provider: ExactAssetImage(name: "assets/background.png", scale: 1.0, bundle: null)
Run Code Online (Sandbox Code Playgroud)
显然,包没有正确解析。有没有人知道我在这里做错了什么?
是否可以在Scaffold的AppBar中添加背景图像?我知道条子但是当你向下滚动时,图像会被隐藏,而AppBar会改变颜色吗?所以我想知道这是否可能,如果没有,是否有任何现有的解决方法?谢谢!
我想要一个带有文本输入的背景图像,但是我不知道应该使用哪个小部件来避免键盘处于活动状态时背景图像缩小。
在这里,您可以找到问题的两个屏幕截图以及我的代码:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
var backgroundImage = new BoxDecoration(
image: new DecorationImage(
image: new AssetImage('assets/forest.jpg'), fit: BoxFit.cover));
return new MaterialApp(
home: new Scaffold(
body: new Stack(
children: <Widget>[
new Container(
decoration: backgroundImage,
),
new TextField()
],
)));
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试添加背景图像,无论哪条路线处于活动状态,该背景图像始终存在。我下面的示例受到这个答案的启发,但背景仅对路线“/”可见。我希望不必为每条路线设置背景图像。有什么建议么?
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: const BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/camping-background.png"),
fit: BoxFit.cover),
),
routes: <String, WidgetBuilder>{
'/login': (BuildContext context) => const Login(),
'/register': (BuildContext context) => const Register(),
'/home': (BuildContext context) => const Home(),
},
);
}
Run Code Online (Sandbox Code Playgroud)