1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-08 13:58:47 +00:00
This commit is contained in:
Brett Morgan
2022-05-11 12:48:11 -07:00
committed by GitHub
parent fb00d0a102
commit ccd68f34e2
242 changed files with 1719 additions and 1430 deletions

View File

@@ -8,7 +8,7 @@ import 'package:flutter_module_books/api.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
@@ -30,30 +30,28 @@ class FlutterBookApiHandler extends FlutterBookApi {
@override
void displayBookDetails(Book book) {
assert(
book != null,
'Non-null book expected from FlutterBookApi.displayBookDetails call.',
);
callback(book);
}
}
class BookDetail extends StatefulWidget {
const BookDetail({this.hostApi, this.flutterApi, Key key}) : super(key: key);
const BookDetail({Key? key, this.hostApi, this.flutterApi, this.book})
: super(key: key);
// These are the outgoing and incoming APIs that are here for injection for
// tests.
final HostBookApi hostApi;
final FlutterBookApi flutterApi;
final HostBookApi? hostApi;
final FlutterBookApi? flutterApi;
final Book? book;
@override
_BookDetailState createState() => _BookDetailState();
State<BookDetail> createState() => _BookDetailState();
}
class _BookDetailState extends State<BookDetail> {
Book book;
Book? book;
HostBookApi hostApi;
late HostBookApi hostApi;
FocusNode textFocusNode = FocusNode();
TextEditingController titleTextController = TextEditingController();
@@ -63,6 +61,7 @@ class _BookDetailState extends State<BookDetail> {
@override
void initState() {
super.initState();
book = widget.book;
// This `HostBookApi` class instance lets us make outgoing calls to the
// platform.
@@ -80,19 +79,19 @@ class _BookDetailState extends State<BookDetail> {
// This book model is what we're going to return to Kotlin eventually.
// Keep it bound to the UI.
this.book = book;
titleTextController.text = book.title;
titleTextController.text = book.title ?? '';
titleTextController.addListener(() {
this.book?.title = titleTextController.text;
this.book!.title = titleTextController.text;
});
// Subtitle could be null.
// TODO(gaaclarke): https://github.com/flutter/flutter/issues/59118.
subtitleTextController.text = book.subtitle ?? '';
subtitleTextController.addListener(() {
this.book?.subtitle = subtitleTextController.text;
this.book!.subtitle = subtitleTextController.text;
});
authorTextController.text = book.author;
authorTextController.text = book.author ?? '';
authorTextController.addListener(() {
this.book?.author = authorTextController.text;
this.book!.author = authorTextController.text;
});
});
}));
@@ -123,10 +122,12 @@ class _BookDetailState extends State<BookDetail> {
IconButton(
icon: const Icon(Icons.check),
// Pressing save sends the updated book to the platform.
onPressed: () {
hostApi.finishEditingBook(book);
clear();
},
onPressed: book != null
? () {
hostApi.finishEditingBook(book!);
clear();
}
: null,
),
],
),
@@ -134,70 +135,98 @@ class _BookDetailState extends State<BookDetail> {
// Draw a spinner until the platform gives us the book to show details
// for.
? const Center(child: CircularProgressIndicator())
: Focus(
: BookForm(
book: book!,
focusNode: textFocusNode,
child: ListView(
padding: const EdgeInsets.all(24),
children: [
TextField(
controller: titleTextController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
filled: true,
hintText: "Title",
labelText: "Title",
),
),
const SizedBox(height: 24),
TextField(
controller: subtitleTextController,
maxLines: 2,
decoration: const InputDecoration(
border: OutlineInputBorder(),
filled: true,
hintText: "Subtitle",
labelText: "Subtitle",
),
),
const SizedBox(height: 24),
TextField(
controller: authorTextController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
filled: true,
hintText: "Author",
labelText: "Author",
),
),
const SizedBox(height: 32),
const Divider(),
Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'${book.pageCount} pages ~ published ${book.publishDate}'),
),
),
const Divider(),
const SizedBox(height: 32),
const Center(
child: Text(
'BOOK DESCRIPTION',
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
decoration: TextDecoration.underline,
),
),
),
const SizedBox(height: 12),
Text(
book.summary,
style: TextStyle(color: Colors.grey.shade600, height: 1.24),
),
],
),
authorTextController: authorTextController,
subtitleTextController: subtitleTextController,
titleTextController: titleTextController,
),
);
}
}
class BookForm extends StatelessWidget {
const BookForm({
Key? key,
required this.book,
required this.focusNode,
required this.authorTextController,
required this.subtitleTextController,
required this.titleTextController,
}) : super(key: key);
final Book book;
final FocusNode focusNode;
final TextEditingController titleTextController;
final TextEditingController subtitleTextController;
final TextEditingController authorTextController;
@override
Widget build(BuildContext context) {
return Focus(
focusNode: focusNode,
child: ListView(
padding: const EdgeInsets.all(24),
children: [
TextField(
controller: titleTextController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
filled: true,
hintText: "Title",
labelText: "Title",
),
),
const SizedBox(height: 24),
TextField(
controller: subtitleTextController,
maxLines: 2,
decoration: const InputDecoration(
border: OutlineInputBorder(),
filled: true,
hintText: "Subtitle",
labelText: "Subtitle",
),
),
const SizedBox(height: 24),
TextField(
controller: authorTextController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
filled: true,
hintText: "Author",
labelText: "Author",
),
),
const SizedBox(height: 32),
const Divider(),
Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'${book.pageCount} pages ~ published ${book.publishDate}'),
),
),
const Divider(),
const SizedBox(height: 32),
const Center(
child: Text(
'BOOK DESCRIPTION',
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
decoration: TextDecoration.underline,
),
),
),
const SizedBox(height: 12),
Text(
book.summary ?? '',
style: TextStyle(color: Colors.grey.shade600, height: 1.24),
),
],
),
);
}
}

