skip to Main Content

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

3

Answers


  1. 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'),
            //       ],
            //     ),
            //   )
          ),
        );
      }
    }
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search