gpt4 book ai didi

flutter - 如何在 Flutter 中设置 Row() 的背景颜色?

转载 作者:IT王子 更新时间:2023-10-29 07:18:52 33 4
gpt4 key购买 nike

我正在尝试为 Row() 小部件设置背景颜色,但 Row 本身没有背景颜色或颜色属性。我已经能够将容器的背景颜色设置为灰色,就在紫色背景文本之前,但是文本本身并没有完全填充背景,并且下面的间隔符根本没有任何颜色。

那么如何将行背景设置为“HexColor(COLOR_LIGHT_GREY)”值,使其跨越整行?

有什么想法吗?非常感谢!

enter image description here

这是我目前的代码:

import 'package:flutter/material.dart';
import '../manager/ShoppingListManager.dart';
import '../model/ShoppingListModel.dart';
import '../hexColor.dart';
import '../Constants.dart';

class ShoppingListWidget extends StatelessWidget {
final Color color = Colors.amberAccent;
final int shoppingListIndex;

ShoppingListWidget({this.shoppingListIndex});

@override
Widget build(BuildContext context) {
ShoppingListManager slm = new ShoppingListManager();
String shoppingListName =
slm.myShoppingLists.shoppingLists[shoppingListIndex].name;
int categoryCount =
slm.myShoppingLists.shoppingLists[shoppingListIndex].categories.length;

return Scaffold(
appBar: AppBar(
title: Text(shoppingListName),
automaticallyImplyLeading: true,
),
body: ListView.builder(
itemBuilder: (context, index) {
Category cat = slm.myShoppingLists.shoppingLists[shoppingListIndex]
.categories[index];

return Container(
decoration: new BoxDecoration(
border: new Border.all(color: Colors.grey[500]),
color: Colors.white,
),
child: new Column(
children: <Widget>[
getCategoryWidget(context, cat),
getCategoryItems(context, cat),
],
),
);
},
itemCount: categoryCount,
),
);
}

// Render the category "headline" row where I want to set the background color
// to HexColor(COLOR_LIGHT_GREY)
Widget getCategoryWidget(BuildContext context, Category cat) {
return new Row(
children: <Widget>[
new Container(height: 40.0, width: 10.0, color: HexColor(cat.color)),
new Container(
height: 40.0, width: 15.0, color: HexColor(COLOR_LIGHT_GREY)),
new Container(
child: new Text("Category", textAlign: TextAlign.start,
style: TextStyle(
fontFamily: 'Bold',
fontSize: 18.0,
color: Colors.black),
),
decoration: new BoxDecoration(
color: Colors.purple,
),
height: 40.0,
),
Spacer(),

CircleAvatar(
backgroundImage:
new AssetImage('assets/icons/food/food_settings.png'),
backgroundColor: HexColor(COLOR_LIGHT_GREY),
radius: 15.0,
),
new Container(height: 15.0, width: 10.0, color: Colors.transparent),
],
);
}

// render the category items
Widget getCategoryItems(BuildContext context, Category cat) {
return ListView.builder(
itemBuilder: (context, index) {
String itemName = "Subcategory";
return new Row(children: <Widget>[
new Container(height: 40.0, width: 5.0, color: HexColor(cat.color)),
new Container(height: 40.0, width: 20.0, color: Colors.white),
new Container(
child: new Text(itemName),
color: Colors.white,
),
Spacer()
]);
},
itemCount: cat.items.length,
shrinkWrap: true,
physics:
ClampingScrollPhysics(),
);
}

}

最佳答案

只需使用具有如下颜色属性的容器包装您的行:

   Container(
color: Colors.black,
child: Row(
children: <Widget>[
Expanded(
child: Text('Demo', style: TextStyle(color: Colors.white),),
)
],
),
)

关于flutter - 如何在 Flutter 中设置 Row() 的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56410074/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com