text
stringlengths
1
372
void initState() {
super.initState();
loadData();
}
widget getBody() {
bool showLoadingDialog = widgets.isEmpty;
if (showloadingdialog) {
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":{