Skip to content

Instantly share code, notes, and snippets.

@hongsw
Last active October 10, 2023 12:35
Show Gist options
  • Save hongsw/ad4ce2e72a4773d600e9ddb22ce242e9 to your computer and use it in GitHub Desktop.
Save hongsw/ad4ce2e72a4773d600e9ddb22ce242e9 to your computer and use it in GitHub Desktop.
반복된 요소를 새로운 위젯으로 만들기

반복된 요소를 새로운 위젯으로 만들기

Created with <3 with dartpad.dev.

import 'package:flutter/material.dart';
// 참조 : https://api.flutter.dev/flutter/material/ListTile-class.html#material.ListTile.3
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: AnimalListPage(),
);
}
}
class AnimalListPage extends StatelessWidget {
final List<Map<String, String>> animals = [
{'name': 'Dog', 'imageUrl': 'https://cataas.com/cat?width=100'},
{'name': 'Cat', 'imageUrl': 'https://cataas.com/cat?width=100'},
{'name': 'Elephant', 'imageUrl': 'https://cataas.com/cat?width=100'},
// ... add more animals if needed
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Animal List'),
),
body: ListView.builder(
itemCount: animals.length,
itemBuilder: (context, index) {
return AnimalCard(
name: animals[index]['name']!,
imageUrl: animals[index]['imageUrl']!,
);
},
),
);
}
}
class AnimalCard extends StatelessWidget {
final String name;
final String imageUrl;
AnimalCard({required this.name, required this.imageUrl});
@override
Widget build(BuildContext context) {
return Card(
child: ListTile(
leading: Image.network(imageUrl),
title: Text(name),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment