An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
Flutter & Dart Tips - Week In Review 5
Hello Reader,
In this post, I'll share the Flutter\Dart tips I tweeted last week. One of those tweets got more than 35 likes. Can you guess which one?
1- AppBar is the topmost component of the app (or sometimes the bottom-most), the AppBars layout contains three components: leading, title, and actions
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text( 'App Title', ), centerTitle: true, actions: [ IconButton( icon: Icon(Icons.logout), onPressed: () {}, ), ], backgroundColor: Colors.yellowAccent[600], elevation: 50.0, leading: IconButton( icon: Icon(Icons.menu), tooltip: 'Menu Icon', onPressed: () {}, ), //IconButton brightness: Brightness.dark, ), body: const Center( child: Text( "Enjoy", style: TextStyle(fontSize: 24), ), //Text ), //Center ); //Scaffold; }
2- To write any Dart program, be it a script or a Flutter app, you must define a function called main(). It is the entry point of every app.
void main() { var list = ['London', 'Dublin', 'Paris']; list.forEach((item) { print('${list.indexOf(item)}: $item'); }); }
3- You can change the text color of a button when its pressed.
TextButton( onPressed: () {}, style: ButtonStyle( foregroundColor: MaterialStateProperty.resolveWith<Color>( (Set<MaterialState> states) { if (states.contains(MaterialState.pressed)) return Colors.pink; return null; // Defer to the widget's default. }), ), child: Text( 'Change My Color', style: TextStyle(fontSize: 30), ), ),
4- The example below is about running a code after dismissing a GetX dialog.
Get.dialog(MyAlert()).then( (value) => runSomeCode(), );
5- Enums and extension methods can make code cleaner to read. In the example below, we are converting Enums to Strings.
enum Cities { London, Dublin, Paris } extension ToString on Cities { String get name => toString().split('.').last; } void main() { print(Cities.London.name); print(Cities.Dublin.name); print(Cities.Paris.name); }
6- Dart allows you to create a callable class so you can call the instance of the class as a function
class WelcomeToTheCity { // Defining call method String call(String a, String b, String c) => 'Welcome to $a$b$c'; } void main() { var welcome_to_city = WelcomeToTheCity(); // Calling the class through its instance var welcome_msg = welcome_to_city('SanDiego ', 'CA ', 'USA'); print(welcome_msg); }
See you next week.
Follow me on Twitter for more tips about #coding, #learning, #technology...etc.
Check my Apps on Google Play
Cover image Clem Onojeghuo on Unsplash
Original Link: https://dev.to/offlineprogrammer/flutter-dart-tips-week-in-review-5-15be
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To