Sankha Jayalath
Sankha Jayalath

Reputation: 785

Add a Raised Button inside a card widget in flutter

I have this bottom navigation bar item that has cards as its column widget children. I am trying to pass a raised button inside my card as the trailing attribute but the onPressed() functions give an error on initializing. It says "invalid constant value".

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'), 
                      ),
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    )),

];

Upvotes: 0

Views: 3734

Answers (2)

Dhaval Patel
Dhaval Patel

Reputation: 409

I think you can try the below code:-

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

Upvotes: 1

Juan V
Juan V

Reputation: 508

The problem is trying to construct the ListTile with const when you have a RaisedButton inside the tree, simply removing the const keyword right before ListTile will fix the issue.

If you want to use const anyway you can do:

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

Upvotes: 3

Related Questions