Flutter自定义单选按钮插件crea_radio_button的使用
Flutter自定义单选按钮插件CreaRadioButton的使用
简介
CreaRadioButton
是一个用于创建简单单选按钮组的Flutter插件。它允许你轻松地创建具有不同样式和功能的单选按钮,支持水平或垂直布局、方形或圆形按钮,并且可以自定义选中状态下的样式。
功能
- 支持任意数量的按钮
- 支持水平或垂直布局
- 支持方形或圆形按钮
- 可以更改选中状态下的边框和背景颜色
- 支持取消选中功能
限制
目前该插件仅支持 String
类型的值和按钮标签。选项通过 RadioOption
类传递给 RadioButtonGroup
。
开始使用
要使用此插件,首先需要在 pubspec.yaml
文件中添加 crea_radio_button
作为依赖项。
dependencies:
crea_radio_button: ^latest_version
RadioButtonGroup 示例
基本用法
以下是一个简单的 RadioButtonGroup
示例,展示了如何创建一个带有四个选项的单选按钮组:
import 'package:flutter/material.dart';
import 'package:crea_radio_button/crea_radio_button.dart';
void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('CreaRadioButton Example')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FrequencyContainer(),
SizedBox(height: 20),
AmountContainer(),
],
),
),
),
));
}
class FrequencyContainer extends StatefulWidget {
const FrequencyContainer({Key? key}) : super(key: key);
[@override](/user/override)
_FrequencyContainerState createState() => _FrequencyContainerState();
}
class _FrequencyContainerState extends State<FrequencyContainer> {
String label = "";
List<RadioOption> options = [
RadioOption("MONTHLY", "Monthly"),
RadioOption("YEARLY", "Yearly")
];
[@override](/user/override)
Widget build(BuildContext context) {
return Container(
height: 250,
width: 400,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
border: Border.all(color: Colors.grey.shade200),
color: Colors.grey.shade50,
),
child: Padding(
padding: const EdgeInsets.all(30.0),
child: Column(
children: [
const Text(
"How do you want to pay?",
style: TextStyle(fontSize: 30),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
label,
style: const TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
color: Colors.pink,
),
),
),
const SizedBox(height: 25),
RadioButtonGroup(
options: options,
textStyle: const TextStyle(fontSize: 15, color: Colors.white),
mainAxisAlignment: MainAxisAlignment.center,
selectedColor: Colors.grey.shade400,
mainColor: Colors.grey.shade400,
selectedBorderSide: BorderSide(width: 2, color: Colors.pink.shade300),
buttonWidth: 120,
buttonHeight: 35,
callback: (RadioOption val) {
setState(() {
label = val.label;
});
print(val);
},
),
],
),
),
);
}
}
class AmountContainer extends StatefulWidget {
const AmountContainer({Key? key}) : super(key: key);
[@override](/user/override)
_AmountContainerState createState() => _AmountContainerState();
}
class _AmountContainerState extends State<AmountContainer> {
final int defaultOptionIndex = 3;
late RadioOption selectedOption;
List<RadioOption> options = [
RadioOption("10.0", "10"),
RadioOption("25.0", "25"),
RadioOption("50.0", "50"),
RadioOption("100.0", "100")
];
[@override](/user/override)
void initState() {
super.initState();
selectedOption = options[defaultOptionIndex]; // 设置默认选项
}
[@override](/user/override)
Widget build(BuildContext context) {
return Container(
height: 200,
width: 400,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
border: Border.all(color: Colors.grey.shade200),
color: Colors.grey.shade200,
),
child: Padding(
padding: const EdgeInsets.all(30.0),
child: Column(
children: [
const Text("Select your amount"),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'\$ ${selectedOption.value}',
style: TextStyle(fontSize: 20, color: Colors.pinkAccent.shade200),
),
),
const SizedBox(height: 25),
RadioButtonGroup(
buttonHeight: 35,
buttonWidth: 60,
circular: true,
mainColor: Colors.grey,
selectedColor: Colors.pink.shade400,
preSelectedIdx: defaultOptionIndex,
options: options,
callback: (RadioOption val) {
setState(() {
selectedOption = val;
});
},
),
],
),
),
);
}
}
更多关于Flutter自定义单选按钮插件crea_radio_button的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter自定义单选按钮插件crea_radio_button的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,下面是一个关于如何在Flutter中使用自定义单选按钮插件 crea_radio_button
的代码示例。假设你已经将 crea_radio_button
插件添加到了你的 pubspec.yaml
文件中,并且已经运行了 flutter pub get
来获取依赖。
1. 添加依赖
首先,确保你的 pubspec.yaml
文件中有以下依赖项:
dependencies:
flutter:
sdk: flutter
crea_radio_button: ^最新版本号 # 请替换为实际最新版本号
2. 导入插件
在你的 Dart 文件中导入 crea_radio_button
插件:
import 'package:crea_radio_button/crea_radio_button.dart';
3. 使用插件
下面是一个简单的示例,展示如何在 Flutter 应用中使用 crea_radio_button
插件来创建自定义单选按钮:
import 'package:flutter/material.dart';
import 'package:crea_radio_button/crea_radio_button.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Crea Radio Button Example'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: MyRadioButtonExample(),
),
),
);
}
}
class MyRadioButtonExample extends StatefulWidget {
@override
_MyRadioButtonExampleState createState() => _MyRadioButtonExampleState();
}
class _MyRadioButtonExampleState extends State<MyRadioButtonExample> {
String selectedValue = '';
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Choose an option:', style: TextStyle(fontSize: 20)),
SizedBox(height: 16),
CreaRadioButtonGroup(
onSelected: (value) {
setState(() {
selectedValue = value;
});
},
buttons: [
CreaRadioButtonData(
title: 'Option 1',
value: 'option1',
isSelected: selectedValue == 'option1',
color: Colors.blue,
),
CreaRadioButtonData(
title: 'Option 2',
value: 'option2',
isSelected: selectedValue == 'option2',
color: Colors.green,
),
CreaRadioButtonData(
title: 'Option 3',
value: 'option3',
isSelected: selectedValue == 'option3',
color: Colors.red,
),
],
),
SizedBox(height: 24),
Text('Selected: $selectedValue', style: TextStyle(fontSize: 18)),
],
);
}
}
代码说明
-
导入必要的包:
import 'package:flutter/material.dart'; import 'package:crea_radio_button/crea_radio_button.dart';
-
创建主应用:
void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('Crea Radio Button Example'), ), body: Padding( padding: const EdgeInsets.all(16.0), child: MyRadioButtonExample(), ), ), ); } }
-
创建单选按钮组件:
class MyRadioButtonExample extends StatefulWidget { @override _MyRadioButtonExampleState createState() => _MyRadioButtonExampleState(); } class _MyRadioButtonExampleState extends State<MyRadioButtonExample> { String selectedValue = ''; @override Widget build(BuildContext context) { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ Text('Choose an option:', style: TextStyle(fontSize: 20)), SizedBox(height: 16), CreaRadioButtonGroup( onSelected: (value) { setState(() { selectedValue = value; }); }, buttons: [ CreaRadioButtonData( title: 'Option 1', value: 'option1', isSelected: selectedValue == 'option1', color: Colors.blue, ), CreaRadioButtonData( title: 'Option 2', value: 'option2', isSelected: selectedValue == 'option2', color: Colors.green, ), CreaRadioButtonData( title: 'Option 3', value: 'option3', isSelected: selectedValue == 'option3', color: Colors.red, ), ], ), SizedBox(height: 24), Text('Selected: $selectedValue', style: TextStyle(fontSize: 18)), ], ); } }
在这个示例中,我们创建了一个简单的 Flutter 应用,其中包含一个 CreaRadioButtonGroup
,它包含三个 CreaRadioButtonData
选项。每当用户选择一个选项时,onSelected
回调函数会被调用,并更新 selectedValue
状态。
请确保你已经按照插件的文档正确配置和使用了它,因为不同版本的插件可能有不同的API或配置要求。