text
stringlengths
1
474
For the full set of these widgets,
see the Cupertino widgets gallery.You can also use a WidgetsApp as your app widget,
which provides some of the same functionality,
but is not as rich as MaterialApp.To customize the colors and styles of any child components,
pass a ThemeData object to the MaterialApp widget.
For example, in the code below,
the color scheme from seed is set to deepPurple and divider color is grey.
<code_start>import 'package:flutter/material.dart';
class SampleApp extends StatelessWidget {
const SampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Sample App',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
dividerColor: Colors.grey,
),
home: const SampleAppPage(),
);
}
}<code_end>
<topic_end>
<topic_start>
Using custom fonts
In UIKit, you import any ttf font files into your project
and create a reference in the info.plist file.
In Flutter, place the font file in a folder
and reference it in the pubspec.yaml file,
similar to how you import images.Then assign the font to your Text widget:
<code_start>@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Sample App'),
),
body: const Center(
child: Text(
'This is a custom font text',
style: TextStyle(fontFamily: 'MyCustomFont'),
),
),
);
}<code_end>
<topic_end>
<topic_start>
Styling text
Along with fonts, you can customize other styling elements on a Text widget.
The style parameter of a Text widget takes a TextStyle object,
where you can customize many parameters, such as:<topic_end>
<topic_start>
Bundling images in apps
While iOS treats images and assets as distinct items,
Flutter apps have only assets. Resources that are
placed in the Images.xcasset folder on iOS,
are placed in an assets’ folder for Flutter.
As with iOS, assets are any type of file, not just images.
For example, you might have a JSON file located in the my-assets folder:Declare the asset in the pubspec.yaml file:And then access it from code using an AssetBundle:
<code_start>import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;
Future<String> loadAsset() async {
return await rootBundle.loadString('my-assets/data.json');
}<code_end>
For images, Flutter follows a simple density-based format like iOS.
Image assets might be 1.0x, 2.0x, 3.0x, or any other multiplier.
Flutter’s devicePixelRatio expresses the ratio
of physical pixels in a single logical pixel.Assets are located in any arbitrary folder—
Flutter has no predefined folder structure.
You declare the assets (with location) in
the pubspec.yaml file, and Flutter picks them up.For example, to add an image called my_icon.png to your Flutter
project, you might decide to store it in a folder arbitrarily called images.
Place the base image (1.0x) in the images folder, and the
other variants in sub-folders named after the appropriate ratio multiplier:Next, declare these images in the pubspec.yaml file:You can now access your images using AssetImage:
<code_start>AssetImage('images/a_dot_burr.jpeg')<code_end>
or directly in an Image widget:
<code_start>@override
Widget build(BuildContext context) {
return Image.asset('images/my_image.png');
}<code_end>
For more details, see
Adding Assets and Images in Flutter.<topic_end>
<topic_start>
Form input
This section discusses how to use forms in Flutter
and how they compare with UIKit.<topic_end>
<topic_start>
Retrieving user input
Given how Flutter uses immutable widgets with a separate state,
you might be wondering how user input fits into the picture.
In UIKit, you usually query the widgets for their current values
when it’s time to submit the user input, or action on it.
How does that work in Flutter?In practice forms are handled, like everything in Flutter,
by specialized widgets. If you have a TextField or a
TextFormField, you can supply a TextEditingController
to retrieve user input:
<code_start>class _MyFormState extends State<MyForm> {
// Create a text controller and use it to retrieve the current value.
// of the TextField!
final myController = TextEditingController();
@override