Say Hello to Flutter Text Widget

Home » Flutter » Say Hello to Flutter Text Widget

In the previous lesson, ‘Hello, World’, We learnt how to create a new Flutter app and a virtual device for testing our app.

Say Hello to Flutter Text Widget

Flutter Text Widget: A Text Class

The Text widget one of the super cool widgets that displays a string of text with a single style.

Let’s write your name on the app’s screen

Create a new Flutter Project in Android Studio. It’s time to renew our main.dart file

import 'package:flutter/material.dart';

void main()
{
  runApp(MyApp());
}
class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
       body: Center(
         child: Text("Your name"),
       ),
      ),
    );

  }
}

Let’s declare a variable ‘name’ as String.

String name = "Nikin";

Let’s declare a variable ‘name’ as String. The $ (dollar) symbol is beneficial for interpolating variables in a string. You can use ${} for expressions.

Text("Your name  $name")

main.dart

import 'package:flutter/material.dart';

void main()
{
  runApp(MyApp());
}
class MyApp extends StatelessWidget{
  String name = "Nikin";
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
       body: Center(
         child:  Text("Your name $name"),
       ),
      ),
    );

  }
}

The style parameter used to give more enhancement to text.

Let’s add style parameter to Text() widget.

Flutter Text widget : fontWeight, fontSize and color

Text("Your name  $name",
  style: const TextStyle(fontSize: 28,
  fontWeight: FontWeight.bold
  ),

main.dart

import 'package:flutter/material.dart';

void main()
{
  runApp(MyApp());
}
class MyApp extends StatelessWidget{
  String name = "Nikin";
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
       body: Center(
         child:  Text("Your name  $name",
           style: const TextStyle(fontSize: 28,
           fontWeight: FontWeight.bold
           ),

         ),
       ),
      ),
    );

  }
}

Flutter : set font color and background for Text() widget

Text("Your name  $name",
  style: const TextStyle(fontSize: 28,
  fontWeight: FontWeight.bold,
    backgroundColor: Colors.amber,
    color: Colors.red
  ),

Final version of main.dart

import 'package:flutter/material.dart';

void main()
{
  runApp(MyApp());
}
class MyApp extends StatelessWidget{
  String name = "Nikin";
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
       body: Center(
         child:  Text("Your name  $name",
           style: const TextStyle(fontSize: 28,
           fontWeight: FontWeight.bold,
             backgroundColor: Colors.amber,
             color: Colors.red
           ),
         ),
       ),
      ),
    );

  }
}

You may also like...

2 Responses

  1. 09/07/2021

    […] a new flutter project, Let’s add a Text widget to column_example.dart file (right click on lib folder > New > Dart […]

  2. 16/07/2021

    […] About this widget : The official doc says, “A run of text with a single style” learn more about Text widget […]

Leave a Reply

Your email address will not be published. Required fields are marked *