flutter/dev/integration_tests/deferred_components_test/lib/main.dart
Michael Goderbauer 5491c8c146
Auto-format Framework (#160545)
This auto-formats all *.dart files in the repository outside of the
`engine` subdirectory and enforces that these files stay formatted with
a presubmit check.

**Reviewers:** Please carefully review all the commits except for the
one titled "formatted". The "formatted" commit was auto-generated by
running `dev/tools/format.sh -a -f`. The other commits were hand-crafted
to prepare the repo for the formatting change. I recommend reviewing the
commits one-by-one via the "Commits" tab and avoiding Github's "Files
changed" tab as it will likely slow down your browser because of the
size of this PR.

---------

Co-authored-by: Kate Lovett <katelovett@google.com>
Co-authored-by: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com>
2024-12-19 20:06:21 +00:00

99 lines
2.7 KiB
Dart

// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_driver/driver_extension.dart';
import 'component1.dart' deferred as component1;
void main() {
enableFlutterDriverExtension();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Deferred Components Example',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
MyHomePageState createState() => MyHomePageState();
}
class MyHomePageState extends State<MyHomePage> {
Future<void>? libraryFuture;
Widget postLoadDisplayWidget = const Text('placeholder', key: Key('PlaceholderText'));
@override
void initState() {
// Automatically trigger load for release test without driver.
Future<void>.delayed(const Duration(milliseconds: 3000), () {
_pressHandler();
});
super.initState();
}
void _pressHandler() {
if (libraryFuture == null) {
setState(() {
libraryFuture = component1.loadLibrary().then((dynamic _) {
// Delay to give debug runs more than one frame to capture
// the placeholder text.
Future<void>.delayed(const Duration(milliseconds: 750), () {
setState(() {
postLoadDisplayWidget = component1.LogoScreen();
});
});
});
});
}
}
@override
Widget build(BuildContext context) {
final Widget testWidget =
libraryFuture == null
? const Text('preload', key: Key('PreloadText'))
: FutureBuilder<void>(
future: libraryFuture,
builder: (BuildContext context, AsyncSnapshot<void> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
}
return postLoadDisplayWidget;
}
return postLoadDisplayWidget;
},
);
return Scaffold(
appBar: AppBar(title: const Text('Deferred components test')),
body: Center(child: testWidget),
floatingActionButton: FloatingActionButton(
key: const Key('FloatingActionButton'),
onPressed: _pressHandler,
tooltip: 'Load',
child: const Icon(Icons.add),
),
);
}
}