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

Mar*_*ltz 2 dart flutter

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

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

任何的想法?非常感谢!

在此处输入图片说明

这是我到目前为止的代码:

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(),
    );
  }

}
Run Code Online (Sandbox Code Playgroud)

Cop*_*oad 13

更新:

从 Flutter 2.0 开始,Container优化并提供与常规ColoredBox.

将你的包裹Row在 a 中Container并给它一个color.

Container(
  color: Colors.red,
  child: Row(children: []),
)
Run Code Online (Sandbox Code Playgroud)

老的:

使用ColoredBox代替 aContainer以提高效率。

使用 Container 会产生一个使用 BoxDecoration 来实际绘制背景颜色的小部件层次结构。BoxDecoration 小部件涵盖了许多情况,而不仅仅是绘制背景颜色,并且不如新的 ColoredBox 小部件高效,后者只绘制背景颜色。 来源

ColoredBox(
  color: Colors.red,
  child: Row(children: []),
)
Run Code Online (Sandbox Code Playgroud)


nic*_*tdr 7

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

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

  • 这将阻止材质墨水飞溅,因为“容器”的背景绘制在它们之上。最好使用“Ink”而不是“Container”。 (4认同)
  • ```墨水(颜色:const Color(0xfffce700),子项:子项,)``` (3认同)