Flutter – FractionalTranslation Widget
Last Updated :
30 Oct, 2023
In Flutter, the FractionalTranslation widget allows you to translate its child widget by a fraction of its own size along both the horizontal and vertical axes. In this article, we are going to implement the FractionalTranslation widget and explore some properties of it. A sample Image is given below to get an idea about what we are going to do in this article.
Basic Syntax of FractionalTranslation Widget
FractionalTranslation(
translation: Offset(dx, dy),
child: YourChildWidget(),
)
Required Tools
To build this app, you need the following items installed on your machine:
- Visual Studio Code / Android Studio
- Android Emulator / iOS Simulator / Physical Device device.
- Flutter Installed
- Flutter plugin for VS Code / Android Studio.
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Import the Package
First of all import material.dart file.
import 'package:flutter/material.dart';
Step 3: Execute the main Method
Here the execution of our app starts.
Dart
void main() {
runApp(MyApp());
}
|
Step 4: Create MyApp Class
In this class we are going to implement the MaterialApp , here we are also set the Theme of our App.
Dart
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.green,
),
debugShowCheckedModeBanner: false ,
home: FractionalTranslationExample(),
);
}
}
|
Step 5: Create FractionalTranslationExample Class
In this class we are going to Implement the FractionalTranslation widget.The translation property of FractionalTranslation is set to Offset(0.8, 0.2), which means it will translate its child by 80% of the child’s own width to the right and 20% of its own height down. Comments are added for better understanding.
FractionalTranslation(
translation: Offset(0.8, 0.2), // Translate by 80% of its width and 20% of its height
child: Container(
width: 100,
height: 100,
color: Colors.green, // Background color of the Container
child: Center(
child: Text(
'FractionalTranslation',
style: TextStyle(color: Colors.white), // Text color
),
),
),
),
Dart
class FractionalTranslationExample extends StatelessWidget {
const FractionalTranslationExample({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text( 'FractionalTranslation Example' ),
),
body: Center(
child: FractionalTranslation(
translation: Offset(0.8, 0.2),
child: Container(
width: 100,
height: 100,
color: Colors.green,
child: Center(
child: Text(
'FractionalTranslation' ,
style: TextStyle(color: Colors.white),
),
),
),
),
),
);
}
}
|
Here is the full Code of main.dart file
Dart
import 'package:flutter/material.dart' ;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.green,
),
debugShowCheckedModeBanner: false ,
home: FractionalTranslationExample(),
);
}
}
class FractionalTranslationExample extends StatelessWidget {
const FractionalTranslationExample({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text( 'FractionalTranslation Example' ),
),
body: Center(
child: FractionalTranslation(
translation: Offset(0.8, 0.2),
child: Container(
width: 100,
height: 100,
color: Colors.green,
child: Center(
child: Text(
'FractionalTranslation' ,
style: TextStyle(color: Colors.white),
),
),
),
),
),
);
}
}
|
Output:
Share your thoughts in the comments
Please Login to comment...