在卡片小部件中添加一个凸起按钮

时间:2019-11-16 03:11:34

标签: flutter mobile

我有一个底部导航栏项目,其中有卡片作为其列小部件子项。我试图将卡上的凸起按钮作为尾随属性传递,但是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'), 
                      ),
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    )),

];

2 个答案:

答案 0 :(得分:0)

我认为您可以尝试以下代码:-

ListTile(
  title: Text('Fine ID: 3265'),
  subtitle: Text(
      'Fines: Crossing double line\novertaking on pedestrian crossing'),
  trailing: Icon(
      Icons.keyboard_arrow_right, color: Colors.green, size: 25.0),
  //Here you can not set other widget.
  onTap: () {
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => ArtistDetail(show: show)));
  },
);

答案 1 :(得分:0)

问题是当树中有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'),
        ),
      ),
    );
  }
}