In Flutter, the DefaultTextStyle widget allows you to set default text styling for a subtree of the widget tree. This means that all descendant Text widgets within that subtree will inherit the specified text style unless they explicitly override it. Here's an example of how you can use DefaultTextStyle to set default text styling:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('DefaultTextStyle Example'),
),
body: Center(
child: DefaultTextStyle(
style: TextStyle(
fontSize: 18.0,
color: Colors.black,
fontWeight: FontWeight.normal,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Default Text Style Example'),
SizedBox(height: 10.0),
Text('This text inherits the default style.'),
SizedBox(height: 10.0),
Text(
'But this text can override the default style.',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.blue,
),
),
],
),
),
),
),
);
}
}
In this example:
The DefaultTextStyle widget is wrapped around a Column.
The style attribute of DefaultTextStyle is set to define the default text style for all descendant Text widgets within the Column.
The first two Text widgets within the Column inherit the default style.
The third Text widget explicitly overrides the default style with a different font weight and text color.
ย