Flutter in Action (Eric Windmill) (z-library.sk, 1lib.sk, z-lib.sk)

Author: Eric Windmill

代码

In 2017, consumers downloaded 178 billion apps, and analysts predict growth to 258 billion by 2022. Mobile customers are demanding more—and better—apps, and it’s up to developers like you to write them! Flutter, a revolutionary new cross-platform software development kit created by Google, makes it easier than ever to write secure, high-performance native apps for iOS and Android. Flutter apps are blazingly fast because this open source solution compiles your Dart code to platform-specific programs with no JavaScript bridge! Flutter also supports hot reloading to update changes instantly. And thanks to its built-in widgets and rich motion APIs, Flutter’s apps are not just highly responsive, they’re stunning! about the technology With Flutter, you can build mobile applications using a single, feature-rich SDK that includes everything from a rendering engine to a testing environment. Flutter compiles programs written in Google’s intuitive Dart language to platform-specific code so your iOS and Android games, utilities, and shopping platforms all run like native Java or Swift apps. about the book Flutter in Action teaches you to build professional-quality mobile applications using the Flutter SDK and the Dart programming language. You’ll begin with a quick tour of Dart essentials and then dive into engaging, well-described techniques for building beautiful user interfaces using Flutter’s huge collection of built-in widgets. The combination of diagrams, code examples, and annotations makes learning a snap. As you go, you’ll appreciate how the author makes easy reading of complex topics like routing, state management, and async programming. what's inside Understanding the Flutter approach to the UI All the Dart you need to get started Creating custom animations Testing and debugging about the reader You’ll need basic web or mobile app development skills.

