Home > Software engineering >  How can Widget Classes Arguments be used as props to create dynamic Widgets with Row and Columns in
How can Widget Classes Arguments be used as props to create dynamic Widgets with Row and Columns in

Time:09-08

I am revisiting Flutter development after working with React after some times and I was trying to implement something as passing props across multiple components, in order to create dynamic Widgets (components) that render based on props. I could somehow achieve the result but still I cannot understand something.

So, in this example, I am creating a Widget Class which accepts text as an argument, and I am able to use the value of the text below in the Text return method:


class NeoText extends StatelessWidget {
  final String text;

  const NeoText({ 
    super. Key,
    required this.text,
  });

  @override
  Widget build(BuildContext context) {
    return Text(
      text,
      style: const TextStyle(
        color: Colors.white,
        fontSize: 15.0,
        fontWeight: FontWeight.bold,
        letterSpacing: 1.0,
        fontFamily: 'Lato',
      ),
    );
  }
}

And I can use the Widget (component) that I created in my main.dart file like:

...
...
    child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: const [
                  NeoText(
                    text: "⚽️Goals: 2",
                  ),
                  NeoText(
                    text: "           
  • Related