text
stringlengths 1
474
|
---|
return getProgressDialog(); |
} else { |
return getListView(); |
} |
} |
Widget getProgressDialog() { |
return const Center(child: CircularProgressIndicator()); |
} |
@override |
Widget build(BuildContext context) { |
return Scaffold( |
appBar: AppBar( |
title: const Text('Sample App'), |
), |
body: getBody(), |
); |
} |
ListView getListView() { |
return ListView.builder( |
itemCount: widgets.length, |
itemBuilder: (context, position) { |
return getRow(position); |
}, |
); |
} |
Widget getRow(int i) { |
return Padding( |
padding: const EdgeInsets.all(10), |
child: Text("Row ${widgets[i]["title"]}"), |
); |
} |
Future<void> loadData() async { |
var dataURL = Uri.parse('https://jsonplaceholder.typicode.com/posts'); |
http.Response response = await http.get(dataURL); |
setState(() { |
widgets = jsonDecode(response.body); |
}); |
} |
}<code_end> |
<topic_end> |
<topic_start> |
Project structure & resources |
<topic_end> |
<topic_start> |
Where do I store my resolution-dependent image files? |
While Android treats resources and assets as distinct items, |
Flutter apps have only assets. All resources that would live |
in the res/drawable-* folders on Android, |
are placed in an assets folder for Flutter.Flutter follows a simple density-based format like iOS. |
Assets might be 1.0x, 2.0x, 3.0x, or any other multiplier. |
Flutter doesn’t have dps but there are logical pixels, |
which are basically the same as device-independent pixels. |
Flutter’s devicePixelRatio expresses the ratio |
of physical pixels in a single logical pixel.The equivalent to Android’s density buckets are: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.Assets stored in the native asset folder are |
accessed on the native side using Android’s AssetManager:Flutter can’t access native resources or assets.To add a new image asset called my_icon.png to our Flutter project, |
for example, and deciding that it should live in a folder we |
arbitrarily called images, you would put the base image (1.0x) |
in the images folder, and all the other variants in sub-folders |
called with the appropriate ratio multiplier:Next, you’ll need to declare these images in your pubspec.yaml file:You can then access your images using AssetImage: |
<code_start>AssetImage('images/my_icon.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> |
<topic_end> |
<topic_start> |
Where do I store strings? How do I handle localization? |
Flutter currently doesn’t have a dedicated resources-like system for strings. |
The best and recommended practice is to hold your strings in a .arb file as key-value pairs For example: |
<code_start>{ |
"@@locale": "en", |
"hello":"Hello {userName}", |
"@hello":{ |
"description":"A message with a single parameter", |
"placeholders":{ |
"userName":{ |
"type":"String", |
"example":"Bob" |
} |
} |
} |
}<code_end> |
Then in your code, you can access your strings as such: |
<code_start>Text(AppLocalizations.of(context)!.hello('John'));<code_end> |
Flutter has basic support for accessibility on Android, |
though this feature is a work in progress.See Internationalizing Flutter apps for more information on this.<topic_end> |
<topic_start> |
What is the equivalent of a Gradle file? How do I add dependencies? |
In Android, you add dependencies by adding to your Gradle build script. |
Flutter uses Dart’s own build system, and the Pub package manager. |
The tools delegate the building of the native Android and iOS |
wrapper apps to the respective build systems.While there are Gradle files under the android folder in your |
Flutter project, only use these if you are adding native |
dependencies needed for per-platform integration. |
In general, use pubspec.yaml to declare |
external dependencies to use in Flutter. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.