Flutter的Switch和Slider
2019-10-10 本文已影响0人
王俏
Switch
import 'package:flutter/material.dart';
class SwitchDemo extends StatefulWidget {
@override
_SwitchDemoState createState() => _SwitchDemoState();
}
class _SwitchDemoState extends State<SwitchDemo> {
bool _switchA = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('SwitchDemo'),
elevation: 0.0,
),
body: Container(
padding: EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(_switchA ? '☺' :'😭',style: TextStyle(fontSize: 32.0)),
Switch(
value: _switchA,
onChanged: (value) {
setState(() {
_switchA = value;
});
},
)
],
),
),
);
}
}
SwitchListTile
SwitchListTile(
value: _switchA,
onChanged: (value) {
setState(() {
_switchA = value;
});
},
title: Text('Switch Item A'),
subtitle:Text('Description'),
secondary: _switchA ? Icon(Icons.visibility):Icon(Icons.visibility_off),
selected: _switchA,
)
Slider
Slider(
value: _sliderA,
onChanged: (value) {
setState(() {
_sliderA = value;
});
},
min: 0.0,
max: 10.0,
divisions: 10,
activeColor: Theme.of(context).accentColor,
inactiveColor: Theme.of(context).accentColor.withOpacity(0.3),
label: '${_sliderA.toInt()}',
)