Home > OS >  How to add multiple children to flutter body
How to add multiple children to flutter body

Time:06-08

Good day, I need help adding a second child to the body of my screen. I keep on getting the error "The argument for the named parameter 'children' was already specified." If I take that piece of code out, my 'app' works perfectly. I've tried adding Column to my body (saw it in a different question) but it still gives me the error.

The problematic code is

,children: [TextButton(
              onPressed: () {
                Navigator.push(context, MaterialPageRoute(builder: (context) {
                   return const FeatureScreenDos(title: 'Feature Screen dos');

Full Code:

class DashBoard extends StatelessWidget {
  const DashBoard({Key? key, required this.title}) : super(key: key);
  final String title;
  @override
   Widget build(BuildContext context){
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: Column(
        children: [TextButton(
          onPressed: () {
            Navigator.push(context, MaterialPageRoute(builder: (context) {
                return const FeatureScreenUno(title: 'Feature Screen uno');
                  }));
                    },
                  child: const Text('Feature Screen uno')
        )
           ]
        ,children: [TextButton(
              onPressed: () {
                Navigator.push(context, MaterialPageRoute(builder: (context) {
                   return const FeatureScreenDos(title: 'Feature Screen dos');
   }));
            }
            ,child: const Text('Feature Screen dos'),
          ),
               ]
        )
           
             
                           
               
        );

      
    
    
  }
}`

CodePudding user response:

adding multiple children to Column or Row or any other multi child widget is not like that,
you would have a single Column with a list of children inside it

Column(
    children: [
        const Text('Child 1'),
        const Text('Child 2'),
        ...
    ]
)

please refer to Column widget to know more about column, same work goes to row & some other multi child widgets

CodePudding user response:

        class DashBoard extends StatelessWidget {
          const DashBoard({Key? key, required this.title}) : super(key: key);
          final String title;
          @override
           Widget build(BuildContext context){
            return Scaffold(
              appBar: AppBar(
                title: Text(title),
              ),
              body: Column(
                children: [
///child one
                TextButton(
                  onPressed: () {
                    Navigator.push(context, MaterialPageRoute(builder: (context) {
                        return const FeatureScreenUno(title: 'Feature Screen uno');
                          }));
                            },
                          child: const Text('Feature Screen uno')
                ),
///child two
                      TextButton(
                      onPressed: () {
                        Navigator.push(context, MaterialPageRoute(builder: (context) {
                           return const FeatureScreenDos(title: 'Feature Screen dos');
           }));
                    }
                    ,child: const Text('Feature Screen dos'),
                  ),
                   ]
                
                )
                   
                     
                                   
                       
                );
        
              
            
            
          }
        }`

body will always take one child we customize further ourselfs by adding column and rows for example here you can add a row as well in children if you want to show something horizontal after text buttons and further add more children to rows

  • Related