Cara Membuat Text Read More di Flutter |
Berikut ini source code nya:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
useMaterial3: false,
primarySwatch: Colors.green,
),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
HomePageState createState() => HomePageState();
}
class HomePageState extends State {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Read More'),
),
body: const Padding(
padding: EdgeInsets.symmetric(
vertical: 30,
horizontal: 20
),
child: SingleChildScrollView(
child: Column(
children: [
TextWrapper(
text:
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Can sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui."
),
Divider(
height: 30,
),
TextWrapper(
text:
"Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc."
)
],
),
),
),
);
}
}
class TextWrapper extends StatefulWidget {
const TextWrapper({super.key, required this.text});
final String text;
@override
State createState() => _TextWrapperState();
}
class _TextWrapperState extends State with TickerProviderStateMixin {
bool isExpanded = false;
@override
Widget build(BuildContext context) {
return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
AnimatedSize(
duration: const Duration(milliseconds: 300),
child: ConstrainedBox(
constraints: isExpanded
? const BoxConstraints()
: const BoxConstraints(maxHeight: 70),
child: Text(
widget.text,
style: const TextStyle(fontSize: 16),
softWrap: true,
overflow: TextOverflow.fade,
)
)
),
isExpanded
? TextButton.icon(
icon: const Icon(Icons.arrow_upward),
label: const Text('Baca Lebih Sedikit'),
onPressed: () => setState(() => isExpanded = false))
: TextButton.icon(
icon: const Icon(Icons.arrow_downward),
label: const Text('Baca Selengkapnya'),
onPressed: () => setState(() => isExpanded = true))
]);
}
}
Untuk Output-nya nanti seperti ini:Support Blog Rivaldi 48 agar terus bisa membagikan artikel-artikel yang bermanfaat dengan cara klik link Sociabuzz dibawah ini :https://sociabuzz.com/azharrvldi_/donate (bisa pakai GoPay, OVO, DANA, Bank Transfer)
Demikian informasi yang saya bagikan untuk kalian. Jangan lupa bagikan artikel ini ke teman-teman kalian agar ikut membaca Cara Membuat Text Read More di Flutter ini. Subscribe juga blog Rivaldi 48 ini agar kalian mendapatkan notifikasi saat Admin update artikel terbaru. Semoga kalian lebih nyaman dan mudah dalam mengakses Blog Rivaldi 48 dimanapun kalian berada. Terima Kasih. Follow Instagram Admin @azhardvls_
0 Comments
Post a Comment
Silahkan tinggalkan komentar jika Anda punya saran, kritik, atau pertanyaan seputar topik pembahasan. Gunakan bahasa yang bijak dan santun. Terima Kasih.