Skip to content

Instantly share code, notes, and snippets.

@3bugs
Created February 26, 2023 05:06
Show Gist options
  • Select an option

  • Save 3bugs/170c34ee8cf305c45d815000f11738ab to your computer and use it in GitHub Desktop.

Select an option

Save 3bugs/170c34ee8cf305c45d815000f11738ab to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
class PhotoListPage extends StatefulWidget {
const PhotoListPage({Key? key}) : super(key: key);
@override
State<PhotoListPage> createState() => _PhotoListPageState();
}
class User {
final String firstName;
final String lastName;
final int age;
User({required this.firstName, required this.lastName, required this.age});
}
class _PhotoListPageState extends State<PhotoListPage> {
List<User> userList = [
User(firstName: 'Promlert', lastName: 'Lovichit', age: 48),
User(firstName: 'Prayuth', lastName: 'Chan-ocha', age: 68),
User(firstName: 'Prawit', lastName: 'wongsuwan', age: 77),
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('PHOTO GALLERY'),
),
body: ListView.builder(
itemCount: userList.length, itemBuilder: _buildListItem));
}
Widget _buildListItem(BuildContext context, int index) {
var user = userList[index];
/*if (user.age > 50) {
return UserView(user: user, showAge: false);
} else {
return UserView(user: user, showAge: true);
}*/
return UserView(user: user, showAge: user.age <= 50);
}
}
class UserView extends StatelessWidget {
const UserView({
Key? key,
required this.user,
required this.showAge,
}) : super(key: key);
final User user;
final bool showAge;
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.all(8.0),
padding: const EdgeInsets.all(8.0),
color: Colors.pink,
child: Row(
children: [
Container(
color: Colors.white,
width: 60.0,
height: 60.0,
child: const Icon(Icons.person, size: 40.0),
),
const SizedBox(width: 8.0),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'${user.firstName} ${user.lastName}',
style: const TextStyle(color: Colors.white, fontSize: 30.0),
),
if (showAge)
Text(
'อายุ ${user.age} ปี',
style: const TextStyle(color: Colors.white, fontSize: 20.0),
),
],
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment