cupertino_segmented_control_demo.dart 4.06 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/cupertino.dart';
import 'package:flutter/material.dart';

8 9
import '../../gallery/demo.dart';

10 11 12 13 14 15 16 17
const Color _kKeyUmbraOpacity = Color(0x33000000); // alpha = 0.2
const Color _kKeyPenumbraOpacity = Color(0x24000000); // alpha = 0.14
const Color _kAmbientShadowOpacity = Color(0x1F000000); // alpha = 0.12

class CupertinoSegmentedControlDemo extends StatefulWidget {
  static const String routeName = 'cupertino/segmented_control';

  @override
18
  _CupertinoSegmentedControlDemoState createState() => _CupertinoSegmentedControlDemoState();
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
}

class _CupertinoSegmentedControlDemoState extends State<CupertinoSegmentedControlDemo> {
  final Map<int, Widget> children = const <int, Widget>{
    0: Text('Midnight'),
    1: Text('Viridian'),
    2: Text('Cerulean'),
  };

  final Map<int, Widget> icons = const <int, Widget>{
    0: Center(
      child: FlutterLogo(
        colors: Colors.indigo,
        size: 200.0,
      ),
    ),
    1: Center(
      child: FlutterLogo(
        colors: Colors.teal,
        size: 200.0,
      ),
    ),
    2: Center(
      child: FlutterLogo(
        colors: Colors.cyan,
        size: 200.0,
      ),
    ),
  };

  int sharedValue = 0;

  @override
  Widget build(BuildContext context) {
xster's avatar
xster committed
53 54 55 56 57 58 59 60
    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        middle: const Text('Segmented Control'),
        // We're specifying a back label here because the previous page is a
        // Material page. CupertinoPageRoutes could auto-populate these back
        // labels.
        previousPageTitle: 'Cupertino',
        trailing: CupertinoDemoDocumentationButton(CupertinoSegmentedControlDemo.routeName),
61
      ),
xster's avatar
xster committed
62 63 64 65 66 67 68
      child: DefaultTextStyle(
        style: CupertinoTheme.of(context).textTheme.textStyle,
        child: SafeArea(
          child: Column(
            children: <Widget>[
              const Padding(
                padding: EdgeInsets.all(16.0),
69
              ),
xster's avatar
xster committed
70 71 72 73 74 75 76 77 78 79
              SizedBox(
                width: 500.0,
                child: CupertinoSegmentedControl<int>(
                  children: children,
                  onValueChanged: (int newValue) {
                    setState(() {
                      sharedValue = newValue;
                    });
                  },
                  groupValue: sharedValue,
80
                ),
xster's avatar
xster committed
81 82 83 84 85 86 87 88 89 90 91
              ),
              Expanded(
                child: Padding(
                  padding: const EdgeInsets.symmetric(
                    vertical: 32.0,
                    horizontal: 16.0,
                  ),
                  child: Container(
                    padding: const EdgeInsets.symmetric(
                      vertical: 64.0,
                      horizontal: 16.0,
92
                    ),
xster's avatar
xster committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
                    decoration: BoxDecoration(
                      color: CupertinoTheme.of(context).scaffoldBackgroundColor,
                      borderRadius: BorderRadius.circular(3.0),
                      boxShadow: const <BoxShadow>[
                        BoxShadow(
                          offset: Offset(0.0, 3.0),
                          blurRadius: 5.0,
                          spreadRadius: -1.0,
                          color: _kKeyUmbraOpacity,
                        ),
                        BoxShadow(
                          offset: Offset(0.0, 6.0),
                          blurRadius: 10.0,
                          spreadRadius: 0.0,
                          color: _kKeyPenumbraOpacity,
                        ),
                        BoxShadow(
                          offset: Offset(0.0, 1.0),
                          blurRadius: 18.0,
                          spreadRadius: 0.0,
                          color: _kAmbientShadowOpacity,
                        ),
                      ],
116
                    ),
xster's avatar
xster committed
117 118
                    child: icons[sharedValue],
                  ),
119 120
                ),
              ),
xster's avatar
xster committed
121
            ],
122
          ),
xster's avatar
xster committed
123
        ),
124 125 126 127
      ),
    );
  }
}