Flutter_学习记录_基本组件的使用记录_2
1. PopupMenuButton的使用
代码案例:
import 'package:flutter/material.dart';
// ----PopupMemuButtonDemo的案例----
class PopupMemuButtonDemo extends StatefulWidget {
const PopupMemuButtonDemo({super.key});
State<PopupMemuButtonDemo> createState() => _PopupMemuButtonDemoState();
}
class _PopupMemuButtonDemoState extends State<PopupMemuButtonDemo> {
String _currentMenuItem = "Home";
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("PopupMemuButtonDemo"),
),
body: Container(
padding: EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(_currentMenuItem),
PopupMenuButton(
onSelected: (value) {
print(value);
setState(() {
_currentMenuItem = value;
});
},
itemBuilder: (BuildContext context) => [
PopupMenuItem(value: "Home", child: Text("Home")),
PopupMenuItem(value: "Discover", child: Text("Discover") ),
PopupMenuItem(value: "My", child: Text("My"))
],
)
],
)
],
),
),
);
}
}
效果图如下:
2. Checkbox 和 CheckboxListTile 的使用
代码案例:
import 'package:flutter/material.dart';
class CheckBoxDemo extends StatefulWidget {
const CheckBoxDemo({super.key});
State<CheckBoxDemo> createState() => _CheckBoxDemoState();
}
class _CheckBoxDemoState extends State<CheckBoxDemo> {
bool _checkboxItemA = true;
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("CheckBoxDemo"),
),
body: Container(
padding: EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CheckboxListTile(
value: _checkboxItemA,
onChanged: (value){
setState(() {
if (value != null) {
_checkboxItemA = value;
}
});
},
title: Text("Checkbox Item A"),
subtitle: Text("Description"),
secondary: Icon(Icons.bookmark),
selected: _checkboxItemA,
activeColor: Colors.green,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Checkbox(
value: _checkboxItemA,
onChanged: (value) {
setState(() {
if (value != null) {
_checkboxItemA = value;
}
});
},
activeColor: Colors.green,
)
],
)
],
),
),
);
}
}
效果图如下:
3. Radio 和 RadioListTile的使用
代码案例如下:
import 'package:flutter/material.dart';
class RadioDemo extends StatefulWidget {
const RadioDemo({super.key});
State<RadioDemo> createState() => _RadioDemoState();
}
class _RadioDemoState extends State<RadioDemo> {
int _radioGroupA = 0;
void _handleRadioValueChanged (int? value) {
setState(() {
if (value != null) {
_radioGroupA = value;
}
});
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("RadioDemo"),
),
body: Container(
padding: EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("男"),
Radio(
value: 0,
groupValue: _radioGroupA,
onChanged: _handleRadioValueChanged,
activeColor: Colors.green,
),
SizedBox(width: 32.0),
Text("女"),
Radio(
value: 1,
groupValue: _radioGroupA,
onChanged: _handleRadioValueChanged,
activeColor: Colors.green,
),
],
),
SizedBox(height: 32.0),
RadioListTile(
value: 0,
groupValue: _radioGroupA,
onChanged: _handleRadioValueChanged,
activeColor: Colors.green,
title: Text("Option A"),
subtitle: Text("Description"),
secondary: Icon(Icons.filter_1_outlined),
selected: _radioGroupA == 0,
),
RadioListTile(
value: 1,
groupValue: _radioGroupA,
onChanged: _handleRadioValueChanged,
activeColor: Colors.green,
title: Text("Option B"),
subtitle: Text("Description"),
secondary: Icon(Icons.filter_2_outlined),
selected: _radioGroupA == 1,
),
],
),
),
);
}
}
效果图如下:
4. Switch 和 SwitchListTile 的案例
import 'package:flutter/material.dart';
class Switchdemo extends StatefulWidget {
const Switchdemo({super.key});
State<Switchdemo> createState() => _SwitchdemoState();
}
class _SwitchdemoState extends State<Switchdemo> {
bool _switchItemA = false;
void _handleSwitchValueChanged(bool? value) {
setState(() {
if (value != null) {
_switchItemA = value;
}
});
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("SwitchDemo"),
),
body: Container(
padding: EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(_switchItemA ? "已打开" : "未打开", style: TextStyle(fontSize: 24.0)),
Switch(
value: _switchItemA,
onChanged: _handleSwitchValueChanged,
activeColor: Colors.lightGreen,
)
],
),
SwitchListTile(
value: _switchItemA,
onChanged: _handleSwitchValueChanged,
title: Text("Switch Item A"),
subtitle: Text("Description"),
secondary: Icon(_switchItemA ? Icons.visibility : Icons.visibility_off),
selected: _switchItemA,
)
],
),
),
);
}
}
效果图如下:
5. Slider 的使用
代码案例:
import 'package:flutter/material.dart';
class SliderDemo extends StatefulWidget {
const SliderDemo({super.key});
State<SliderDemo> createState() => _SliderDemoState();
}
class _SliderDemoState extends State<SliderDemo> {
double _sliderItemA = 0.0;
void _handleSliderValueChanged(double? value) {
setState(() {
if (value != null) {
_sliderItemA = value;
}
});
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("SliderDemo"),
),
body: Container(
padding: EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Slider(
value: _sliderItemA,
onChanged: _handleSliderValueChanged,
activeColor: Colors.green,
inactiveColor: Colors.grey[100],
min: 0.0, // 设置最小值
max: 10.0, // 设置最大值
divisions: 10, // 分割为几份
label: "${_sliderItemA.toInt()}", // 标签
),
SizedBox(height: 18.0),
Text("SliderValue: $_sliderItemA")
],
),
),
);
}
}
效果图:
6. 时间选择器的简单使用
代码案例:
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'dart:async';
class DatetimeDemo extends StatefulWidget {
const DatetimeDemo({super.key});
State<DatetimeDemo> createState() => _DatetimeDemoState();
}
class _DatetimeDemoState extends State<DatetimeDemo> {
DateTime selectedDate = DateTime.now();
TimeOfDay selectedTime = TimeOfDay(hour: 9, minute: 30);
Future<void> _selectedDate() async {
final DateTime? date = await showDatePicker(
context: context,
initialDate: selectedDate,
firstDate: DateTime(1900),
lastDate: DateTime(2100),
);
if (date != null) {
setState(() {
selectedDate = date;
});
}
}
Future<void> _selectedTime() async {
final TimeOfDay? time = await showTimePicker(
context: context,
initialTime: selectedTime
);
if (time != null) {
selectedTime = time;
}
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("DateTimeDemo"),
),
body: Container(
padding: EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
InkWell(
onTap: _selectedDate,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(DateFormat.yMMMd().format(selectedDate), style: TextStyle(fontSize: 18.0)),
Icon(Icons.arrow_drop_down),
],
),
),
SizedBox(height: 18.0),
InkWell(
onTap: _selectedTime,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(selectedTime.format(context), style: TextStyle(fontSize: 18.0)),
Icon(Icons.arrow_drop_down),
],
),
)
],
),
),
);
}
}
效果图: