text
stringlengths
1
372
}
}
class OrientationList extends StatelessWidget {
final string title;
const OrientationList({super.key, required this.title});
@override
widget build(BuildContext context) {
return scaffold(
appBar: AppBar(title: text(title)),
body: OrientationBuilder(
builder: (context, orientation) {
return GridView.count(
// create a grid with 2 columns in portrait mode, or 3 columns in
// landscape mode.
crossAxisCount: orientation == orientation.portrait ? 2 : 3,
// generate 100 widgets that display their index in the list.
children: list.generate(100, (index) {
return center(
child: text(
'item $index',
style: Theme.of(context).textTheme.displayLarge,
),
);
}),
);
},
),
);
}
}
<code_end>
<topic_end>
<topic_start>
locking device orientation
in the previous section, you learned
how to adapt the app UI to device orientation changes.
flutter also allows you to specify the orientations your app supports
using the values of DeviceOrientation. you can either:
in the application main() method,
call SystemChrome.setPreferredOrientations()
with the list of preferred orientations that your app supports.
to lock the device to a single orientation,
you can pass a list with a single item.
for a list of all the possible values, check out DeviceOrientation.
<code_start>
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);
runApp(const MyApp());
}
<code_end>
<topic_end>
<topic_start>
use themes to share colors and font styles
info note
this recipe uses flutter’s support for material 3 and
the google_fonts package. as of the flutter 3.16 release,
material 3 is flutter’s default theme.
to share colors and font styles throughout an app, use themes.
you can define app-wide themes.
you can extend a theme to change a theme style for one component.
each theme defines the colors, type style, and other parameters
applicable for the type of material component.
flutter applies styling in the following order:
after you define a theme, use it within your own widgets.
flutter’s material widgets use your theme to set the background
colors and font styles for app bars, buttons, checkboxes, and more.
<topic_end>
<topic_start>
create an app theme
to share a theme across your entire app, set the theme property
to your MaterialApp constructor.
this property takes a ThemeData instance.
as of the flutter 3.16 release, material 3 is flutter’s
default theme.
if you don’t specify a theme in the constructor,
flutter creates a default theme for you.
<code_start>
MaterialApp(
title: appName,
theme: ThemeData(
useMaterial3: true,
// define the default brightness and colors.
colorScheme: ColorScheme.fromSeed(
seedColor: colors.purple,
// ···
brightness: brightness.dark,
),
// define the default `texttheme`. use this to specify the default
// text styling for headlines, titles, bodies of text, and more.
textTheme: TextTheme(
displayLarge: const TextStyle(
fontSize: 72,
fontWeight: FontWeight.bold,
),
// ···
titleLarge: GoogleFonts.oswald(
fontSize: 30,