Flutter自定义切换组件插件custom_switcher的使用

Flutter自定义切换组件插件custom_switcher的使用

安装

在你的 pubspec.yaml 文件中添加依赖:

dependencies:
  custom_switcher: ^1.0.0

然后在你的 Dart 文件中导入该库:

import 'package:custom_switcher/custom_switcher.dart';

如何使用

CustomSwitcher 组件可以让你轻松地创建一个自定义的切换组件。以下是一个简单的示例:

CustomSwitcher(
  children: [
    ItemCustomSwitcher(
      child: Center(child: Text("Cricket", overflow: TextOverflow.ellipsis, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15))),
      icon: Icon(Icons.sunny, color: Colors.amber),
    ),
    ItemCustomSwitcher(
      child: Center(child: Text("Golf", overflow: TextOverflow.ellipsis, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15))),
      icon: Icon(Icons.egg_alt_rounded, color: Colors.teal)
    ),
    ItemCustomSwitcher(
      child: Center(child: Text("Motorsport", overflow: TextOverflow.ellipsis, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15))),
      icon: FlutterLogo()
    ),
  ],
)

完整示例

以下是一个完整的示例,展示了如何使用 CustomSwitcher 组件。

import 'package:custom_switcher/custom_switcher.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var switcherIndex = 0;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("CustomSwitcher"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 3),
              child: CustomSwitcher(
                initialIndex: switcherIndex,
                totalWidth: MediaQuery.of(context).size.width - 6,
                cardUnselectedWidht: MediaQuery.of(context).size.width * 0.20,
                animationCurve: Curves.easeInOutCubicEmphasized,
                children: [
                  ItemCustomSwitcher(
                    child: Center(child: Text("Cricket", overflow: TextOverflow.ellipsis, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15))),
                    icon: Padding(
                      padding: EdgeInsets.symmetric(vertical: 10),
                      child: Image.network("https://cdn-icons-png.flaticon.com/256/5971/5971593.png"),
                    ),
                  ),
                  ItemCustomSwitcher(
                    child: Center(child: Text("Golf", overflow: TextOverflow.ellipsis, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15))),
                    icon: Padding(
                      padding: EdgeInsets.symmetric(vertical: 10),
                      child: Image.network("https://cdn-icons-png.flaticon.com/256/606/606058.png"),
                    ),
                    cardShadowColor: Colors.teal,
                  ),
                  ItemCustomSwitcher(
                    child: Center(child: Text("Motorsport", overflow: TextOverflow.ellipsis, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15))),
                    icon: Padding(
                      padding: EdgeInsets.symmetric(vertical: 10),
                      child: Image.network("https://cdn-icons-png.flaticon.com/256/3089/3089028.png"),
                    ),
                  ),
                ],
                onChangue: (index) {
                  setState(() { this.switcherIndex = index; });
                },
              ),
            )
          ],
        ),
      ),
    );
  }
}

更多关于Flutter自定义切换组件插件custom_switcher的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter自定义切换组件插件custom_switcher的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


custom_switcher 是一个假设的 Flutter 插件,用于自定义切换组件。虽然目前没有名为 custom_switcher 的官方 Flutter 插件,但你可以通过自定义组件来实现类似的功能。以下是一个简单的自定义切换组件的实现示例,你可以根据自己的需求进行扩展。

1. 创建自定义切换组件

首先,创建一个自定义的切换组件。这个组件将包含一个开关按钮和相关的状态管理。

import 'package:flutter/material.dart';

class CustomSwitcher extends StatefulWidget {
  final bool initialValue;
  final ValueChanged<bool>? onChanged;

  const CustomSwitcher({
    Key? key,
    this.initialValue = false,
    this.onChanged,
  }) : super(key: key);

  [@override](/user/override)
  _CustomSwitcherState createState() => _CustomSwitcherState();
}

class _CustomSwitcherState extends State<CustomSwitcher> {
  late bool _isSwitched;

  [@override](/user/override)
  void initState() {
    super.initState();
    _isSwitched = widget.initialValue;
  }

  void _toggleSwitch() {
    setState(() {
      _isSwitched = !_isSwitched;
    });
    widget.onChanged?.call(_isSwitched);
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: _toggleSwitch,
      child: Container(
        width: 60.0,
        height: 30.0,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(15.0),
          color: _isSwitched ? Colors.green : Colors.grey,
        ),
        child: Stack(
          children: [
            AnimatedPositioned(
              duration: const Duration(milliseconds: 200),
              curve: Curves.easeInOut,
              left: _isSwitched ? 30.0 : 0.0,
              child: Container(
                width: 30.0,
                height: 30.0,
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  color: Colors.white,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

2. 使用自定义切换组件

在你的应用程序中使用这个自定义切换组件。你可以通过 onChanged 回调来监听开关状态的变化。

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Custom Switcher Example'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              CustomSwitcher(
                initialValue: false,
                onChanged: (value) {
                  print('Switch is $value');
                },
              ),
              SizedBox(height: 20),
              CustomSwitcher(
                initialValue: true,
                onChanged: (value) {
                  print('Switch is $value');
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}
回到顶部