Scrollable Text - Flutter

We will create a Flutter application and add a Text Widget that can be scrolled horizontally or vertically. These might have a variety of applications based on the demands of the consumers.

In this article, you will learn how to create your Scrollable text  in Flutter. So let’s take one example of the Scrollable text in Flutter of Mobile application.


Step-by-Step Implementation

Step 1: Create a New Project in Android Studio (File >new flutter project).


Step 2: Adding material package

Import method the runApp method in the main function call first while run the application.

 import 'package:flutter/material.dart';

 void main() {

     runApp(RunMyApp());

 }

Step 3: Creating a stateless widget. 

we can create a stateless widget that contains MaterialApp widget,Appbar,etc.

 class RunMyApp extends StatelessWidget {

     const RunMyApp({super.key});  

    @override

    Widget build(BuildContext context) {

    return MaterialApp(home);

     }

 }

Step 4: Vertical Scrolling.

 we can scroll text in vertical of mobile application, used property is "Axis.vertical"

Step 4.1: Final code of text scroll view.

 import 'package:flutter/material.dart';

void main() {
runApp(Scrollabletext());
}

class Scrollabletext extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
margin: const EdgeInsets.all(15.0),
padding: const EdgeInsets.all(3.0),
decoration: BoxDecoration(
border: Border.all(
color: Colors.blueAccent,
width: 1.0,
),
),
child: Expanded(
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Text(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sed vulputate orci. Proin id scelerisque velit. Fusce at ligula ligula. Donec fringilla sapien odio, et faucibus tortor finibus sed. Aenean rutrum ipsum in sagittis auctor. Pellentesque mattis luctus consequat. Sed eget sapien ut nibh rhoncus cursus. Donec eget nisl aliquam, ornare sapien sit amet, lacinia quam.",
style: TextStyle(
color: Colors.blue,
fontWeight: FontWeight.bold,
fontSize: 20.0,
letterSpacing: 3,
wordSpacing: 3,
),
),
),
),
),
);
}
}

Step 4.2: Output of Vertical Scrolling.


Step 5: Horizontal Scrolling.

 we can scroll text in horizontal of mobile application, used property is "Axis.horizontal".

Step 5.1: Final code of text scroll view.

 import 'package:flutter/material.dart';

void main() {
runApp(Scrollabletext());
}
class Scrollabletext extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
margin: const EdgeInsets.all(15.0),
padding: const EdgeInsets.all(3.0),
height: 30.0,
decoration: BoxDecoration(
border: Border.all(
color: Colors.blueAccent,
width: 2.0,
),
),
// SingleChildScrollView should be
// wrapped in an Expanded Widget
child: Expanded(
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Text(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sed vulputate orci. Proin id scelerisque velit. Fusce at ligula ligula. Donec fringilla sapien odio, et faucibus tortor finibus sed. Aenean rutrum ipsum in sagittis auctor. Pellentesque mattis luctus consequat. Sed eget sapien ut nibh rhoncus cursus. Donec eget nisl aliquam, ornare sapien sit amet, lacinia quam.",
style: TextStyle(
color: Colors.green,
fontWeight: FontWeight.bold,
fontSize: 20.0,
letterSpacing: 3,
wordSpacing: 3,
),
),
),
),
),
);
}
}

 

Step 5​.2: Output of Horizontal Scrolling.


Happy coding! 
365Bloggy June 13, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive