resolution_awareness.dart 2.48 KB
Newer Older
1 2 3
// Copyright (c) 2016, the Flutter project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
4 5

import 'dart:ui' as ui show window;
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';

class ExampleApp extends StatefulComponent {
  ExampleState createState() => new ExampleState();
}

const List<double> _ratios = const <double>[ 1.0, 1.8, 1.3, 2.4, 2.5, 2.6, 3.9 ];

class ExampleState extends State<ExampleApp> {

  int _index = 0;
  double _ratio = _ratios[0];

  final EdgeDims padding = new EdgeDims.TRBL(
    ui.window.padding.top,
    ui.window.padding.right,
    ui.window.padding.bottom,
    ui.window.padding.left
  );

  void _handlePressed() {
    setState(() {
      _index++;
      _index = _index % _ratios.length;
      _ratio = _ratios[_index];
    });
  }

  Widget build(BuildContext context) {
    const double size = 200.0; // 200 logical pixels
    TextStyle style = new TextStyle(color: const Color(0xFF0000000));
    return new MediaQuery(
      data: new MediaQueryData(
        size: ui.window.size,
        devicePixelRatio: _ratio,
        padding: padding
      ),
      child: new AssetVendor(
        bundle: rootBundle,
        devicePixelRatio: _ratio,
        child: new Material(
          child: new Padding(
            padding: const EdgeDims.symmetric(vertical: 48.0),
            child: new Column(
              children: <Widget>[
                new AssetImage(
                  name: 'assets/2.0x/starcircle.png',
                  height: size,
                  width: size,
                  fit: ImageFit.fill
                ),
                new Text('Image designed for pixel ratio 2.0', style: style),
                new AssetImage(
                  name: 'assets/starcircle.png',
                  height: size,
                  width: size,
                  fit: ImageFit.fill
                ),
                new Text(
                  'Image variant for pixel ratio: ' + _ratio.toString(),
                  style: style
                ),
                new RaisedButton(
                  child: new Text('Change pixel ratio', style: style),
                  onPressed: _handlePressed
                )
              ],
              justifyContent: FlexJustifyContent.spaceBetween
            )
          )
        )
      )
    );
  }
}

main() {
  runApp(new ExampleApp());
}