MOCKSTACKS
EN
Questions And Answers

More Tutorials









Flutter Basic Material Widgets


Text

The Text widget displays a string of text with single style. Multiple line texts are allowed.
To style the entire text in one way, specify a ‘style’ property in the constructor of the Text Widget.
To style sections of the text, use child TextSpans (see example below).

Example – ‘text’

Every time you hit the ‘+’ a new word comes out in a different color.

Source Code

import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Styled Text Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),
);
}
}
class TextBlock {
final Color _color;
final String _text;
TextBlock(this._color, this._text);
String get text => _text;
Color get color => _color;
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _index = 0;
final List<TextBlock> textBlocks = [
TextBlock(Colors.red, 'every'),
TextBlock(Colors.redAccent, ' schoolboy'),
TextBlock(Colors.green, '\nknows'),
TextBlock(Colors.greenAccent, ' who'),
TextBlock(Colors.blue, '\nimprisoned'),
TextBlock(Colors.blueAccent, '\nMontezuma')
];
void _incrementCounter() {
setState(() {
if (_index < textBlocks.length) {
_index++;
}
});
}
@override
Widget build(BuildContext context) {
final List<TextSpan> textSpans = List<TextSpan>();
for (var i = 0; i < _index; i++) {
TextBlock textBlock = textBlocks[i];
textSpans.add(TextSpan(
text: textBlock.text,
style: TextStyle(color: textBlock.color, fontSize: 32.0)));
}
return new Scaffold(
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[Text.rich(TextSpan(children: textSpans))],
),
),
floatingActionButton: new FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: new Icon(Icons.note_add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}


Conclusion

In this page (written and validated by ) you learned about Flutter Basic Material Widgets . What's Next? If you are interested in completing Flutter tutorial, your next topic will be learning about: Flutter Image.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.