hot_reload_project.dart 3.12 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import '../test_utils.dart';
6
import 'project.dart';
7

8
class HotReloadProject extends Project {
9 10
  HotReloadProject({super.indexHtml});

11 12 13 14
  @override
  final String pubspec = '''
  name: test
  environment:
15
    sdk: ">=2.12.0-0 <4.0.0"
16 17 18 19 20 21 22 23 24

  dependencies:
    flutter:
      sdk: flutter
  ''';

  @override
  final String main = r'''
  import 'package:flutter/material.dart';
25
  import 'package:flutter/scheduler.dart';
26 27
  import 'package:flutter/services.dart';
  import 'package:flutter/widgets.dart';
28
  import 'package:flutter/foundation.dart';
29 30 31

  void main() async {
    WidgetsFlutterBinding.ensureInitialized();
32 33
    final ByteData message = const StringCodec().encodeMessage('AppLifecycleState.resumed')!;
    await ServicesBinding.instance!.defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { });
34 35 36 37 38 39 40 41 42
    // See https://github.com/flutter/flutter/issues/86202
    if (kIsWeb) {
      while (true) {
        runApp(MyApp());
        await Future.delayed(const Duration(seconds: 1));
      }
    } else {
     runApp(MyApp());
    }
43
  }
44

45 46
  int count = 1;

47 48 49
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
50 51 52 53
      // This method gets called each time we hot reload, during reassemble.

      // Do not remove the next line, it's uncommented by a test to verify that
      // hot reloading worked:
54 55
      // printHotReloadWorked();

56 57 58 59 60 61 62 63 64
      print('((((TICK $count))))');
      // tick 1 = startup warmup frame
      // tick 2 = hot reload warmup reassemble frame
      // after that there's a post-hot-reload frame scheduled by the tool that
      // doesn't trigger this to rebuild, but does trigger the first callback
      // below, then that callback schedules another frame on which we do the
      // breakpoint.
      // tick 3 = second hot reload warmup reassemble frame (pre breakpoint)
      if (count == 2) {
65 66
        SchedulerBinding.instance!.scheduleFrameCallback((Duration timestamp) {
          SchedulerBinding.instance!.scheduleFrameCallback((Duration timestamp) {
67 68 69 70 71 72 73
            print('breakpoint line'); // SCHEDULED BREAKPOINT
          });
        });
      }
      count += 1;

      return MaterialApp( // BUILD BREAKPOINT
74
        title: 'Flutter Demo',
75
        home: Container(),
76 77 78 79
      );
    }
  }

80
  void printHotReloadWorked() {
81 82 83 84 85 86
    // The call to this function is uncommented by a test to verify that hot
    // reloading worked.
    print('(((((RELOAD WORKED)))))');
  }
  ''';

87 88 89 90 91 92
  Uri get scheduledBreakpointUri => mainDart;
  int get scheduledBreakpointLine => lineContaining(main, '// SCHEDULED BREAKPOINT');

  Uri get buildBreakpointUri => mainDart;
  int get buildBreakpointLine => lineContaining(main, '// BUILD BREAKPOINT');

93 94
  void uncommentHotReloadPrint() {
    final String newMainContents = main.replaceAll(
95
      '// printHotReloadWorked();',
96
      'printHotReloadWorked();',
97
    );
98 99 100 101 102
    writeFile(
      fileSystem.path.join(dir.path, 'lib', 'main.dart'),
      newMainContents,
      writeFutureModifiedDate: true,
    );
103 104
  }
}