好吧,我不想占便宜,不想發太多的問題,但我還有一個與flutter有關的問題。我想給我創建的容納ListView的卡片設定一個背景顏色,但只要我給容器提供任何屬性,就會反復出現這樣的錯誤:
════════ Exception caught by rendering library ═════════════════════════════════
RenderBox沒有被布置好。_RenderColoredBox#0caae relayoutBoundary=up1 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
'package:flutter/src/rendering/box.dart'。
失敗的斷言:行1930位置12。'hasSize'。
下面的代碼:
class ThisView extends StatefulWidget {
static const routeName = 'this-view';
@override
_ThisViewState createState() => _ThisViewState();
}
class _ThisViewState extends State< ThisView> {
@override>
Widget build(BuildContext context) {
return MaterialApp(
標題。'Title'。
首頁。Scaffold(
appBar: AppBar(
標題。Text("Title")。
centerTitle: true,
backgroundColor: Colors.Green)。
身體。容器(
color: Colors.gray,
孩子。列(
兒童。[
customCard1(),
容器()
// color: Colors.white,
孩子。擴展的(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(
左邊。24, right: 24, top: 28, bottom: 14)。)
孩子。文本(
"Subtitle"。
風格。TextStyle(
fontWeight: FontWeight.bold,
),
),
),
填充(
padding:
EdgeInsets.only(left: 24, right: 24, bottom: 8)。)
孩子。文本(
"23rd of Oct"。
風格。TextStyle(
fontWeight: FontWeight.w500,
),
),
),
擴展的(
孩子。ListView(
shrinkWrap: true。
padding: const EdgeInsets.all(8) 。
children: <Widget>[
容器(
高度。200,
color: Colors.mber[600]。
孩子。const Center(child: Text('Entry A'))。
),
容器(
高度。200。
color: Colors.amber[500]。
孩子。const Center(child: Text('Entry B'))。
),
容器(
高度。200。
color: Colors.amber[100]。
孩子。const Center(child: Text('Entry C'))。
),
],
),
)
]),
))
],
))),
);
}
}
我注釋了我想為卡片設定顏色的地方。 在未來,我打算動態渲染listView,所以它可能不會占據整個空間,所以我希望設定卡片的背景顏色,而不是將整個背景顏色設定為白色,只是在卡片之間放置一個灰色的SizeBox
。有誰知道當我試圖向容器添加屬性時,是什么導致了這個錯誤?
注意:我把這段代碼從我的一個自定義卡片Widget中取出來,這樣我就可以更容易地進行除錯,同時我也沒有把customCard1包括在這個代碼片段中。
如果需要更多的資訊,請讓我知道。
uj5u.com熱心網友回復:
容器需要知道它的尺寸。首先用MediaQuery類檢查你的設備尺寸,然后給出你想要的容器尺寸。
像這樣的例子
class ThisView extends StatefulWidget {
static const routeName = 'this-view';
_ThisViewState createState() => _ThisViewState();
}
class _ThisViewState extends State< ThisView> {
>
Widget build(BuildContext context) {
double screenHeight = MediaQuery.of(contex).size.height,
double screenWidth = MediaQuery.of(contex).size.width,
return MaterialApp(
標題。'Title'。
首頁。Scaffold(
appBar: AppBar(
標題。Text("Title")。
centerTitle: true,
backgroundColor: Colors.Green)。
身體。容器(
color: Colors.gray,
孩子。列(
兒童。[
customCard1(),
容器()
height :screenHeight/4。
width :screenWidth/2,
//color: Colors.white,
孩子。擴展的(
孩子。Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(
左邊。24, right: 24, top: 28, bottom: 14)。)
孩子。文本(
"Subtitle"。
風格。TextStyle(
fontWeight: FontWeight.bold,
),
),
),
填充(
padding:
EdgeInsets.only(left: 24, right: 24, bottom: 8)。)
孩子。文本(
"23rd of Oct"。
風格。TextStyle(
fontWeight: FontWeight.w500,
),
),
),
擴展的(
孩子。ListView(
shrinkWrap: true。
padding: const EdgeInsets.all(8) 。
children: <Widget>[
容器(
高度。200,
color: Colors.mber[600]。
孩子。const Center(child: Text('Entry A'))。
),
容器(
高度。200。
color: Colors.amber[500]。
孩子。const Center(child: Text('Entry B'))。
),
容器(
高度。200。
color: Colors.amber[100]。
孩子。const Center(child: Text('Entry C'))。
),
],
),
)
]),
))
],
))),
);
}
}
uj5u.com熱心網友回復:
請檢查,它對你有用嗎?
import 'package:flutter/material.dart'。
class ThisView extends StatefulWidget{
static const routeName = 'this-view';
@override
_ThisViewState createState() => _ThisViewState();
}
class _ThisViewState extends State< ThisView> {
@override>
Widget build(BuildContext context) {
return MaterialApp(
標題。'Title'。
首頁。Scaffold(
appBar: AppBar(
標題。Text("Title")。
centerTitle: true,
backgroundColor: Colors.green)。
身體。容器(
color: Colors.gray,
孩子。列(
兒童。[
customCard1(),
展開()
孩子。列()
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(
左邊。24, right: 24, top: 28, bottom: 14)。)
孩子。文本(
"Subtitle"。
風格。TextStyle(
fontWeight: FontWeight.bold,
),
),
),
填充(
padding:
EdgeInsets.only(left: 24, right: 24, bottom: 8)。)
孩子。文本(
"23rd of Oct"。
風格。TextStyle(
fontWeight: FontWeight.w500,
),
),
),
擴展的(
孩子。ListView(
shrinkWrap: true。
padding: const EdgeInsets.all(8) 。
children: <Widget>[
容器(
高度。200,
color: Colors.mber[600]。
孩子。const Center(child: Text('Entry A'))。
),
容器(
高度。200。
color: Colors.amber[500]。
孩子。const Center(child: Text('Entry B'))。
),
容器(
高度。200。
color: Colors.amber[100]。
孩子。const Center(child: Text('Entry C'))。
),
],
),
)
]),
)
],
))),
);
}
}
uj5u.com熱心網友回復:
>。不要使用容器來給背景上色,你可以使用Card widget的顏色屬性來改變卡片的背景顏色。所以像Card(color: Colors.white)。
uj5u.com熱心網友回復:
home: Scaffold(
backgroundColor: Color.fromRGBO(178, 186, 187, 1.0) 。
在fromRGBO中R-->紅,G--綠,B-->藍,O-->不透明度是這個顏色的alpha通道的雙數,0.0為透明,1.0為完全不透明。
https://htmlcolorcodes.com/參考此鏈接以獲得顏色組合。
這將設定背景顏色。你也可以使用
home: Scaffold(
backgroundColor: Colors.black,
或任何其他顏色。https://api.flutter.dev/flutter/material/Colors-class.html
完整的代碼:
class ThisView extends StatefulWidget {
static const routeName = 'this-view';
@override
_ThisViewState createState() => _ThisViewState();
}
class _ThisViewState extends State< ThisView> {
@override>
Widget build(BuildContext context) {
return MaterialApp(
標題。'Title'。
首頁。Scaffold(
backgroundColor: Color.fromRGBO(178, 186, 187, 1.0) 。
appBar: AppBar(
標題。Text("Title")。
centerTitle: true,
backgroundColor: Colors.Green)。
身體。容器(
color: Colors.gray,
孩子。列(
兒童。[
customCard1(),
容器()
// color: Colors.white,
孩子。擴展的(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(
左邊。24, right: 24, top: 28, bottom: 14)。)
孩子。文本(
"Subtitle"。
風格。TextStyle(
fontWeight: FontWeight.bold,
),
),
),
填充(
padding:
EdgeInsets.only(left: 24, right: 24, bottom: 8)。)
孩子。文本(
"23rd of Oct"。
風格。TextStyle(
fontWeight: FontWeight.w500,
),
),
),
擴展的(
孩子。ListView(
shrinkWrap: true。
padding: const EdgeInsets.all(8) 。
children: <Widget>[
容器(
高度。200,
color: Colors.mber[600]。
孩子。const Center(child: Text('Entry A'))。
),
容器(
高度。200。
color: Colors.amber[500]。
孩子。const Center(child: Text('Entry B'))。
),
容器(
高度。200。
color: Colors.amber[100]。
孩子。const Center(child: Text('Entry C'))。
),
],
),
)
]),
))
],
))),
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/315716.html
標籤:
