0

I'm trying to turn a list of strings into a list of widgets, which are generated in a previous widget dynamically through a text edit / text edit controller.

I've pulled the dynamic list names into a new stateful widget.

class NameMachine extends StatefulWidget {
  final List<String> names;

  NameMachine({Key key, @required this.names}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _NameMachineState(names);
  }
}

class _NameMachineState extends State<NameMachine> {
  final List<String> names;
 @override
  Widget build(BuildContext context) {

I've then tried to create a list of widgets, but encounter issues with static members and initalisers.

final List<Widget> nameSlots = ...(names)
      .map(
        (names) => Container(
          padding: EdgeInsets.all(4.0),
          color: Colors.white,
          child: Text(
            names,
            textScaleFactor: 1.2,
            textAlign: TextAlign.center,
          ),
          width: double.infinity,
          height: double.infinity,
        ),
      )
      .toList();
2

2 Answers 2

2

Try this:

import "package:flutter/material.dart";

class NameMachine extends StatefulWidget {
  final List<String> names;

  NameMachine({Key key, @required this.names}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _NameMachineState();
}

class _NameMachineState extends State<NameMachine> {
 List<Widget> nameSlots;
 @override
 void initState(){
    super.initState();

    //the code below takes the argument passed to the constructor and 
    //map each item of the list to a Container widget and stores the result in nameSlots

    nameSlots = widget.names 
    .map(
    (names) => Container(
      padding: EdgeInsets.all(4.0),
      color: Colors.white,
      child: Text(
        names,
        textScaleFactor: 1.2,
        textAlign: TextAlign.center,
      ),
      width: double.infinity,
      height: double.infinity,
    ),
  )
  .toList();
 }
 @override
  Widget build(BuildContext context) {
        //some code
    }
}


What we do here is using the map method on the list not each item of the list (...(names) takes each item of the list) and map each item to a widget.

Sign up to request clarification or add additional context in comments.

1 Comment

Your welcome. Please accept the answer so others can use it too! Thanks,
0
class NameMachine extends StatefulWidget {
  final List<String> names;

  NameMachine({Key key, @required this.names}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _NameMachineState();
}

class _NameMachineState extends State<NameMachine> {
 List<Widget> names;
 @override
 void initState(){
    super.initState();
    names = widget.names
    .map(
    (names) => Container(
      padding: EdgeInsets.all(4.0),
      color: Colors.white,
      child: Text(
        names,
        textScaleFactor: 1.2,
        textAlign: TextAlign.center,
      ),
      width: double.infinity,
      height: double.infinity,
    ),
  )
  .toList();
 }
 @override
  Widget build(BuildContext context) {

There might be some syntax errors in my but this might help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.