xxxxxxxxxx
import 'package:flutter/material.dart';
class SwitchButtonDemo extends StatefulWidget {
@override
_SwitchButtonDemoState createState() => _SwitchButtonDemoState();
}
class _SwitchButtonDemoState extends State<SwitchButtonDemo> {
bool _switchValue = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Switch Button Demo'),
),
body: Center(
child: Switch(
value: _switchValue,
onChanged: (value) {
setState(() {
_switchValue = value;
});
},
),
),
);
}
}
void main() {
runApp(SwitchButtonDemo());
}
xxxxxxxxxx
bool isChecked = false;
Switch(
value: isChecked,
onChanged: (bool value) {
setState(() {
isChecked = value;
});
},
),
xxxxxxxxxx
String commentMark(int mark) {
switch (mark) {
case 0 : // Enter this block if mark == 0
return "mark is 0" ;
case 1:
case 2:
case 3: // Enter this block if mark == 1 or mark == 2 or mark == 3
return "mark is either 1, 2 or 3" ;
// etc.
default :
return "mark is not 0, 1, 2 or 3" ;
}
}
xxxxxxxxxx
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool switchValue = false;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Switch Example'),
),
body: Container(
padding: EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Switch'),
SizedBox(width: 16.0),
Switch(
value: switchValue,
onChanged: (newValue) {
setState(() {
switchValue = newValue;
});
},
),
],
),
),
),
);
}
}