main.dart 5.66 KB
Newer Older
1 2 3 4
// Copyright 2015 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.

5
import 'package:sky/mojo/activity.dart';
6
import 'package:sky/mojo/asset_bundle.dart';
7
import 'package:sky/painting.dart';
8 9
import 'package:sky/theme/colors.dart' as colors;
import 'package:sky/theme/typography.dart' as typography;
10
import 'package:sky/widgets.dart';
11 12 13 14 15 16 17 18 19 20

AssetBundle _initBundle() {
  if (rootBundle != null)
    return rootBundle;
  const String _kAssetBase = '..';
  return new NetworkAssetBundle(Uri.base.resolve(_kAssetBase));
}

final AssetBundle _bundle = _initBundle();

21
EventDisposition launch(String relativeUrl, String bundle) {
22 23 24 25 26 27
  // TODO(eseidel): This is a hack to keep non-skyx examples working for now:
  Uri productionBase = Uri.parse(
    'https://domokit.github.io/example/demo_launcher/lib/main.dart');
  Uri base = rootBundle == null ? Uri.base : productionBase;
  Uri url = base.resolve(relativeUrl);

28
  ComponentName component = new ComponentName()
29 30
    ..packageName = 'org.domokit.sky.demo'
    ..className = 'org.domokit.sky.demo.SkyDemoActivity';
31
  Intent intent = new Intent()
32 33
    ..action = 'android.intent.action.VIEW'
    ..component = component
34
    ..flags = MULTIPLE_TASK | NEW_DOCUMENT
35 36 37
    ..url = url.toString();

  if (bundle != null) {
38
    StringExtra extra = new StringExtra()
39 40 41 42 43 44
      ..name = 'bundleName'
      ..value = bundle;
    intent.stringExtras = [extra];
  }

  activity.startActivity(intent);
45
  return EventDisposition.processed;
46 47 48
}

class SkyDemo {
49 50 51 52 53 54 55
  SkyDemo({
    name,
    this.href,
    this.bundle,
    this.description,
    this.textTheme,
    this.decoration
56
  }) : name = name, key = new Key(name);
57
  final String name;
58
  final Key key;
59 60 61 62 63
  final String href;
  final String bundle;
  final String description;
  final typography.TextTheme textTheme;
  final BoxDecoration decoration;
64 65 66 67 68 69 70 71 72 73 74 75
}

List<SkyDemo> demos = [
  new SkyDemo(
    name: 'Stocks',
    href: '../../stocks/lib/main.dart',
    bundle: 'stocks.skyx',
    description: 'Multi-screen app with scrolling list',
    textTheme: typography.black,
    decoration: new BoxDecoration(
      backgroundImage: new BackgroundImage(
        image: _bundle.loadImage('assets/stocks_thumbnail.png'),
76
        fit: ImageFit.cover
77 78 79 80 81
      )
    )
  ),
  new SkyDemo(
    name: 'Asteroids',
82
    href: '../../game/lib/main.dart',
83
    bundle: 'game.skyx',
Collin Jackson's avatar
Collin Jackson committed
84
    description: '2D game using sprite sheets',
85 86 87 88
    textTheme: typography.white,
    decoration: new BoxDecoration(
      backgroundImage: new BackgroundImage(
        image: _bundle.loadImage('assets/game_thumbnail.png'),
89
        fit: ImageFit.cover
90 91 92 93 94 95 96
      )
    )
  ),
  new SkyDemo(
    name: 'Fitness',
    href: '../../fitness/lib/main.dart',
    bundle: 'fitness.skyx',
Collin Jackson's avatar
Collin Jackson committed
97
    description: 'Track progress towards healthy goals',
98 99
    textTheme: typography.white,
    decoration: new BoxDecoration(
Collin Jackson's avatar
Collin Jackson committed
100
      backgroundColor: colors.Indigo[500]
101 102 103
    )
  ),
  new SkyDemo(
Collin Jackson's avatar
Collin Jackson committed
104
    name: 'Swipe Away',
105 106
    href: '../../widgets/card_collection.dart',
    bundle: 'cards.skyx',
Collin Jackson's avatar
Collin Jackson committed
107
    description: 'Infinite list of swipeable cards',
108 109
    textTheme: typography.white,
    decoration: new BoxDecoration(
Collin Jackson's avatar
Collin Jackson committed
110
      backgroundColor: colors.RedAccent[200]
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    )
  ),
  new SkyDemo(
    name: 'Interactive Text',
    href: '../../rendering/interactive_flex.dart',
    bundle: 'interactive_flex.skyx',
    description: 'Swipe to reflow the app',
    textTheme: typography.white,
    decoration: new BoxDecoration(
      backgroundColor: const Color(0xFF0081C6)
    )
  ),
  // new SkyDemo(

  //   'Touch Demo', '../../rendering/touch_demo.dart', 'Simple example showing handling of touch events at a low level'),
  new SkyDemo(
    name: 'Minedigger Game',
    href: '../../mine_digger/lib/main.dart',
    bundle: 'mine_digger.skyx',
    description: 'Clone of the classic Minesweeper game',
Collin Jackson's avatar
Collin Jackson committed
131 132 133 134
    textTheme: typography.white,
    decoration: new BoxDecoration(
      backgroundColor: colors.black
    )
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
  ),

  // TODO(jackson): This doesn't seem to be working
  // new SkyDemo('Licenses', 'LICENSES.sky'),
];

const double kCardHeight = 120.0;
const EdgeDims kListPadding = const EdgeDims.all(4.0);

class DemoList extends Component {
  Widget buildCardContents(SkyDemo demo) {
      return new Container(
        decoration: demo.decoration,
        child: new InkWell(
          child: new Container(
Collin Jackson's avatar
Collin Jackson committed
150
            margin: const EdgeDims.only(top: 24.0, left: 24.0),
151 152 153 154 155 156 157 158
            child: new Column([
                new Text(demo.name, style: demo.textTheme.title),
                new Flexible(
                  child: new Text(demo.description, style: demo.textTheme.subhead)
                )
              ],
              alignItems: FlexAlignItems.start
            )
159 160 161 162 163 164
          )
        )
    );
  }

  Widget buildDemo(SkyDemo demo) {
165
    return new GestureDetector(
166
      key: demo.key,
167
      onTap: () => launch(demo.href, demo.bundle),
168 169 170 171 172 173 174 175 176 177 178 179
      child: new Container(
        height: kCardHeight,
        child: new Card(
          child: buildCardContents(demo)
        )
      )
    );
  }

  Widget build() {
    return new ScrollableList<SkyDemo>(
      items: demos,
180
      itemExtent: kCardHeight,
181 182 183 184 185 186 187 188 189 190 191 192 193
      itemBuilder: buildDemo,
      padding: kListPadding
    );
  }
}

class SkyHome extends App {
  Widget build() {
    return new Theme(
      data: new ThemeData(
        brightness: ThemeBrightness.light,
        primarySwatch: colors.Teal
      ),
194 195
      child: new Title(
        title: 'Sky Demos',
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
        child: new Scaffold(
          toolbar: new ToolBar(center: new Text('Sky Demos')),
          body: new Material(
            type: MaterialType.canvas,
            child: new DemoList()
          )
        )
      )
    );
  }
}

void main() {
  runApp(new SkyHome());
}