text stringlengths 1 372 |
|---|
theme: ThemeData( |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), |
textSelectionTheme: |
const TextSelectionThemeData(selectionColor: colors.red)), |
home: const SampleAppPage(), |
); |
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
how do i add style themes? |
in react native, common themes are defined for |
components in stylesheets and then used in components. |
in flutter, create uniform styling for almost everything |
by defining the styling in the ThemeData |
class and passing it to the theme property in the |
MaterialApp widget. |
<code_start> |
@override |
widget build(BuildContext context) { |
return MaterialApp( |
theme: ThemeData( |
primaryColor: colors.cyan, |
brightness: brightness.dark, |
), |
home: const StylingPage(), |
); |
} |
<code_end> |
a theme can be applied even without using the MaterialApp widget. |
the theme widget takes a ThemeData in its data parameter |
and applies the ThemeData to all of its children widgets. |
<code_start> |
@override |
widget build(BuildContext context) { |
return theme( |
data: ThemeData( |
primaryColor: colors.cyan, |
brightness: brightness, |
), |
child: scaffold( |
backgroundColor: Theme.of(context).primaryColor, |
//... |
), |
); |
} |
<code_end> |
<topic_end> |
<topic_start> |
state management |
state is information that can be read synchronously |
when a widget is built or information |
that might change during the lifetime of a widget. |
to manage app state in flutter, |
use a StatefulWidget paired with a state object. |
for more information on ways to approach managing state in flutter, |
see state management. |
<topic_end> |
<topic_start> |
the StatelessWidget |
a StatelessWidget in flutter is a widget |
that doesn’t require a state change— |
it has no internal state to manage. |
stateless widgets are useful when the part of the user interface |
you are describing does not depend on anything other than the |
configuration information in the object itself and the |
BuildContext in which the widget is inflated. |
AboutDialog, CircleAvatar, and text are examples |
of stateless widgets that subclass StatelessWidget. |
<code_start> |
import 'package:flutter/material.dart'; |
void main() => runApp( |
const MyStatelessWidget( |
text: 'statelesswidget example to show immutable data', |
), |
); |
class MyStatelessWidget extends StatelessWidget { |
const MyStatelessWidget({ |
super.key, |
required this.text, |
}); |
final string text; |
@override |
widget build(BuildContext context) { |
return center( |
child: text( |
text, |
textDirection: TextDirection.ltr, |
), |
); |
} |
} |
<code_end> |
the previous example uses the constructor of the MyStatelessWidget |
class to pass the text, which is marked as final. |
this class extends StatelessWidget—it contains immutable data. |
the build method of a stateless widget is typically called |
in only three situations: |
<topic_end> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.