0

I have got a Dropdown Button that shows app languages when pressed, and when anyone is selected the app language is changed. Now I need to convert this Dropdown Button to listview. builder so that the languages are shown as a list instead of a dropdown, but could not do that. I will appreciate it if someone can help.

This is my code:

Languages class

class Language {
  final int id;
  final String flag;
  final String name;
  final String languageCode;

  Language(this.id, this.flag, this.name, this.languageCode);

  static List<Language> languageList() {
    return <Language>[
      Language(1, "🇺🇸", "English", "en"),
      Language(2, "🇫🇷", "français", "fr"),
    ];
  }
}

Dropdown Button

DropdownButton<Language>(
  underline: SizedBox(),
  icon: Icon(
  Icons.language,
  color: Colors.blue,
),
onChanged: (Language language) {
  _changeLanguage(language);
},
items: Language.languageList().map<DropdownMenuItem<Language>>(
  (e) => DropdownMenuItem<Language>(
     value: e,
     child: Row(
     mainAxisAlignment: MainAxisAlignment.spaceAround,
     children: <Widget>[
     Text(
     e.flag,
     style: TextStyle(fontSize: 30),
   ),
  Text(e.name)
  ],),
 ),
).toList(),
), 
1
  • What is not working? Show your ListView.builder implementation. Commented Feb 15, 2021 at 17:24

2 Answers 2

1

This should do what you need.

ListView.builder(
        shrinkWrap: true,
        itemCount: Language.languageList().length,
        itemBuilder: (context, index) {
Language language = Language.languageList()[index];
          return GestureDetector(
onTap:(){
_changeLanguage(language)
},
child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: <Widget>[
                            Text(
                              e.flag,
                              style: TextStyle(fontSize: 30),
                            ),
                            Text(e.name)
                          ],
                        )
);
        },
      )
Sign up to request clarification or add additional context in comments.

Comments

1

I agree with @Tirth that it's not clear what exactly is not working. However, with what you have provided here is a reusable ListView.builder that will display your languages.

class LanguageList extends StatelessWidget {
  final List<Language> languageList;

  const LanguageList({Key key, this.languageList}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: ListView.builder(
        itemCount: languageList.length,
        itemBuilder: (context, index) {
          return Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Text(languageList[index].id.toString()),
              Text(languageList[index].name),
              Text(languageList[index].flag),
              Text(languageList[index].languageCode),
            ],
          );
        },
      ),
    );
  }
}

Then you could put this where ever you need it in your UI

LanguageList(languageList: Language.languageList())

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.