text stringlengths 1 474 |
|---|
// or 3 columns in landscape mode. |
crossAxisCount: orientation == Orientation.portrait ? 2 : 3, |
); |
}, |
),<code_end> |
info Note |
If you’re interested in the orientation of the screen, |
rather than the amount of space available to the parent, |
use MediaQuery.of(context).orientation instead of an |
OrientationBuilder widget.<topic_end> |
<topic_start> |
Interactive example |
<code_start>import 'package:flutter/material.dart'; |
void main() { |
runApp(const MyApp()); |
} |
class MyApp extends StatelessWidget { |
const MyApp({super.key}); |
@override |
Widget build(BuildContext context) { |
const appTitle = 'Orientation Demo'; |
return const MaterialApp( |
title: appTitle, |
home: OrientationList( |
title: appTitle, |
), |
); |
} |
} |
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, |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.