【问题标题】:Changing Card height and width更改卡片高度和宽度
【发布时间】:2020-01-23 06:55:28
【问题描述】:

我正在尝试设置卡片的高度和宽度,如下所示,但它返回错误:高度和宽度均未定义“命名参数”。这是设置卡片高度和宽度的错误方法吗?

小部件构建(BuildContext 上下文){ 最终 ThemeData 主题 = Theme.of(context);

    return new WillPopScope(
      onWillPop: () => _onBackPressed(context),
      child: Scaffold(
        appBar: new AppBar(
          title: new Text(
            'Finding your pizza',
            style: theme.textTheme.title,
          ),
        ),
        body: new Card(
          width: MediaQuery.of(context).size.width/60,
          height: MediaQuery.of(context).size.height/60,
          child: new Center(
            child: new Column(
              children: <Widget>[

【问题讨论】:

  • 您可以将 Card 用作 Container 的子级并为该 Container 设置宽度、高度

标签: flutter dart


【解决方案1】:

如果容器是多余的,您也可以使用SizedBox 小部件来设置宽度和高度。

final double cardWidth = MediaQuery.of(context).size.width/60;
final double cardHeight = MediaQuery.of(context).size.height/60,
Widget build(BuildContext context) {
...
   SizedBox(
    width:  cardWidth,
    height: cardHeight,
    child: Card(child: Text('Hello World!')),
  ),
...
}

【讨论】:

    【解决方案2】:

    根据文档,Card 类既不包含 height 也不包含 width 命名参数。

    您可以使用Container,它同时具有heightwidth

    Widget build(BuildContext context) {
      return Center(
        child: Card(
          child: InkWell(
            splashColor: Colors.blue.withAlpha(30),
            onTap: () {
              print('Card tapped.');
            },
            child: Container(
              width: 300,
              height: 100,
              child: Text('A card that can be tapped'),
            ),
          ),
        ),
      );
    }
    

    【讨论】:

      猜你喜欢
      • 2014-09-26
      • 2021-02-01
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-12
      相关资源
      最近更新 更多