Flutter分段控制器插件segmented_control的使用
Flutter分段控制器插件segmented_control的使用
将一组项目呈现为可选择且可自定义的分段。
特性
- 可以更改边框半径。
- 可以更改分段、文本和边框的颜色。
- 可以获取所选值的索引。
使用方法
以下是一个完整的示例代码,展示了如何在Flutter应用中使用segmented_control
插件。
import 'package:flutter/material.dart';
import 'package:segmented_control/segmented_control.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Segmented Control'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late int _selectedSegment;
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// 分段控制器
SegmentedControl(
segmentCount: 3,
borderRadius: 30,
borderColor: Colors.blueAccent,
segmentColor: Colors.white,
selectedSegmentColor: Colors.blue,
textColor: Colors.blue,
selectedTextColor: Colors.white,
segmentText: const ['一', '二', '三'], // 段落标签
onSelected: (val) {
setState(() {
_selectedSegment = val;
});
},
),
const SizedBox(
height: 20,
),
const Text(
'所选分段索引',
style: TextStyle(fontSize: 18),
),
const SizedBox(
height: 10,
),
Text(
_selectedSegment.toString(),
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
],
),
),
);
}
}
更多关于Flutter分段控制器插件segmented_control的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter分段控制器插件segmented_control的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
segmented_control
是 Flutter 中一个常用的分段控制器插件,它允许你在应用中创建一个分段选择控件,类似于 iOS 中的 UISegmentedControl
。这个插件非常适合用于在多个选项之间进行切换的场景。
安装
首先,你需要在 pubspec.yaml
文件中添加 segmented_control
插件的依赖:
dependencies:
flutter:
sdk: flutter
segmented_control: ^2.0.0
然后运行 flutter pub get
来安装依赖。
基本使用
下面是一个简单的使用 segmented_control
的示例:
import 'package:flutter/material.dart';
import 'package:segmented_control/segmented_control.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Segmented Control Example'),
),
body: Center(
child: SegmentedControlExample(),
),
),
);
}
}
class SegmentedControlExample extends StatefulWidget {
@override
_SegmentedControlExampleState createState() => _SegmentedControlExampleState();
}
class _SegmentedControlExampleState extends State<SegmentedControlExample> {
int _selectedIndex = 0;
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SegmentedControl(
children: {
0: Text('Option 1'),
1: Text('Option 2'),
2: Text('Option 3'),
},
selectedIndex: _selectedIndex,
onValueChanged: (int newIndex) {
setState(() {
_selectedIndex = newIndex;
});
},
),
SizedBox(height: 20),
Text('Selected Option: ${_selectedIndex + 1}'),
],
);
}
}
参数说明
children
: 一个Map<int, Widget>
,用于定义每个分段的内容。键是分段的索引,值是对应的 Widget。selectedIndex
: 当前选中的分段索引。onValueChanged
: 当用户选择不同的分段时触发的回调函数,参数为新的分段索引。controller
: 可选的SegmentedController
,用于控制分段选择器的行为。style
: 可选的SegmentedControlStyle
,用于自定义分段选择器的样式。
自定义样式
你可以通过 style
参数来自定义分段选择器的样式,例如背景颜色、边框颜色、选中颜色等。
SegmentedControl(
children: {
0: Text('Option 1'),
1: Text('Option 2'),
2: Text('Option 3'),
},
selectedIndex: _selectedIndex,
onValueChanged: (int newIndex) {
setState(() {
_selectedIndex = newIndex;
});
},
style: SegmentedControlStyle(
backgroundColor: Colors.grey[200],
selectedColor: Colors.blue,
borderColor: Colors.grey,
borderRadius: BorderRadius.circular(8),
),
);