Flutter – Scrollable Text
Last Updated :
15 Jan, 2021
In this article, we are going to make a Flutter application in which we will add a Text Widget that can be scrolled horizontally or vertically. These can have a wide range of applications depending upon the needs of the users. Here we will be implementing the simplest form of scrollable text.
We can make the text scrollable in flutter using these 2 widgets:
- Expanded Class: A widget that expands a child of a Row, Column, or Flex so that the child fills the available space.
- SingleChildScrollView Class: A box in which a single widget can be scrolled.
We have to wrap the Text widget within SingleChildScrollView widget and further wrap the SingleChildScrollView widget within the Expanded widget like this :
Expanded(
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Text(
"GeeksForGeeks : Learn Anything, Anywhere",
style: TextStyle(
color: Colors.green,
fontWeight: FontWeight.bold,
fontSize: 20.0,
),
),
),
);
Let’s look into some example now:
Example 1: Horizontal Scrolling
Follow the below steps to implement horizontal scrolling in a Text Widget:
Step 1: Create a new flutter project in Android Studio.
Step 2: Make the layout using widgets
Go to the main.dart file and refer to the following code. Below is the code for the main.dart file.
Dart
import 'package:flutter/material.dart' ;
void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Color.fromRGBO(15, 157, 88, 1),
title: Text(
"GeeksForGeeks" ,
style: TextStyle(
color: Colors.white,
),
),
),
body: MyApp(),
),
));
}
class MyApp 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: 5.0,
),
),
child: Expanded(
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Text(
"GeeksForGeeks is a good platform to learn programming."
" It is an educational website." ,
style: TextStyle(
color: Colors.green,
fontWeight: FontWeight.bold,
fontSize: 20.0,
letterSpacing: 3,
wordSpacing: 3,
),
),
),
),
),
);
}
}
|
Output:
Example 2: Vertical Scrolling
For vertical scrolling of the text follow Step 1 and Step 2 of the first example and change the scrolling direction to vertical as shown below:
scrollDirection: Axis.vertical
The main.dart file
Dart
import 'package:flutter/material.dart' ;
void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Color.fromRGBO(15, 157, 88, 1),
title: Text(
"GeeksForGeeks" ,
style: TextStyle(
color: Colors.white,
),
),
),
body: MyApp(),
),
));
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
margin: const EdgeInsets.all(15.0),
padding: const EdgeInsets.all(3.0),
height: 80.0,
decoration: BoxDecoration(
border: Border.all(
color: Colors.blueAccent,
width: 5.0,
),
),
child: Expanded(
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Text(
"GeeksForGeeks is a good platform to learn programming."
" It is an educational website." ,
style: TextStyle(
color: Colors.green,
fontWeight: FontWeight.bold,
fontSize: 20.0,
letterSpacing: 3,
wordSpacing: 3,
),
),
),
),
),
);
}
}
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...