App development
Everything is a widget
Create a new Project
To create a new Flutter app, run the following commands in your shell or Terminal.
flutter create test_flutter
cd test_flutter
To verify that you have a running target device, run the following command.
flutter devices
Open in Vscode and run the project from there
To run on emulator
List devices
flutter emulators
Launch devices
flutter emulators --launch <emulator id>
To run your app, run the following command.
flutter run
Make changes
Open lib/main.dart.
Make changes
Save your changes.
Type
r
in the terminal window.
Widget types
Stateless widget
- cannot change with time
Stateful widget
- can change with time
initState()
- called only when the widget is created
- Subscribe to streams or any object that could change our widget data
Build()
- Builds the widget tree
- A build is triggered every time we use setState()
Dispose()
- When the widget/state object is removed
Text Widget
- style
- textAlign
- overflow
- maxLin- etc
'Click me') Text(
Row and Column Widget
: Column(
body: MainAxisAlignment.spaceEvenly,
mainAxisAlignment: CrossAxisAlignment.center,
crossAxisAlignment: <Widget>[
children
Row(: MainAxisAlignment.center,
mainAxisAlignment: const <Widget>[
children'Hello'),
Text('World'),
Text(,
],
)
Container(: const EdgeInsets.all(20.0),
padding: Colors.grey[200],
color: const TextButton(
child: null,
onPressed: Text('Click Me'),
child,
),
)const ElevatedButton(
: null,
onPressed: Text('Click Me'),
child,
)const OutlinedButton(
: null,
onPressed: Text('Click'),
child,
),
], )
Image Widget
: Center(
body: Image.asset('assets/beach1.jpg'),
child )
Expanded Widget
life flex grid
: Row(
body: <Widget>[
children
Expanded(: 3,
flex: Image.asset('assets/space1.jpg'),
child,
)
Expanded(: 2,
flex: Container(
child: const EdgeInsets.all(30.0),
padding: Colors.cyan,
color: Text('1'),
child,
),
)
Expanded(: 1,
flex: Container(
child: const EdgeInsets.all(30.0),
padding: Colors.pink,
color: Text('2'),
child,
),
)
Expanded(: 3,
flex: Container(
child: const EdgeInsets.all(30.0),
padding: Colors.amber,
color: Text('3'),
child,
),
),
] )
Async functions
void getData() async {
// simulate network request for a username
String username = await Future.delayed(Duration(seconds: 3), () {
return 'yoshi';
});
// simulate network request to get bio of the username
String bio = await Future.delayed(Duration(seconds: 2), () {
return 'vegan, musician & egg collector';
});
'$username - $bio');
print(}
Future<void> getTime() async {
= await get(
Response response .parse('http://worldtimeapi.org/api/timezone/$url'),
Uri
);Map data = json.decode(response.body);
String datetime = data['datetime'];
String offset = data['utc_offset'].substring(1, 3);
= DateTime.parse(datetime);
DateTime now = now.add(Duration(hours: int.parse(offset)));
now
= now.toString();
time }
}
Routing
: {
routes'/': (context) => Loading(),
'/home': (context) => Home(),
'/location': (context) => ChooseLocation(),
},
.pushReplacementNamed(context, '/home', arguments: {
Navigator'location': instance.location,
'flag': instance.flag,
'time': instance.time,
'isDaytime': instance.isDaytime,
});
: () async {
onPresseddynamic result =
await Navigator.pushNamed(context, '/location');
{
setState(() = {
data 'time': result['time'],
'location': result['location'],
'isDaytime': result['isDaytime'],
'flag': result['flag'],
};
});
},
.pop(context, {
Navigator'location': instance.location,
'flag': instance.flag,
'time': instance.time,
'isDaytime': instance.isDaytime,
});