hot_reload_project.dart 2.48 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2018 The Chromium 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 'package:flutter_tools/src/base/file_system.dart';

import '../test_utils.dart';
8
import 'project.dart';
9

10
class HotReloadProject extends Project {
11 12 13 14 15 16 17 18 19 20 21 22 23 24
  @override
  final String pubspec = '''
  name: test
  environment:
    sdk: ">=2.0.0-dev.68.0 <3.0.0"

  dependencies:
    flutter:
      sdk: flutter
  ''';

  @override
  final String main = r'''
  import 'package:flutter/material.dart';
25
  import 'package:flutter/scheduler.dart';
26

27
  void main() => runApp(MyApp());
28

29 30
  int count = 1;

31 32 33
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
34 35 36 37
      // 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:
38 39
      // printHotReloadWorked();

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
      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) {
        SchedulerBinding.instance.scheduleFrameCallback((Duration timestamp) {
          SchedulerBinding.instance.scheduleFrameCallback((Duration timestamp) {
            print('breakpoint line'); // SCHEDULED BREAKPOINT
          });
        });
      }
      count += 1;

      return MaterialApp( // BUILD BREAKPOINT
58
        title: 'Flutter Demo',
59
        home: Container(),
60 61 62 63
      );
    }
  }

64
  void printHotReloadWorked() {
65 66 67 68 69 70
    // The call to this function is uncommented by a test to verify that hot
    // reloading worked.
    print('(((((RELOAD WORKED)))))');
  }
  ''';

71 72 73 74 75 76
  Uri get scheduledBreakpointUri => mainDart;
  int get scheduledBreakpointLine => lineContaining(main, '// SCHEDULED BREAKPOINT');

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

77 78
  void uncommentHotReloadPrint() {
    final String newMainContents = main.replaceAll(
79
      '// printHotReloadWorked();',
80
      'printHotReloadWorked();',
81
    );
82 83 84
    writeFile(fs.path.join(dir.path, 'lib', 'main.dart'), newMainContents);
  }
}