Flutter App Development. How to write for IOS and Android at once

Flutter App Development. How to write for IOS and Android at once

Flutter App Development. How to write for IOS and Android at once

Автор: Rap Payne
Дата выхода: 2024
Издательство: Apress Media, LLC.
Количество страниц: 328
Размер файла: 8,3 МБ
Тип файла: PDF
Добавил: codelibs
 Проверить на вирусы

About the Author ..............................................................................xvii
About the Technical Reviewer ...........................................................xix
Who Is This Book For? .......................................................................xxi
Foreword ........................................................................................ xxvii
Preface ............................................................................................ xxix
Chapter 1: Hello Flutter .........................................................................1
What Is Flutter? .....................................................................................................2
Why Flutter? ..........................................................................................................3
The Other Options .................................................................................................4
Native Solutions ....................................................................................................5
Conclusion ............................................................................................................6
Chapter 2: Developing in Flutter ..............................................................................7
The Flutter Toolchain .............................................................................................8
The Flutter SDK ...............................................................................................8
IDEs .................................................................................................................8
IDE DevTools ..................................................................................................10
Emulators ......................................................................................................11
Keeping the Tools Up to Date .........................................................................13
The Flutter Development Process .......................................................................16
Scaffolding the App and Files ........................................................................17
Anatomy of a Flutter Project ..........................................................................17
Running Your App ..........................................................................................19
Running It As a Web App ................................................................................20
Running It on a Tethered Device ....................................................................21
Conclusion ..........................................................................................................23
Chapter 3: Everything Is Widgets ........................................................................25
UI As Code ...........................................................................................................28
Built-In Flutter Widgets .......................................................................................29
Value Widgets ................................................................................................30
Layout Widgets ..............................................................................................31
Navigation Widgets ........................................................................................31
Other Widgets ................................................................................................32
How to Create Your Own Stateless Widgets ........................................................32
Widgets Have Keys .............................................................................................36
Passing a Value into Your Widget ........................................................................37
Stateless and Stateful Widgets ...........................................................................39
So Which One Should I Create? .....................................................................39
Conclusion ..........................................................................................................40
Chapter 4: Value Widgets .................................................................................41
The Text Widget ...................................................................................................41
The Icon Widget ..................................................................................................42
The Image Widget ...............................................................................................44
Embedded Images .........................................................................................45
Network Images ............................................................................................46
Sizing an Image .............................................................................................46
SnackBar Widget .................................................................................................49
Input Widgets ......................................................................................................51
Text Fields .....................................................................................................52
Checkboxes ...................................................................................................56
Radio Buttons ................................................................................................57
Sliders ...........................................................................................................59
Dropdowns ....................................................................................................60
Putting the Form Widgets Together ...............................................................62
One Big Form Example ..................................................................................68
Conclusion ..........................................................................................................68
Chapter 5: Responding to Gestures ..................................................................69
Meet the Button Family .......................................................................................70
ElevatedButton ..............................................................................................72
TextButton and IconButton ............................................................................72
FloatingActionButton (FAB) ............................................................................72
SegmentedButton ..........................................................................................73
CupertinoButton ............................................................................................74
Dismissible ..........................................................................................................74
Custom Gestures for Your Custom Widgets .........................................................75
Step 1: Decide on Your Gestures and Behaviors ............................................76
Step 2: Create Your Custom Widget ...............................................................76
Step 3: Add a GestureDetector widget ...........................................................77
Step 4: Associate Your Gesture with Its Behavior ..........................................78
Example 1: Reacting to a Long Press ............................................................78
Example 2: Pinching to Add a New Item ........................................................80
Example 3: Swiping Left or Right ..................................................................81
Conclusion ..........................................................................................................83
Chapter 6: Navigation and Routing ................................................................85
Stack Navigation .................................................................................................86
Navigating Forward and Back .......................................................................87
Using Anonymous Routes ..............................................................................88
Get Result After a Scene Is Closed ................................................................89
Drawer Navigation ..............................................................................................90
The Drawer Widget ........................................................................................91
Filling the Drawer ..........................................................................................93
Tab Navigation ....................................................................................................95
DefaultTabController ......................................................................................96
TabBarView ....................................................................................................97
TabBar and Tabs ............................................................................................97
TabBar at the Bottom .....................................................................................98
The Dialog Widget ...............................................................................................99
showDialog() and AlertDialog ......................................................................100
Responses with a Dialog .............................................................................101
Navigation Methods Can Be Combined .............................................................102
Chapter 7: Managing State ............................................................................105
What Is State? ...................................................................................................105
The Shape of a StatefulWidget .........................................................................107
The Most Important Rule About State! ..............................................................108
Passing State Down ..........................................................................................109
Lifting State Back Up ........................................................................................110
An Example of State Management ....................................................................112
When Should We Use State? .............................................................................116
Conclusion ........................................................................................................118
Chapter 8: State Management Libraries ...............................................................119
The InheritedWidget ..........................................................................................119
The BLoC Pattern ..............................................................................................120
Some Libraries ..................................................................................................121
ScopedModel ...............................................................................................121
Redux and Hooks .........................................................................................122
Provider .......................................................................................................122
Whoa! That’s a Lot of Packages! ......................................................................122
Raw State ..........................................................................................................123
Riverpod ............................................................................................................124
1. Install flutter_riverpod .............................................................................125
2. Wrap Your App with a ProviderScope ......................................................125
3. Write a Provider .......................................................................................126
4. Inherit from ConsumerStateWidget .........................................................126
5. Read Data with ref.watch( ) .....................................................................127
6. Write Data with ref.read( ) ........................................................................128
Conclusion ........................................................................................................128
Chapter 9: Making RESTful API Calls with HTTP .....................................................129
What Is an API Call? ..........................................................................................130
The Flavors of API Requests ..............................................................................131
Making an HTTP GET or DELETE Request .........................................................132
Making an HTTP PUT, POST, or PATCH Request .................................................134
HTTP Responses to Widgets .............................................................................135
Brute Force – The Easy Way ........................................................................135
FutureBuilder – The Clean Way ...................................................................136
StreamBuilder ..............................................................................................138
Strongly Typed Classes .....................................................................................140
Create a Business Class ..............................................................................140
Write a .fromJSON() Method ........................................................................141
Use .fromJSON() to Hydrate the Object .......................................................141
One Big Example ...............................................................................................142
Overview of the App ....................................................................................143
Create the Flutter App .................................................................................145
Making a Strongly Typed Business Class .........................................................146
list_people.dart ............................................................................................147
A GET Request in Flutter ..............................................................................150
A DELETE Request in Flutter ........................................................................150
upsert_person.dart ......................................................................................151
A POST and PUT Request in Flutter .............................................................152
Conclusion ........................................................................................................153
Chapter 10: Styling with Themes ......................................................................155
Thinking in Flutter Styles ..................................................................................155
Individual Styles ................................................................................................156
Styling Text ..................................................................................................156
Mass-Changing Values ......................................................................................158
Themes .............................................................................................................159
1. ColorScheme ...........................................................................................160
2. TextTheme ...............................................................................................164
3. Widget-Specific Themes ..........................................................................166
4. Put Them Together in a Theme ................................................................167
5. Override the Styles on Individual Widgets ...............................................168
Final Styling Thoughts .......................................................................................169
A Word About Colors ....................................................................................169
Custom Fonts ..............................................................................................171
Conclusion ........................................................................................................174
Chapter 11: Laying Out Your Widgets ....................................................................177
Our Approach ....................................................................................................177
1. Layout the Entire Screen (a.k.a. Scene) .....................................................178
2. Position Widgets Above and Below Each Other or Side by Side ......................178
3. Handle Situations When We Run Out of Space and Overflow
the Scene ................................................................................................180
4. Handle Extra Space in the Scene ............................................................180
5. Make Finer Adjustments in Positioning .....................................................181
Laying Out the Whole Scene .............................................................................181
MaterialApp Widget .....................................................................................182
The Scaffold Widget ....................................................................................183
The AppBar Widget ......................................................................................185
SafeArea Widget ..........................................................................................186
Flutter’s Layout Algorithm .................................................................................187
The Dreaded “Unbounded Height” Error ......................................................187
The Algorithm Itself ...........................................................................................191
Conclusion ........................................................................................................194
Chapter 12: Layout – Positioning Widgets .........................................................195
Putting Widgets Next to or Below Others ..........................................................195
Responsive Design ............................................................................................197
Responsive Design in Flutter .......................................................................197
Flex and MediaQuery ...................................................................................198
Conclusion ........................................................................................................199
Chapter 13: Layout – Fixing Overflows ................................................................201
Overflow Warning Bars .....................................................................................201
Our Options to Correct Overflows .....................................................................202
A Sample Problem to Solve ...............................................................................203
1. Allow the Children to Wrap ............................................................................205
2. Squeeze the Children Until They Fit ..............................................................206
3. Allow the User to Scroll .................................................................................208
SingleChildScrollView Widget ......................................................................209
The ListView Widget ....................................................................................211
GridView Widget ..........................................................................................214
Conclusion ........................................................................................................218
Chapter 14: Layout – Filling Extra Space ...........................................................219
What if There’s Extra Space Left Over? .............................................................220
mainAxisAlignment ...........................................................................................220
crossAxisAlignment .....................................................................................222
IntrinsicWidth ..............................................................................................223
Expanded Widget ..............................................................................................224
Open Space with Expandeds ..............................................................................227
Conclusion ........................................................................................................228
Chapter 15: Layout – Fine-Tuning Positioning .......................................................229
Container Widget and the Box Model ................................................................230
EdgeInsets for Padding and Margin ..................................................................231
Alignment and Positioning Within a Container ..................................................232
Container Sizes Are Not Obvious .......................................................................234
Container Decorations .......................................................................................236
Border ..........................................................................................................239
BorderRadius ...............................................................................................240
BoxShape ....................................................................................................242
Conclusion ........................................................................................................244
Chapter 16: Layout – Special Presentation Widgets .................................................245
Slivers ...............................................................................................................245
Stack Widget .....................................................................................................246
Positioned Widget ........................................................................................248
Card Widget .................................................................................................250
The Table Widget ...............................................................................................251
Conclusion ........................................................................................................254
Appendix A: Dart Language Overview ..............................................255
Appendix B: Futures, Async, and Await ............................................271
Appendix C: Including Packages in Your Flutter App .......................277
Appendix D: How to Work with Files ................................................287
Appendix E: How to Debug Your Layout ............................................297
Index .................................................................................................301

 Create apps for iOS and Android phones and tablets using Flutter, a Google framework that produces two outputs for a single app. Let’s face it, creating iOS apps using Apple tools such as Swift and Xcode is extremely complicated. Creating Android apps with Kotlin and Android Studio is just as difficult. This book breaks down complex concepts and tasks into easily digestible segments with examples, full-color pictures, and hands-on labs with starters and solutions.
 As you work through the accompanying lab exercises, you’ll learn the Dart programming language; the entire Flutter development toolchain; the differences between stateful and stateless widgets; and a working knowledge of the architecture of apps. You’ll work with themes and styles, develop custom widgets, and even teach your app to respond to gestures like taps, swipes, and pinches. You’ll also design, create, and control the layout of your app. Create tools to handle form data entry from users. And ultimately create killer multiscreen apps with navigation, menus, and tabs.
 You’ll see that with Flutter, you can write an app one time that produces two outputs: one that can be submitted to the Apple App Store and another that can be uploaded to the Google Play Store. In short, one code creates the same app that runs on iPhones, iPads, Android phones and tablets, virtually all devices. And if you like, it can also create a version that runs on Macs, Windows, Linux, and in browsers!
 Flutter App Development explains difficult topics in plain, everyday terms that simplify the process of writing mobile apps.

What You’ll Learn

 

  • Get the most out of great Flutter widgets
  • Create custom widgets, both stateless and stateful
  • Exercise expert control over your Flutter layouts
  • Manage data in stateful widgets and with libraries like Riverpod
  • Exchange data actively with RESTful API servers

Who This Book Is For

 Developers who have coded in JavaScript, Python, Java, C#, C++, or any similar language. Managers, product owners, and business analysts who need to understand Flutter’s capabilities.


Похожее:

Список отзывов:

Нет отзывов к книге.