Last active
October 4, 2020 19:39
-
-
Save lesliearkorful/fb603ccc2a31c7b0ad2ab85d2a68a718 to your computer and use it in GitHub Desktop.
Tweet thumbnail with media
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: SingleChildScrollView( | |
child: Column( | |
children: [ | |
ListTile( | |
leading: CircleAvatar(), | |
dense: true, | |
title: Text("Leslie Arkorful"), | |
subtitle: Text("@lesliearkorful"), | |
), | |
Container( | |
height: 300, | |
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 10), | |
child: ThumbnailGrid( | |
images: [ | |
"image1", | |
"image2", | |
"image3", | |
"image4", | |
], | |
), | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} | |
class ThumbnailGrid extends StatelessWidget { | |
final List<String> images; | |
final double spacing; | |
const ThumbnailGrid({Key key, this.images = const [], this.spacing = 5.0}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
if (images?.isEmpty ?? true) return SizedBox(); | |
return ClipRRect( | |
borderRadius: BorderRadius.circular(8), | |
child: Row( | |
mainAxisSize: MainAxisSize.max, | |
children: [ | |
if (images.length == 1) ImageThumbnail(imageUrl: images[0]), | |
if (images.length == 2) ...[ | |
ImageThumbnail(imageUrl: images[0]), | |
SizedBox(width: spacing), | |
ImageThumbnail(imageUrl: images[1]), | |
], | |
if (images.length == 3) ...[ | |
ImageThumbnail(imageUrl: images[0]), | |
SizedBox(width: spacing), | |
Expanded( | |
child: Column( | |
children: [ | |
ImageThumbnail(imageUrl: images[1]), | |
SizedBox(height: spacing), | |
ImageThumbnail(imageUrl: images[2]), | |
], | |
), | |
), | |
], | |
if (images.length == 4) ...[ | |
Expanded( | |
child: Column( | |
children: [ | |
ImageThumbnail(imageUrl: images[0]), | |
SizedBox(height: spacing), | |
ImageThumbnail(imageUrl: images[2]), | |
], | |
), | |
), | |
SizedBox(width: spacing), | |
Expanded( | |
child: Column( | |
children: [ | |
ImageThumbnail(imageUrl: images[1]), | |
SizedBox(height: spacing), | |
ImageThumbnail(imageUrl: images[3]), | |
], | |
), | |
), | |
], | |
], | |
), | |
); | |
} | |
} | |
class ImageThumbnail extends StatelessWidget { | |
final String imageUrl; | |
final Function(String imageUrl) onTap; | |
const ImageThumbnail({Key key, this.imageUrl, this.onTap}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Expanded( | |
child: GestureDetector( | |
onTap: () { | |
if ((onTap != null) && (imageUrl != null)) onTap(imageUrl); | |
}, | |
child: Container( | |
decoration: BoxDecoration(color: Colors.blueGrey.shade200), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment