순한맛 시즌2-3 리스트뷰(ListView)에서 데이터 전달하고 페이지 이동하기
- Generate
: length의 길이만큼 0부터 인덱스-1까지의 범위에 각 인덱스를 오름차순으로 호출해서 만든 값으로 리스트 생성
예시1) int 타입 3개를 호출함. 0, 1, 2.
그런데 이 호출한 값을 index * index 하고 출력하라는 조건이 주어져서 해당 식을 수행한 후 출력 결과 = 0, 1, 4
- LikeButton
like_button | Flutter Package
Like Button is a flutter library that allows you to create a button with animation effects similar to Twitter's heart when you like something.
pub.dev
전체 코드
// main.dart
import 'package:flutter/material.dart';
import 'anmal_page.dart';
import 'model.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyPage(),
);
}
}
class MyPage extends StatefulWidget {
const MyPage({Key? key}) : super(key: key);
@override
State<MyPage> createState() => _MyPageState();
}
class _MyPageState extends State<MyPage> {
static List<String> animalName = [
'Pig',
'Duck',
'Cat',
'Cow',
'Goat',
'Chicken',
'Sheep',
'horse',
'Rabbit'
];
static List<String> animalImagePath = [
'image/1.PNG',
'image/2.PNG',
'image/3.PNG',
'image/4.PNG',
'image/5.PNG',
'image/6.PNG',
'image/7.PNG',
'image/8.PNG',
'image/9.PNG',
];
static List<String> animalLocation = [
'mountain',
'river',
'house',
'mountain',
'ground',
'farm',
'mountain',
'home',
'river'
];
final List<Animal> animalData = List.generate(
animalLocation.length,
(index) => Animal(
animalName[index], animalLocation[index], animalImagePath[index]));
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('ListView'),
),
body: ListView.builder(
itemCount: animalData.length,
itemBuilder: (context, index) {
return Card(
child: ListTile(
title: Text(
animalData[index].name,
),
leading: SizedBox(
height: 50,
width: 50,
child: Image.asset(
animalData[index].imgPath,
),
),
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => AnimalPage(animal: animalData[index],)));
debugPrint(animalData[index].name);
},
),
);
},
),
);
}
}
// model.dart
class Animal{
final String name;
final String imgPath;
final String location;
Animal(this.name, this.location, this.imgPath);
}
//anmal_page
//AppBar => 동물이름
//body => 큰 동물 이미지
// => 동물 서식지
import 'package:flutter/material.dart';
import 'model.dart';
import 'package:like_button/like_button.dart';
class AnimalPage extends StatefulWidget {
const AnimalPage({Key? key, required this.animal}) : super(key: key);
//AnimalPage 클래스는 컬리 브레이스를 가지고 있음.
//AnimalPage 클래스는 선택에 의해서 구현되는 named argument를 가지고 있음
//named argument인 animal argument 앞에 required 키워드가 붙어있기 때문에 반드시 구현을 해야함
final Animal animal;
@override
State<AnimalPage> createState() => _AnimalPageState();
}
class _AnimalPageState extends State<AnimalPage> {
bool isLiked = false;
int likeCount = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.animal.name),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
height: 200,
width: 200,
child: Image.asset(widget.animal.imgPath)
),
const SizedBox(
height: 20,
),
Text(
'It lives in ' + widget.animal.location,
style: const TextStyle(
fontSize: 18,
),
),
const SizedBox(
height: 20,
),
LikeButton(
size: 30,
isLiked: isLiked,
likeCount: likeCount,
)
],
),
),
);
}
}
반응형
'공부 > Flutter' 카테고리의 다른 글
Flutter 스터디 16 Stateful Widget (0) | 2023.02.05 |
---|---|
Flutter 스터디 15 반응형 레이아웃 (0) | 2023.02.02 |
Flutter 스터디 13 플러터 ListView builder와 Dialog (0) | 2023.02.02 |
Flutter 스터디 12 플러터 Onboarding screen (0) | 2023.02.02 |
Flutter 스터디 11 플러터 2.0 Snack bar와 ScaffoldMessenger, 버튼들(Elevated button, Text button, Outlined button) (0) | 2023.02.02 |