Home > Software engineering >  Flutter function can not be called
Flutter function can not be called

Time:05-20

hello i want to create this function in which i have a row and a divider and i pass some parameters but i call this function in anther file it doesn't know it despite i did the import and it shows a worning in this function which is Dead code. Try removing the code, or fixing the code before it so that it can be reached.dartdead_code

  rowTable(String title, double thickness) {
    return Row(
      children: [
        Row(
          children: [
            Text(
              title,
              style: const TextStyle(
                  fontSize: 20,
                  fontFamily: 'SFProDisplay',
                  color: Color(0xFF131313)),
            ),
          ],
        ),
        Divider(
          color: Colors.grey[300],
          indent: 0,
          thickness: thickness,
        ),
      ],
    );
  }

i called the function inside a column

        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            rowTable(),
            // Row(
            //   children: const [
            //     Text(
            //       "Équipement ",
            //       style: TextStyle(
            //           fontSize: 20,
            //           fontFamily: 'SFProDisplay',
            //           color: Color(0xFF131313)),
            //     ),
            //   ],
            // ),
            // Divider(
            //   color: Colors.grey[300],
            //   indent: 0,
            //   thickness: 1.5,
            // ),
          ],
        ),

CodePudding user response:

I'm not sure if that's what you mean, can you please correct me, but if you want to put the same as you have commented your function is wrong because you encapsulate them in a Row, and the divider will not show below as you want, so I changed it as follows :

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppTheme.darkBlueBackground,
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          _rowTable("Équipement", 1.5),
        ],
      ),
    );
  }

  Widget _rowTable(String title, double thickness) {
    return Column( // change here cause its a column
      crossAxisAlignment: CrossAxisAlignment.start, // put here the .start
      children: [
        Text( // i delete the row also here
          title,
          style: const TextStyle(
              fontSize: 20,
              fontFamily: 'SFProDisplay',
              color: Color(0xFF131313)),
        ),
        Divider(
          color: Colors.grey[300],
          indent: 0,
          thickness: thickness,
        ),
      ],
    );
  }

On the other hand, I highly recommend you not to use functions, create another Widget for your rowTable and then you can call it from another :

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppTheme.darkBlueBackground,
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: const [
          RowTable(
            thickness: 1.5,
            title: 'Équipement',
          ),
        ],
      ),
    );
  }
}

class RowTable extends StatelessWidget {
  const RowTable({
    Key? key,
    required this.title,
    required this.thickness,
  }) : super(key: key);

  final String title;
  final double thickness;

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          title,
          style: const TextStyle(
              fontSize: 20,
              fontFamily: 'SFProDisplay',
              color: Color(0xFF131313)),
        ),
        Divider(
          color: Colors.grey[300],
          indent: 0,
          thickness: thickness,
        ),
      ],
    );
  }
}

CodePudding user response:

rowTable must be return List type.

You can define a List object and handle it like this

List<Widget> rowTable() { ... }

Column(
  crossAxisAlignment: CrossAxisAlignment.start,
  children: rowTable(),
),

or

Column(
  crossAxisAlignment: CrossAxisAlignment.start,
  children: [
    ...List.generate(
      2, // length
      (index) => Text("123"),
    ),
  ],
),
  • Related