【发布时间】:2023-01-24 01:53:34
【问题描述】:
我想在背景中添加图像。我在 StackOverflow 中找到了这段代码 sn-p。
SizedBox.expand(
// -> 01
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
'https://wallpapercave.com/wp/wp5561184.jpg'),
fit: BoxFit.cover, // -> 02
),
),
),
),
这在这段代码中对我来说效果很好:
enter codeimport 'package:flutter/material.dart';
class Welcome extends StatelessWidget {
final int _picState;
final VoidCallback _picStateChange;
final Function _quizStarted;
Welcome(this._quizStarted, this._picState, this._picStateChange);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
alignment: AlignmentDirectional.bottomCenter,
children: [
SizedBox.expand(
// -> 01
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
'https://wallpapercave.com/wp/wp5561184.jpg'),
fit: BoxFit.cover, // -> 02
),
),
),
),
SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Spacer(),
Text(
'Start Application',
style: Theme.of(context).textTheme.headline3?.copyWith(
color: Colors.white54,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
Spacer(),
Spacer(),
TextField(
onTap: () => _picStateChange(),
decoration: InputDecoration(
focusColor: Colors.amber,
filled: true,
fillColor: Colors.white.withOpacity(0.2),
hintText: 'ENTER YOUR NAME HERE',
hintStyle: TextStyle(
wordSpacing: 2,
fontWeight: FontWeight.bold,
color: Colors.white,
decoration: TextDecoration.underline),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(12))),
),
),
InkWell(
onTap: () => _quizStarted(),
child: Container(
width: double.infinity,
alignment: Alignment.center,
padding: EdgeInsets.all(15),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
gradient: (LinearGradient(
colors: [Colors.lime, Colors.blueGrey.shade700],
begin: Alignment.topRight,
end: Alignment.bottomLeft))),
child: Text('START THE APPLICATION',
style: Theme.of(context)
.textTheme
.button
?.copyWith(color: Colors.white38)),
),
),
Spacer(),
],
),
))
],
),
);
}
}
但是当我在另一部分(下面的部分)中使用相同的代码时,错误“断言失败:第 1927 行第 12 行:'hasSize'“出现
**import 'package:flutter/material.dart';
class Answer extends StatelessWidget {
final VoidCallback ansk;
final String gimme_Da_Answer;
Answer(this.ansk, this.gimme_Da_Answer);
@override
Widget build(BuildContext context) {
return Stack(
alignment: AlignmentDirectional.bottomCenter,
children: [
SizedBox.expand(
// -> 01
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
'https://wallpapercave.com/wp/wp5561184.jpg'),
fit: BoxFit.fitHeight, // -> 02
),
),
),
),
SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 18, horizontal: 17),
child: InkWell(
onTap: () => ansk(),
child: (Container(
child: Text(gimme_Da_Answer,textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25,
color: Colors.cyan.shade800,
fontWeight: FontWeight.bold)),
width: double.infinity,
padding: EdgeInsets.all(15),
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
border: Border.all(color: Colors.blue, width: 4),
color: Colors.blueAccent.shade100.withOpacity(0.4),
borderRadius: BorderRadius.all(Radius.circular(15))),
)),
),
),
),
]);
}
}**
^为我显示错误的代码 多谢!!!
【问题讨论】:
-
用
material小部件包裹你的堆栈,比如Scaffold