text
stringlengths 1
372
|
|---|
<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
|
void dispose() {
|
// clean up the controller when disposing of the widget.
|
myController.dispose();
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.