text stringlengths 1 474 |
|---|
Basic widgets |
Flutter comes with a suite of powerful basic widgets, |
of which the following are commonly used:Below are some simple widgets that combine these and other widgets: |
<code_start>import 'package:flutter/material.dart'; |
class MyAppBar extends StatelessWidget { |
const MyAppBar({required this.title, super.key}); |
// Fields in a Widget subclass are always marked "final". |
final Widget title; |
@override |
Widget build(BuildContext context) { |
return Container( |
height: 56, // in logical pixels |
padding: const EdgeInsets.symmetric(horizontal: 8), |
decoration: BoxDecoration(color: Colors.blue[500]), |
// Row is a horizontal, linear layout. |
child: Row( |
children: [ |
const IconButton( |
icon: Icon(Icons.menu), |
tooltip: 'Navigation menu', |
onPressed: null, // null disables the button |
), |
// Expanded expands its child |
// to fill the available space. |
Expanded( |
child: title, |
), |
const IconButton( |
icon: Icon(Icons.search), |
tooltip: 'Search', |
onPressed: null, |
), |
], |
), |
); |
} |
} |
class MyScaffold extends StatelessWidget { |
const MyScaffold({super.key}); |
@override |
Widget build(BuildContext context) { |
// Material is a conceptual piece |
// of paper on which the UI appears. |
return Material( |
// Column is a vertical, linear layout. |
child: Column( |
children: [ |
MyAppBar( |
title: Text( |
'Example title', |
style: Theme.of(context) // |
.primaryTextTheme |
.titleLarge, |
), |
), |
const Expanded( |
child: Center( |
child: Text('Hello, world!'), |
), |
), |
], |
), |
); |
} |
} |
void main() { |
runApp( |
const MaterialApp( |
title: 'My app', // used by the OS task switcher |
home: SafeArea( |
child: MyScaffold(), |
), |
), |
); |
}<code_end> |
Be sure to have a uses-material-design: true entry in the flutter |
section of your pubspec.yaml file. It allows you to use the predefined |
set of Material icons. It’s generally a good idea to include this line |
if you are using the Materials library.Many Material Design widgets need to be inside of a MaterialApp |
to display properly, in order to inherit theme data. |
Therefore, run the application with a MaterialApp.The MyAppBar widget creates a Container with a height of 56 |
device-independent pixels with an internal padding of 8 pixels, |
both on the left and the right. Inside the container, |
MyAppBar uses a Row layout to organize its children. |
The middle child, the title widget, is marked as Expanded, |
which means it expands to fill any remaining available space |
that hasn’t been consumed by the other children. |
You can have multiple Expanded children and determine the |
ratio in which they consume the available space using the |
flex argument to Expanded.The MyScaffold widget organizes its children in a vertical column. |
At the top of the column it places an instance of MyAppBar, |
passing the app bar a Text widget to use as its title. |
Passing widgets as arguments to other widgets is a powerful technique |
that lets you create generic widgets that can be reused in a wide |
variety of ways. Finally, MyScaffold uses an |
Expanded to fill the remaining space with its body, |
which consists of a centered message.For more information, check out Layouts.<topic_end> |
<topic_start> |
Using Material Components |
Flutter provides a number of widgets that help you build apps |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.