Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a text background with Flutter?

Tags:

I'm very new to flutter but I'm interested in learning it pretty much from the beginning.

Right now I'm trying such a basic thing as changing the background color of some text, but I'm stuck.

import 'package:flutter/material.dart';  void main() {    final barColor = const Color(0xFFD63031);    var app = MaterialApp(         home: Scaffold(           backgroundColor: barColor,         ),       );    Center(         child: Text('My Text',             textDirection: TextDirection.ltr,         ),       );       runApp(app); } 

I do understand why the text doesn't show but I've been working on this for days now and I have tried a lot of different things without succeeding, so any help would be very appreciated.

Thank You

like image 244
J.W Avatar asked Dec 17 '18 01:12

J.W


People also ask

How do you decorate Text in Flutter?

You do it by applying decoration: TextDecoration. underline to TextStyle of a Text . Show activity on this post. Text( 'Welcome to Flutter', style: TextStyle( fontSize: 24, decoration: TextDecoration.

How do you add custom background color in Flutter?

Step 1: Go to your main.Step 2: Inside the MaterialApp, find the ThemeData widget. Step 3: Add the scaffoldBackgroundColor property inside and assign the color you want.


1 Answers

TL;DR - (Updated 07-08-2019)

Using style property (backgroundColor)

Text(   'Some text...',   style: TextStyle(backgroundColor: Colors.blue), ) 

Using style property (background)

Text(   'Some text...',   style: TextStyle(background: Paint()..Colors.blue), ) 

Using a DecoratedBox

const DecoratedBox(   decoration: const BoxDecoration(color: Colors.blue),   child: const Text('Some text...'), ); 

Long answer

First of all, welcome to Flutter and StackOverflow :)

That happens because are misunderstand the way you should develop with Flutter. As opposite to what happens with other architectures where you start in the main() function, instaciate your vars/objects and develop your flow from there, with Flutter you start your widget tree from your main() function as well, usually with a MaterialApp or CupertinoApp and fit in all its children to create your app.

So, as example to get what you want, you must add your Center widget as the body of your Scaffold and then give a TextStyle to your Text widget, providing the property color. I gave it blue, but you can give anything else you want. Thereby, this is your refactored code

void main() => runApp(       MaterialApp(         home: Scaffold(           backgroundColor: const Color(0xFFD63031),           body: Center(             child: Text(               'MyText',               textDirection: TextDirection.ltr,               style: TextStyle(                 background: Paint()..color = Colors.blue,               ),             ),           ),         ),       ),     ); 

that will provide the following result

example

I suggest you to take a look at the Awesome Flutter repo where you have a lot of good Flutter content to start with that can really help you out.

like image 86
Miguel Ruivo Avatar answered Oct 12 '22 00:10

Miguel Ruivo