flutter_link_previewer 2.0.2 flutter_link_previewer: ^2.0.2 copied to clipboard
URL preview extracted from the provided text with basic customization and ability to render from cached data.
import 'package:flutter/material.dart';
import 'package:flutter_link_previewer/flutter_link_previewer.dart';
import 'package:flutter_chat_types/flutter_chat_types.dart' show PreviewData;
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Map<String, PreviewData> datas = {};
List<String> get urls => const [
'https://flyer.chat',
'github.com/flyerhq',
];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
brightness: Brightness.dark,
title: const Text('Example'),
),
body: ListView.builder(
itemCount: urls.length,
itemBuilder: (context, index) => Align(
alignment: Alignment.centerLeft,
child: Container(
key: ValueKey(urls[index]),
margin: const EdgeInsets.all(16),
decoration: const BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(20),
),
color: Color(0xfff7f7f8),
),
child: ClipRRect(
borderRadius: const BorderRadius.all(
Radius.circular(20),
),
child: LinkPreview(
enableAnimation: true,
onPreviewDataFetched: (data) {
setState(() {
datas = {
...datas,
urls[index]: data,
};
});
},
previewData: datas[urls[index]],
text: urls[index],
width: MediaQuery.of(context).size.width,
),
),
),
),
),
);
}
}