View File

@@ -2,22 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_module_books/api.dart';
import 'package:pigeon/pigeon.dart';
class Book {
String title;
String subtitle;
String author;
String summary;
String publishDate;
int pageCount;
Thumbnail thumbnail;
}
class Thumbnail {
String url;
}
@FlutterApi()
abstract class FlutterBookApi {
void displayBookDetails(Book book);

View File

@@ -70,7 +70,7 @@ packages:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
version: "1.16.0"
convert:
dependency: transitive
description:
@@ -84,14 +84,14 @@ packages:
name: crypto
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
version: "3.0.2"
fake_async:
dependency: transitive
description:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.0"
file:
dependency: transitive
description:
@@ -110,7 +110,7 @@ packages:
name: flutter_lints
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
version: "2.0.1"
flutter_test:
dependency: "direct dev"
description: flutter
@@ -129,7 +129,7 @@ packages:
name: lints
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
version: "2.0.0"
matcher:
dependency: transitive
description:
@@ -143,7 +143,7 @@ packages:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
version: "0.1.4"
meta:
dependency: transitive
description:
@@ -164,7 +164,7 @@ packages:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
pigeon:
dependency: "direct dev"
description:
@@ -190,7 +190,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
version: "1.8.2"
stack_trace:
dependency: transitive
description:
@@ -225,7 +225,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.8"
version: "0.4.9"
typed_data:
dependency: transitive
description:
@@ -239,7 +239,7 @@ packages:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
version: "2.1.2"
watcher:
dependency: transitive
description:
@@ -255,4 +255,4 @@ packages:
source: hosted
version: "3.1.0"
sdks:
dart: ">=2.14.0 <3.0.0"
dart: ">=2.17.0-206.0.dev <3.0.0"

View File

@@ -6,7 +6,7 @@ description: A Flutter module using the Pigeon package to demonstrate
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
sdk: ">=2.17.0-0 <3.0.0"
dependencies:
flutter:
@@ -16,7 +16,7 @@ dev_dependencies:
pigeon: ">=2.0.2 <4.0.0"
flutter_test:
sdk: flutter
flutter_lints: ^1.0.4
flutter_lints: ^2.0.1
flutter:
uses-material-design: true

View File

@@ -27,7 +27,7 @@ void main() {
await tester.pumpWidget(
MaterialApp(
home: BookDetail(hostApi: mockHostApi),
home: BookDetail(book: Book(), hostApi: mockHostApi),
),
);