ebook img

Programming Flutter: Native, Cross-Platform Apps the Easy Way PDF

364 Pages·2020·16.664 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Programming Flutter: Native, Cross-Platform Apps the Easy Way

Early Praise for Programming Flutter I’m excited to read this book: ten years ago, I learned Android development reading a PragProg book, and this has changed my life forever. Now this book could change it again. ➤ Giordano Scalzo iOS Senior Software Engineer, NatWest Markets Carmine’s book is a comprehensive journey into Flutter. He leads you through the basics like widgets, standard library, packages, and plugins, and helps you to understand the more advanced topics like network, I/O, navigation, testing, debugging, and firebase. His book is a reference for practical suggestions, useful tips, and solid advice, without sacrificing the details. What I liked the most is that each chapter starts with a premise, and ends with a conclusion and “where we’re going next,” building up a natural, logical, and consequential narrative. ➤ Alessio Salvadorini Creative Technologist, Nokia Great book for anyone who wants to start grasping Flutter. ➤ Erdem Orman Software Development Engineer, Amazon If you want or need to do cross-platform mobile app development, you should have this book at hand. Programming Flutter, by Carmine Zaccagnino, walks you systematically through this important Google platform for building full apps that work with Android, iOS, and Google Fuchsia. ➤ John Barry Independent Editorial Consultant, various Carmine’s hands-on tutorial will help you understand Flutter and why it is the best solution to target iPhones and Androids with a single codebase. Great book for a great framework! ➤ Paul Freiberger Coauthor, Fire in the Valley: The Birth and Death of the Personal Computer Programming Flutter Native, Cross-Platform Apps the Easy Way Carmine Zaccagnino The Pragmatic Bookshelf Raleigh, North Carolina Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade- marks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic books, screencasts, and audio books can help you and your team create better software and have more fun. Visit us at https://pragprog.com. The team that produced this book includes: Publisher: Andy Hunt VP of Operations: Janet Furlow Executive Editor: Dave Rankin Development Editor: Michael Swaine Copy Editor: Jasmine Kwityn Indexing: Potomac Indexing, LLC Layout: Gilson Graphics For sales, volume licensing, and support, please contact [email protected]. For international rights, please contact [email protected]. Copyright © 2020 The Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. ISBN-13: 978-1-68050-695-2 Encoded using the finest acid-free high-entropy binary digits. Book version: P1.0—February 2020 Contents Acknowledgments . . . . . . . . . . . ix Preface . . . . . . . . . . . . . . xi Part I — Getting Started with Flutter 1. Making Your First Flutter App . . . . . . . . . 3 Get Familiar with Dart Syntax and Flutter Classes 3 Give the App Your Personal Touch 13 Where We’re Going Next 25 2. Laying Out More Widgets . . . . . . . . . 27 Introducing Layout Widgets 28 Contain and Add Padding to Widgets Using Invisible Layout Widgets 28 Add Your Own Assets to the App 33 Displaying Images 35 Vertical Layout 41 Horizontal Layout: The Row 59 Making Grids 63 The Card 67 Fill the Space Available in the View Using Expanded 71 Slivers, Custom Scrollables, and Collapsable App Bars 73 The Key 74 Where We’re Going Next 75 3. Building a Calculator App . . . . . . . . . 77 Importing Basic Dependencies 77 Writing main 77 The Calculator’s Home Page 79 How the App Will Be Structured 79 Contents • vi Implement the Basic Layout 80 Let’s Give the Calculator a Screen 82 Add the Deletion Row 83 The Third Row 84 Why a GridView Wouldn’t Work 88 Make the App Look and Work Right with Expanded 88 The Finished Layout 92 Implement the Calculations 98 Use the Calculation Inside the App 107 Wrapping Up the Calculator 108 What If We Try to Divide by 0? 115 Where We’re Going Next 118 4. Beyond the Standard Library: Plugins and Packages . . 119 An Introduction to Packages 119 Package Usage Example 122 Making Your Own Packages 129 Publishing a Package to Pub 137 Packages for Windows, macOS, and Linux 137 Where We’re Going Next 138 Part II — Doing More with Flutter 5. Network and Storage I/O and Navigation . . . . . 141 The API 141 The http Package 143 Writing Methods to Fetch Comics 147 What You Need to Build the UI: Navigation and the InheritedWidget 149 Build the App’s Basic UI 167 Building the Comic Page 173 Using the CircularProgressIndicator 176 Making Everything Faster by Caching to Local Storage 177 Allowing the User to Click the Image and Go to the Website: The url_launcher Package 184 Adding Comic Selection by Number 187 Permanent Data I/O in Flutter: Adding “Starred” Comics 191 Where We’re Going Next 200 Contents • vii 6. Testing and Debugging Flutter Apps . . . . . . 201 Testing 201 Testing the XKCD App: Using Mock Objects 211 Throwing and Catching Exceptions 233 Assert Statements 236 Where We’re Going Next 236 7. Build a Chat App Using Firebase . . . . . . . 237 What Is Firebase? 237 Animations and Transistions 246 Custom Shapes and Drawing in Flutter Apps Using Painters 250 The StreamBuilder 260 Building the Chat App 260 Where You’re Going Next 283 A1. Introduction to Dart . . . . . . . . . . 285 Comments in Dart 285 Variables and Conditions 286 Functions 295 main 297 Asynchronous Code in Dart: The dart:async Library 297 Conditional Constructs and Expressions 303 Loops: while, do while, and for 305 Classes 306 Typedef and Callbacks 311 Mastering import 312 Conversion Between Native Java/Apple and Dart Data Types 313 Where You’re Going Next 314 A2. Apple-Like Look and Additional App Configuration . . . 315 Cupertino Widgets 315 pubspec.yaml 326 Platform-Specific Setup 327 Index . . . . . . . . . . . . . . 331 Acknowledgments This book exists because Flutter was released and such a revolutionary technology needed a way for people to know about it and learn it however they prefer. So, before even talking about the book, it’s necessary to remember how good it is for the developer community that Google has released Flutter and they’re continuing to invest in it to make it even more revolutionary and important every day that passes. Talking about the book, it has to be said that working with the Pragmatic Bookshelf has been great. First of all, I want to thank Andy Hunt, who as a publisher has built a wonderful team: starting from my very first interactions with Brian MacDonald over my proposal to write this book everyone has been thorough, welcoming, and helpful. In particular, I want to thank Michael Swaine who, as the editor of this book, has been encouraging, helpful, and understanding throughout the process, never making me feel pressured and always helping and encouraging me when any issues arose or when I had questions about anything related to the book. When bigger and more technical issues arose, production manager Janet Furlow has fixed them, allowing me to keep working on the content of the book instead. The cover Gilson Graphics made for the book is very nice as a visual metaphor of Flutter’s multi-platform nature. In addition to those I have mentioned, I want to thank everyone else at the Pragmatic Bookshelf who has been involved in making the book as good as it is. I want to thank everybody who has looked at the book and given me feedback, which I have taken on and tried to use to the best of my ability to make the book better for everyone. In particular, the technical reviewers who have been thorough and thoughtful in their feedback and the beta book readers who have taken their time to submit errata: it’s been very important and seeing how thorough they have been has encouraged me to do as much as I could report erratum • discuss

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.