Skip to main content

How to write a custom responsive text widget in Flutter

 Custom responsive Text widget in Flutter


Creating a custom responsive text widget in Flutter involves using a combination of Flutter widgets and media query information to adjust the text size based on the screen size. Here's a basic example of how you can create a custom responsive text widget:

import 'package:flutter/material.dart';

//ResponsiveText widget class
class ResponsiveText extends StatelessWidget {
  final String text;
  final double minTextSize;
  final double maxTextSize;

  ResponsiveText({
    required this.text,
    required this.minTextSize,
    required this.maxTextSize,
  });

  @override
  Widget build(BuildContext context) {
    double screenWidth = MediaQuery.of(context).size.width;
    double scaleFactor = screenWidth / 375.0; // Assuming the base screen width is 375.0 (adjust as needed)

    double dynamicTextSize = max(minTextSize, min(maxTextSize, 16.0 * scaleFactor));

    return Text(
      text,
      style: TextStyle(
        fontSize: dynamicTextSize,
      ),
    );
  }
}

//******************************************************************************

// main() function 

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

// MyApp Widget class
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Responsive Text Example'),
        ),
        body: Center(
          child: ResponsiveText(
            text: 'Hello, Flutter!',
            minTextSize: 12.0,
            maxTextSize: 24.0,
          ),
        ),
      ),
    );
  }
}

//************************************************************************


In this example:
ResponsiveText is a custom widget that takes the text to display (text), a minimum text size (minTextSize), and a maximum text size (maxTextSize) as parameters.
Inside the build method, we use MediaQuery to get the current screen width.
We calculate a scaleFactor based on the assumption that the base screen width is 375.0. You may adjust this value depending on your design.
The dynamicTextSize is calculated by multiplying the base font size (16.0) by the scaleFactor. We ensure that the calculated size is within the specified minimum and maximum limits.
The Text widget is then created with the calculated font size.

You can customize this example based on your specific needs and adjust the scaling logic as necessary.


Thanks for reading the article...

Comments

Popular posts from this blog

How to write a code of Encode and Decode json data in Dart language

 Encode and decode JSON data in dart language import 'dart:convert'; void main() {   // Original data as a Dart map   Map<String, dynamic> originalData = {     'field1': 'value1',     'field2': 42,     'field3': true,   };   // Encode the Dart map to JSON string   String jsonString = jsonEncode(originalData);   print('Original JSON String:');   print(jsonString);   // Encode the JSON string to base64   String base64EncodedString = base64.encode(utf8.encode(jsonString));   print('\nBase64 Encoded String:');   print(base64EncodedString);   // Decode the base64 string to JSON string   String decodedJsonString = utf8.decode(base64.decode(base64EncodedString));   print('\nDecoded JSON String:');   print(decodedJsonString);   // Decode the JSON string to a Dart map   Map<String, dynamic> decodedData = jsonDecode(decodedJsonString);   print('\nDecoded Dart Ma...

About of Free Learning Tech Point

  Welcome to Free Learning Tech Point , where knowledge meets accessibility. Our platform is dedicated to providing high-quality educational resources and e-learning opportunities to learners around the world, completely free of charge. Our Mission: At Free Learning Tech Point, we believe that education is a fundamental right, and everyone should have access to valuable learning materials. Our mission is to break down barriers to education by offering a diverse range of courses, tutorials, and resources across various subjects and disciplines. What Sets Us Apart: - Free Access: Our commitment is to make learning accessible to all. No subscription fees, no hidden costs – just free, open access to knowledge.    - Quality Content: We curate and create content that is both engaging and informative. Whether you're a student, professional, or lifelong learner, our resources are designed to cater to various learning styles and levels. - Diverse Subjects: From tech and science...

Privacy Policy Of Free Learning Tech Point

  Thank you for visiting Free Learning Tech Point . You can use our website, services, and products with the awareness that this Privacy Policy describes how we gather, use, disclose, and protect your personal information. 1- Information We Collect: We may collect personal information that you provide directly to us, such as your name, email address, and any other information you choose to provide when using our Services. We may also collect non-personal information, such as aggregated data and usage patterns. 2- How We Use Your Information: We may use the information we collect for various purposes, including but not limited to: Providing and improving our Services. Responding to your inquiries and requests. Analyzing usage patterns and trends. Sending you updates, newsletters, and other communications. Personalizing your experience on our platform. 3- Cookies and Similar Technologies: We may use cookies and similar technologies to collect information about your interactions with...