【问题标题】:Add a Raised Button inside a card widget in flutter在颤动的卡片小部件内添加一个凸起的按钮
【发布时间】:2020-03-12 04:59:13
【问题描述】:

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

];

【问题讨论】:

    标签: flutter mobile


    【解决方案1】:

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

    【讨论】:

    • RaisedButton 的替代品是 ElevatedButton
    【解决方案2】:

    我想你可以试试下面的代码:-

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-25
      • 2019-06-27
      • 2021-10-02
      • 1970-01-01
      • 2020-08-26
      • 2022-07-20
      • 2020-09-06
      • 1970-01-01
      相关资源
      最近更新 更多