flutter switch 切换 仅做记录

import 'package:flutter/material.dart';
void main() => runApp(MyApp());


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return  MaterialApp(
      title: 'Navigation',
      initialRoute: '/switch',
      routes: {
//        '/': (BuildContext context) =>  Home(),
//        '/search': (BuildContext context) =>  SearchBarDemo(),
        '/switch': (BuildContext context) =>  SwitchDemo(),
//        '/scrollMap': (BuildContext context) =>  scrollMap(),
      },
    );
  }
}


class SwitchDemo extends StatefulWidget {
  @override
  State createState() => FirstScreen();
}

class FirstScreen extends State {
  bool check = true;
  @override
  Widget build(BuildContext context) {
    return  Scaffold(
      appBar:  AppBar(
        title:  Text('First Screen'),
      ),
      body:  Column(
        children: [
          Container(
            child: Center(
              child: Container(
                child: GestureDetector(
                  child: new Text(
                      'hello',
                      style: new TextStyle(
                        color: Colors.white,
                        fontSize: 24.0,
                        fontWeight: FontWeight.w900,
                      )),
                ),
                width: 400,
                height: 200,
              ),
            ),
            width: 400,
            color: Colors.red,
          ),
          Switch(
            value: check,
            activeColor: Colors.red,
            activeTrackColor: Colors.blue,
            inactiveThumbColor: Colors.green,
            inactiveTrackColor: Colors.orange,    // 激活时原点颜色
            materialTapTargetSize: MaterialTapTargetSize.padded,
            onChanged: (bool val) {
              setState(() {
                check = !check;
                if (check) {
                  print(check);
                }
              });
            },
          )
        ],

      ),
    );
  }
}

 

你可能感兴趣的:(flutter)