Exercise : Mapping Data to Widgets

Try to map the list of countries with the capital city to Widgets using ListView, CircleAvatar and ListTile.

Country By Capital : Download

The Result :

Exercise : Mapping Data to Widgets Flutter






The Solution :

import 'package:flutter/material.dart';

import './country_by_capital_city.dart';

 

void main() {

  runApp(const MyApp());

}

 

class MyApp extends StatelessWidget {

  const MyApp({Keykey}) : super(keykey);

 

  @override

  Widget build(BuildContext context) {

    return MaterialApp(

      title'mapping_data_to_widgets_exercice',

      themeThemeData(

        primarySwatchColors.blue,

      ),

      homeconst MyHomePage(),

    );

  }

}

 

class MyHomePage extends StatefulWidget {

  const MyHomePage({Keykey}) : super(keykey);

 

  @override

  _MyHomePageState createState() => _MyHomePageState();

}

 

class _MyHomePageState extends State<MyHomePage> {

  @override

  Widget build(BuildContext context) {

    return Scaffold(

      appBarAppBar(

        titleconst Text("Country By Capital City"),

      ),

      bodyContainer(

        marginconst EdgeInsets.all(8),

        childListView.builder(

          itemBuilder: (ctxindex) {

            return ListTile(

              leadingCircleAvatar(

                childText(data[index]["country"].toString().substring(02)),

              ),

              titleText(data[index]["country"].toString()),

              subtitleText(data[index]["city"].toString()),

              trailingText((index + 1).toString()),

            );

          },

          itemCountdata.length,

        ),

      ),

    );

  }

}