Home > Back-end >  Too many postival arguments when adding hero transition, image, and text
Too many postival arguments when adding hero transition, image, and text

Time:11-06

import 'package:flutter/material.dart';

class DetailsScreen extends StatefulWidget {
  final int index;

  const DetailsScreen({Key? key, required this.index}) : super(key: key);

  @override
  State<DetailsScreen> createState() => _DetailsScreenState();
}

class _DetailsScreenState extends State<DetailsScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      
      

      body: Center(
        child: Hero(
          tag: widget.index,
           

           
          child: Image.network(
            "https://raw.githubusercontent.com/markknguyen/pictures/master/pic/${widget.index   1}.png",
             
          ),
          const Text("Rome"),
        ),
      ),
    );
  
  }
}

I tried adding const thinking it will resolve the issue but I didn't. The code did not run. I Just wanted to add some sort of text box in a page. const Text("Rome"), is the main concern.

CodePudding user response:

You can't just have your Text widget there with no parent. You need to put your Hero and Text widget in a Column like so:

   Center(
        child: Column(
          children: [
            Hero(
              tag: widget.index,
              child: Image.network(
                "https://raw.githubusercontent.com/markknguyen/pictures/master/pic/${widget.index   1}.png",
              ),
            ),
            Text("Rome"),
          ],
        ),
      ),

or any other Widget that acceptes multiple children such as Row or ListView based on your needs

CodePudding user response:

The issue is that your Text() widget isn't passed as a parameter. Currently, the code can't compile due to treating your Text("Rome") as a 'mistake', sort of speaking.

Depending on your use-case scenario, you can either use:

  • a Column() - if you want your widgets to be one after another in a column.
  • a Stack() - if you want your widgets to be placed one under another.

You'll have to pass the children attribute to both, so for example:

    Column(
        children: [
            Image.network(
   "https://raw.githubusercontent.com/markknguyen/pictures/master/pic/${widget.index   1}.png",
                 
                      ),
            const Text("Rome"),
        ]
    )
  • Related