首页 > 解决方案 > 在颤动的卡片小部件内添加一个凸起的按钮

问题描述

我有这个底部导航栏项目,它有卡片作为它的列小部件子项。我试图将卡片内的凸起按钮作为尾随属性传递,但 onPressed() 函数在初始化时出错。它说“无效的常数值”。

List _widgetOptions = <Widget>[
Scaffold(
    appBar: AppBar(
      title: Text("Fines"),
    ),
    body: Container(
      child: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            Card(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  const ListTile(
                    title: Text('Fine ID: 3265'),
                    subtitle: Text('Fines: Crossing double line\novertaking on pedestrian crossing'),
                    trailing: RaisedButton(
                      onPressed: () {},//**this line is underlined in red. Error is here**
                      color: Colors.green,
                      child: Text('Pay'), 
                      ),
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    )),

];

标签: fluttermobile

解决方案


问题是在树中有 RaisedButton 时尝试使用 const 构造ListTile,只需在ListTile解决问题之前删除const关键字即可。

如果你想使用const,你可以这样做:

import 'package:flutter/material.dart';

void main() {
  return runApp(
    MaterialApp(
      home: Scaffold(
        body: TestPage(),
      ),
    ),
  );
}

class TestPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: ListTile(
        title: const Text('Fine ID: 3265'),
        subtitle: const Text(
            'Fines: Crossing double line\novertaking on pedestrian crossing'),
        trailing: RaisedButton(
          onPressed: () {}, //**this line is underlined in red. Error is here**
          color: Colors.green,
          child: const Text('Pay'),
        ),
      ),
    );
  }
}

推荐阅读