📄 File Format: PDF
💾 File Size: 12.8 MB
7
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
M A N N I N G Eric Windmill Foreword by Ray Rischpater
📄 Page 2
Widget Lifecycles Stateless widget Stateful widget Constructor Constructor build() createState() State object (Mounted) initState() (Dirty state) dispose() (Clean state) Rebuilds when configuration changes When it receives new configuration When internal state changes Produces a state object build() setState()widgetDidUpdate()
📄 Page 3
Flutter in Action ERIC WINDMILL FOREWORD BY RAY RISCHPATER M A N N I N G SHELTER ISLAND
📄 Page 4
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: orders@manning.com ©2020 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Acquisitions editor: Brian Sawyer Manning Publications Co. Development editor: Susanna Kline 20 Baldwin Road Technical development editor: John Guthrie PO Box 761 Review editor: Aleks Dragosavljević Shelter Island, NY 11964 Production editor: Anthony Calcara Copyeditor: Tiffany Taylor and Frances Buran Proofreader: Melody Dolab Technical proofreader: Gonzalo Huerta-Cánepa Typesetter: Gordan Salinovic Cover designer: Marija Tudor ISBN 9781617296147 Printed in the United States of America
📄 Page 5
iii brief contents PART 1 MEET FLUTTER ................................................................1 1 ■ Meet Flutter 3 2 ■ A brief intro to Dart 24 3 ■ Breaking into Flutter 54 PART 2 FLUTTER USER INTERACTION, STYLES, AND ANIMATIONS.........95 4 ■ Flutter UI: Important widgets, themes, and layout 97 5 ■ User interaction: Forms and gestures 129 6 ■ Pushing pixels: Flutter animations and using the canvas 158 PART 3 STATE MANAGEMENT AND ASYNCHRONOUS DART ................189 7 ■ Flutter routing in depth 191 8 ■ Flutter state management 212 9 ■ Async Dart and Flutter and infinite scrolling 236 PART 4 BEYOND FOUNDATIONS..................................................265 10 ■ Working with data: HTTP, Firestore, and JSON 267 11 ■ Testing Flutter apps 292
📄 Page 6
(This page has no text content)
📄 Page 7
v contents foreword xv preface xvii acknowledgments xix about this book xxi about the author xxiv about the cover illustration xxv PART 1 MEET FLUTTER ......................................................1 1 Meet Flutter 3 1.1 Why does Flutter use Dart? 4 1.2 On Dart 5 1.3 Who uses Flutter? 6 1.4 Who should be using Flutter? 6 Teams, project leads, and CTOs 6 ■ Individual developers 7 Code school students and recent CS grads 7 ■ Open source developers 7 ■ People who value speed 7 ■ People who are lazy 7 ■ People who value control 7 1.5 Who this book is for 8
📄 Page 8
CONTENTSvi 1.6 Other mobile development options 8 Native development (iOS and Android) 8 ■ Cross-platform JavaScript options 8 1.7 The immediate benefits of Flutter 10 No JavaScript bridge 10 ■ ■ Compile time 10 ■ Write once, test once, deploy everywhere 10 ■ Code sharing 11 ■ Productivity and collaboration 11 ■ Code maintenance 11 ■ The bottom line: Is Flutter for you? 11 1.8 Future benefits of Flutter: Web apps and desktop apps 12 1.9 A brief intro to how Flutter works 12 Everything is a widget 14 ■ Composing UI with widgets 15 Widget types 16 1.10 Flutter rendering: Under the hood 18 Composing the widget tree and layout 20 ■ Compositing step 21 Paint to the screen 22 1.11 Final note 22 1.12 Summary 23 2 A brief intro to Dart 24 2.1 Hello, Dart! 25 Anatomy of a Dart program 26 ■ Adding more greetings 26 I/O and Dart libraries 28 2.2 Common programming concepts in Dart 29 Intro to Dart’s type system 30 ■ Comments 32 ■ Variables and assignment 33 ■ Operators 34 ■ Null-aware operators 34 2.3 Control flow 36 if and else 37 ■ switch and case 37 ■ Advanced switch usage 38 ■ Loops 40 2.4 Functions 41 Anatomy of a Dart function 41 ■ Parameters 42 ■ Default parameter values 43 ■ Advanced function concepts 43 Lexical scope 45 2.5 Object-oriented programming (in Dart) 45 Classes 46 ■ Constructors 48 ■ Inheritance 49 ■ Factories and named constructors 50 ■ Enumerators 51 2.6 Summary 53
📄 Page 9
CONTENTS vii 3 Breaking into Flutter 54 3.1 Intro to the counter app 55 Flutter project structure 56 ■ Anatomy of a Flutter app 56 Again, everything is a widget 57 ■ The build method 58 The new and const constructors in Flutter 59 ■ Hot reload 59 3.2 Widgets: The widget tree, widget types, and the State object 60 Stateless widgets 61 ■ Stateful widgets 62 ■ setState 64 initState 66 3.3 BuildContext 67 3.4 Enhancing the counter app with the most important widgets 68 RaisedButton 68 3.5 Favor composition in Flutter (over inheritance) 69 What is composition? 69 ■ An example of composition in Flutter 71 3.6 Intro to layout in Flutter 72 Row and Column 72 ■ Layout constraints in Flutter 74 RenderObject 74 ■ RenderObject and constraints 75 RenderBoxes and layout errors 75 ■ Multi-child widgets 76 Icons and the FloatingActionButton 78 ■ Images 80 Container widget 81 3.7 The element tree 83 Elements and widgets 85 ■ Exploring the element tree with an example 86 ■ The element tree and State objects 88 ■ Widget keys 90 3.8 A final note 92 3.9 Summary 93 PART 2 FLUTTER USER INTERACTION, STYLES, AND ANIMATIONS...................................................95 4 Flutter UI: Important widgets, themes, and layout 97 4.1 Setting up and configuring a Flutter app 99 Configuration: pubspec.yaml and main.dart 99 SystemChrome 101
📄 Page 10
CONTENTSviii 4.2 Structural widgets and more configuration 102 MaterialApp widget 102 ■ The Scaffold widget 104 ■ AppBar widget 106 4.3 Styling and themes in Flutter 108 Theme widget 108 ■ MediaQuery and the of method 110 ScreenAwareSize method 111 4.4 Common layout and UI widgets 112 Stack widget 112 ■ Table widget 116 ■ TabBar widget 122 4.5 ListView and builders 126 5 User interaction: Forms and gestures 129 5.1 User interaction and gestures 130 The GestureDetector widget 130 ■ GestureDetector in practice 131 ■ The Dismissible widget 134 5.2 Flutter forms 136 The Form widget 137 ■ GlobalKey<FormState> 138 ■ The structure of the AddCityPage form 138 ■ Implementing the form in the weather app 140 5.3 FormField widgets 141 The TextFormField widget 142 ■ The DropdownFormButton widget 143 ■ Generic form fields 146 5.4 Form UI and working with focus nodes 147 InputDecoration 147 ■ Improving the UI with FocusNodes 149 5.5 Managing form state with form methods 151 Form.onChange 152 ■ FormState.save 153 Form.onWillPop 155 5.6 Summary 157 6 Pushing pixels: Flutter animations and using the canvas 158 6.1 Introducing Flutter animations 159 Tweens 160 ■ Animation curves 161 ■ Ticker providers 162 AnimationController 162 ■ AnimatedWidget 163 ■ Implement ing the animation controller and tween for the background 166 6.2 CustomPainter and the canvas 172 The shapes used to make up the clouds 173 ■ Defining the CustomPainter and the Paint object 173 ■ The CustomPainter paint method 175
📄 Page 11
CONTENTS ix 6.3 Staggered animations, TweenSequence, and built-in animations 179 Creating a custom animation state class 179 ■ Built-in animation widgets: SlideTransition 182 ■ Building animations for the Clouds widget 184 ■ TweenSequence 185 6.4 Reusable custom color transition widgets 187 PART 3 STATE MANAGEMENT AND ASYNCHRONOUS DART ......189 7 Flutter routing in depth 191 7.1 Routing in Flutter 192 The Farmers Market app 192 ■ The app source code 193 7.2 Declarative routing and named routes 193 Declaring routes 194 ■ Navigating to named routes 195 MaterialDrawer widget and the full menu 197 ■ Menu items and the appropriate widgets: ListView and ListItems 198 NavigatorObserver: Highlighting the active route with RouteAware 201 7.3 Routing on the fly 204 MaterialRouteBuilder 204 ■ showSnackBar, showBottomSheet, and the like 205 7.4 Routing animations 209 7.5 Summary 211 8 Flutter state management 212 8.1 Deep dive into StatefulWidgets 213 The widget tree and the element tree 213 ■ The StatefulWidget lifecycle and when to do what 214 8.2 Pure Flutter state management: The InheritedWidget 216 Creating a Central Store wth an InheritedWidget/StatefulWidget team 218 ■ The inheritFromWidgetOfExactType and of methods 218 ■ Use the of method to lift up state 222 State management patterns beyond Flutter 224 8.3 Blocs: Business Logic Components 225 How do blocs work? 227 ■ Implementing the bloc architecture 228 ■ Intro to streams and async Dart 231 Implementing streams in the CartBloc 232 8.4 Summary 235
📄 Page 12
CONTENTSx 9 Async Dart and Flutter and infinite scrolling 236 9.1 Async Dart 237 Future recap 237 ■ The async/await keywords 239 ■ Catching errors with futures 240 ■ Catching errors with try and catch 241 9.2 Sinks and streams (and StreamControllers) 242 Anatomy of the observer pattern with Dart streams 243 Implementing streams 243 ■ Broadcasting streams 245 Higher-order streams 247 9.3 Using streams in blocs 250 Blocs use inputs and outputs 250 ■ Implementing a bloc input 253 9.4 Async Flutter: StreamBuilder 254 9.5 Infinite and custom scrollable widgets 255 CustomScrollView and slivers 256 ■ Catalog widget scroll view 256 The SliverGrid widget 260 ■ Delegates 260 ■ Custom slivers 261 PART 4 BEYOND FOUNDATIONS ........................................265 10 Working with data: HTTP, Firestore, and JSON 267 10.1 HTTP and Flutter 268 HTTP package 269 ■ GET requests 269 10.2 JSON serialization 270 Manual serialization 271 ■ Auto-generated JSON serialization 275 Updating the Todo class 275 ■ Bringing it all together in the UI 277 10.3 Working with Firebase in Flutter 281 Installing Firestore 282 ■ Create a Firestore project 283 Configure your app 283 ■ Add Firebase to your pubspec 286 Using Firestore 286 10.4 Dependency injection 288 10.5 Summary 291 11 Testing Flutter apps 292 11.1 Tests in Flutter 293 Dart unit tests 293 ■ Using mockito to test methods that need external dependencies 297 ■ Flutter widget tests 300 ■ Flutter integration tests 303 ■ Performance profiling integration tests 307 11.2 Accessibility with the semantics widgets 310 11.3 Next steps with Flutter 311
📄 Page 13
CONTENTS xi appendix A Installation: Dart2 313 appendix B The Pub package manager 318 appendix C Flutter for web developers 321 appendix D Flutter for iOS developers 324 appendix E Flutter for Android developers 328 index 331
📄 Page 14
(This page has no text content)
📄 Page 15
xiii foreword One of the things the Flutter team is deeply grateful for is the supportive community of Flutter developers. For nearly any question you may have, you can find an answer on Stack Overflow, Medium, or even someone’s GitHub account. Many answers come with fully working sample code with a license that lets you use the code right in your application. We see this spirit of cooperation and camaraderie as crucial to making you successful with Flutter. Until now, though, there’s been little material that you can actually hold in your hands and work through at your desk or in the evenings as you learn how to use Flut- ter. While blogs, Medium, and online documentation have been a paradigm shift for book publishers, especially in computing, there’s still a need for long-form material on topics, and Flutter is no exception. This is why this book is so important. There are things you can’t get from a five- hundred-word Medium post or a snippet of code on Stack Overflow. Thinking deeply about things like how your application manages its state requires you to understand the platform deeply. In this book, you’ll not only see how to use Flutter, but you’ll understand why using Flutter in the ways Eric and people online say to actually works in practice. Eric covers many of the things that developers have found challenging when moving to Flutter. Between these pages you’ll learn about how layout works, how to build widgets that interact with users, and how to build complex applications that span multiple pages and carry complex application sate. For users new to Dart, there’s an entire chapter on how Dart handles asynchronicity. Because today’s mobile
📄 Page 16
FOREWORDxiv applications are communicating applications, you’ll also see how to handle JSON with HTTP backends, and as a bonus, how to use Firestore to manage data storage. And, to wrap things up, there’s a whole chapter on testing. Throughout, Eric’s taken the time to explain not just what, but why. I urge you to do the same—while you can dip in and out of a chapter to get just the morsel of infor- mation you need, why not pause for a minute and savor the experience of actually holding this book and going deeper? Doing so will make you a better programmer with Flutter and pay dividends elsewhere in your life as you slow down and remember how to not just learn, but master a new technology. I and the entire Flutter team are excited to see what you build with Flutter. Thank you for trusting us with your ideas. —RAY RISCHPATER TECHNICAL PROGRAM MANAGER, FLUTTER GOOGLE
📄 Page 17
xv preface When I started using Flutter in September 2017, it was in an alpha stage. I started using it because my boss told me to. I had no opinions about it because I had never heard of it. I hadn’t even heard of Dart, which had been around for nearly a decade by then. But—and this probably isn’t a spoiler— I got hooked immediately. Not only is the end product of the highest quality, but the development process is perhaps the most enjoyable of any SDK that I’ve used. The tooling, the community, the API, and the Dart language are all a joy to participate in. That’s why I’ve written this book. I legitimately believe that Dart and Flutter are the near-future, gold-standard of application development. And I’ve written a book that I think will get any developer from zero to one with Flutter. This book is half tutorial, half spreading-the-good-word. Nearly two years after starting to use Flutter, I’m now working at my second job that lets me build a Flutter app everyday, and my enthusiasm hasn’t wained. Flutter is the truth. In those two years, Flutter has grown quite a bit. It went from alpha to beta to ver- sion 1, and it’s now stable. Dart went from version 1 to 2, and is now putting a lot of effort into making it an ideal language to write modern UIs in. And now, at the time of this writing, Flutter for web is in technical preview. It looks like it’ll only get more exciting. Flutter is going to keep improving, but the foundation is now set. And that’s why I think this book can really help. No matter how it grows, this book will get you started and build your Flutter foundation.
📄 Page 18
PREFACExvi There is no shortage of resources for learning Flutter. My goal with this book, how- ever, is to cover the process in one go. You’ll learn about Dart a bit, and you’ll learn about Flutter a lot. By the end of the book, you’ll have experience writing a mobile app from scratch. This book covers all of the foundational knowledge you need to write beautiful, buttery-smooth mobile apps with Flutter. I’ll cover UI and layout, ani- mations and styling, network requests, state management, and more.
📄 Page 19
xvii acknowledgments This is the first book I’ve written. One of the things I’ve learned in the process is just how many people are involved. I am truly only one of many, many people who put a lot of work into this. First, I’d like to thank two of my former bosses and colleagues, Matthew Smith and John Ryan. When they hired me at AppTree, I hadn’t heard of Flutter or Dart. And more, I still had (and continue to have) a lot to learn about building software. They taught me everything I know, and were patient the entire time. It is the best job I’ve ever had, and it allowed me to fall in love with Dart and Flutter. I’d like to acknowledge my editor at Manning, Susanna Kline, for two reasons. First, I had no clue about how to write a book. Susanna has been patient, yet per- sistent. She’s also been kind, yet honest. All those qualities certainly allowed me to write the best book I could. And secondly, she really let me explore and write the book I wanted to write. Which is why, at the end of this process, I’m still loving it. I’d like to thank all the reviewers, colleagues, and friends who’ve read the manu- script and given feedback. This includes those who’ve commented over at the Man- ning book forum. I can say with 100% certainty that the book would’ve suffered without the feedback. Specifically, I’d to thank all the reviewers: Andy King, Damian Esteban, David Cabrero Souto, Edwin Kwok, Flavio Diez, Fred Heath, George Onof- rei, Godfred Asamoah, Gonzalo Huerta-Cánepa, Jacob Romero, Joel Kotarski, Jose San Leandro, Kumar Unnikrishnan, Martin Dehnert, Nitin Gode, Paul Brown, Petru Bocsanean, Pietro Maffi, Samuel Bosch, Sander Zegvelt, Serge Simon, Thamizh Arasu, Willis Hampton, and Zorodzayi Mukuya.
📄 Page 20
ACKNOWLEDGMENTSxviii Of course, I have to thank everyone who works on Flutter and Dart, as well as the Flutter community online. This community has been by far the most pleasant, uplift- ing, and friendly tech community I’ve ever been a part of. Lastly, I want to thank the following dogs and cats that I know, who I used as exam- ples through out the book: Nora, Odyn, Ruby, Doug, Harper, Tucker, Yeti, and Rosie. (If you own one of these animals and you’re reading this, you get no royalties. Thank you.)
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now

Recommended for You

Loading recommended books...
Failed to load, please try again later
Back to List