0

I want the text and icon together in a line under one widget in body. I tried and I got the following error

Too many positional arguments, 0 expected but 1 received

Can Any one help me with this? Thanks in advance!

void main() => runApp(const Myapp());

class Myapp extends StatefulWidget {
  const myapp({Key? key}) : super(key: key);
  _MyappState createState() => _MyappState();
}

class _MyappState extends State<Myapp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Padding(
          padding: EdgeInsets.only(top: 30),

          child: Stack(
            alignment: AlignmentDirectional.center,

            children: <Widget>[
              /** Positioned WIdget **/

              Positioned(
                top: 0.0,
                child: Image.asset(
                  'images/logo.png',
                  height: 150,
                  width: 150,
                ),
              ),
              //Positioned

              /** Positioned WIdget **/

              //Positioned
            ], //<Widget>[]
          ), //Stack
        ),

        Text.rich(
  TextSpan(
    children: [
      TextSpan(text: 'Click'),
      WidgetSpan(child: Icon(Icons.add)),
      TextSpan(text: 'to add'),
    ],
  ),
)
        
      ),
    );
  }
}
Harsh Sureja
  • 1,052
  • 1
  • 9
  • 22
Priya
  • 67
  • 6

3 Answers3

0

First, the error "Too many positional arguments, 0 expected but 1 received", is due to the fact that the placement of RichText is outside the Stack Widget children. (The square brackets [].)

To achieve the desired output as you mentioned in the question, use a row specified in code comments with the attached code.

void main() => runApp(const Myapp());

class Myapp extends StatefulWidget {
  const Myapp({Key? key}) : super(key: key);
  @override
  _MyappState createState() => _MyappState();
}

class _MyappState extends State<Myapp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Padding(
          padding: const EdgeInsets.only(top: 30),

          child: Stack(
            alignment: AlignmentDirectional.center,

            children: const <Widget>[
              /** Positioned WIdget **/
              // Positioned(
              //   top: 0.0,
              //   child: Image.asset(
              //     'images/logo.png',
              //     height: 150,
              //     width: 150,
              //   ),
              // ),

              /** Positioned WIdget **/
              
              /// This is where the RichTextWidget should be
              Text.rich(
                TextSpan(
                  children: [
                    TextSpan(text: 'Click'),
                    WidgetSpan(child: Icon(Icons.add)),
                    TextSpan(text: 'to add'),
                  ],
                ),
              ),
              /// Additionally, use a Row([Text,Icon,Text]) and use
              /// crossAxisAlignment Property to align the children
            ], //<Widget>[]
          ), //Stack
        ),
        /// You placed the below RickText outside the Stack, it should be above in the Widget[] of the stack.
        //  Text.rich(
        //   TextSpan(
        //     children: [
        //       TextSpan(text: 'Click'),
        //       WidgetSpan(child: Icon(Icons.add)),
        //       TextSpan(text: 'to add'),
        //       ],
        //     ),
        //   )
      ),
    );
  }
}
Ashish
  • 35
  • 5
  • If I try text code in 'stack' or 'padding', the text is placed over the image. But I want the text under the image. Can you help me with this? – Priya Feb 10 '23 at 07:31
  • @Priya Use the Column Widget in place of the Stack Widget – Ashish Feb 10 '23 at 08:40
0

If you want to place a button with title and icon then you can use TextButton.icon instead of putting it inside a row or putting inside RichText.

Here's the button example:

class DemoWidget extends StatelessWidget {
  const DemoWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: TextButton.icon(
          onPressed: () {},
          icon: const Icon(Icons.mail),
          label: const Text("Title"),
        ),
      ),
    );
  }
}

Then you can put it inside a stack and assign a background image in it. You can use Positioned.fill and then try modifying the parameters until it matches your requirment.

Cavin Macwan
  • 1,183
  • 5
  • 12
0
caffold(
      body: Center(
        child: Container(color: Colors.lightGreen,
          height: 200,
          width: 200,
          child: Stack(
            alignment: AlignmentDirectional.center,
            children:  <Widget>[
              Positioned(child: Image.asset("assets/test.png")),
              const Text.rich(
                TextSpan(
                  children: [
                    TextSpan(text: 'Click'),
                    WidgetSpan(child: Icon(Icons.add)),
                    TextSpan(text: 'to add'),
                  ],
                ),
              ),
            ], //<Widget>[]
          ),
        ),
      ),
    )

See Result

Sachin Kumawat
  • 297
  • 1
  • 7