Flutter滚动按钮栏插件scrolling_buttons_bar的使用

Flutter滚动按钮栏插件scrolling_buttons_bar的使用

ScrollingButtonsBar 是一个 Flutter 库,允许你创建带有滚动和选择动画效果的按钮栏。

演示视频

演示视频

完整示例代码

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

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // 这个小部件是你的应用的根
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter 动画按钮栏'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int selectedItemIndex = -1;

  final ScrollController _scrollController = ScrollController();

  @override
  Widget build(BuildContext context) {
    // 计算每个按钮的高度和宽度
    double childHeight = MediaQuery.of(context).size.height / 20;
    double childWidth = MediaQuery.of(context).size.width / 5;
    double iconSize = childHeight * 0.8;
    
    // 定义按钮图标数组
    var toolbarItems = [
      Icon(Icons.home, size: iconSize, color: Colors.red,),
      Icon(Icons.search, size: iconSize, color: Colors.blue,),
      Icon(Icons.notifications, size: iconSize, color: Colors.orange,),
      Icon(Icons.description, size: iconSize, color: Colors.indigoAccent,),
      Icon(Icons.image, size: iconSize, color: Colors.green,),
      Icon(Icons.opacity, size: iconSize, color: Colors.black),
      Icon(Icons.expand, size: iconSize, color: Colors.pinkAccent),
    ];

    return Scaffold(
      extendBody: true,
      extendBodyBehindAppBar: true,
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SafeArea(child: Container()),
      bottomNavigationBar: SafeArea(
        child: ScrollingButtonBar(
          selectedItemIndex: selectedItemIndex,
          scrollController: _scrollController,
          childWidth: childWidth,
          childHeight: childHeight,
          foregroundColor: const Color(0xffc7c7c7),
          radius: 6,
          animationDuration: const Duration(milliseconds: 333),
          children: [for (int i = 0; i < toolbarItems.length; i++) buildSingleItemInToolbar(i, toolbarItems)],
        ),
      ),
    );
  }

  // 构建单个按钮项
  buildSingleItemInToolbar(i, toolbarItems) {
    return ButtonsItem(
        child: toolbarItems[i],
        onTap: () {
          setState(() {
            selectedItemIndex = i;
          });
        });
  }
}

代码说明

  1. 导入必要的库

    import 'package:flutter/material.dart';
    import 'package:scrolling_buttons_bar/scrolling_buttons_bar.dart';
    
  2. 定义主应用类 MyApp

    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
      
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: const MyHomePage(title: 'Flutter 动画按钮栏'),
        );
      }
    }
    
  3. 定义主页类 MyHomePage 和状态类 _MyHomePageState

    class MyHomePage extends StatefulWidget {
      const MyHomePage({Key? key, required this.title}) : super(key: key);
      
      final String title;
      
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int selectedItemIndex = -1;
      final ScrollController _scrollController = ScrollController();
      
      @override
      Widget build(BuildContext context) {
        double childHeight = MediaQuery.of(context).size.height / 20;
        double childWidth = MediaQuery.of(context).size.width / 5;
        double iconSize = childHeight * 0.8;
        
        var toolbarItems = [
          Icon(Icons.home, size: iconSize, color: Colors.red,),
          Icon(Icons.search, size: iconSize, color: Colors.blue,),
          Icon(Icons.notifications, size: iconSize, color: Colors.orange,),
          Icon(Icons.description, size: iconSize, color: Colors.indigoAccent,),
          Icon(Icons.image, size: iconSize, color: Colors.green,),
          Icon(Icons.opacity, size: iconSize, color: Colors.black),
          Icon(Icons.expand, size: iconSize, color: Colors.pinkAccent),
        ];
        
        return Scaffold(
          extendBody: true,
          extendBodyBehindAppBar: true,
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: SafeArea(child: Container()),
          bottomNavigationBar: SafeArea(
            child: ScrollingButtonBar(
              selectedItemIndex: selectedItemIndex,
              scrollController: _scrollController,
              childWidth: childWidth,
              childHeight: childHeight,
              foregroundColor: const Color(0xffc7c7c7),
              radius: 6,
              animationDuration: const Duration(milliseconds: 333),
              children: [for (int i = 0; i < toolbarItems.length; i++) buildSingleItemInToolbar(i, toolbarItems)],
            ),
          ),
        );
      }
    
      buildSingleItemInToolbar(i, toolbarItems) {
        return ButtonsItem(
            child: toolbarItems[i],
            onTap: () {
              setState(() {
                selectedItemIndex = i;
              });
            });
      }
    }
    

更多关于Flutter滚动按钮栏插件scrolling_buttons_bar的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter滚动按钮栏插件scrolling_buttons_bar的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


scrolling_buttons_bar 是一个用于 Flutter 的滚动按钮栏插件,允许你在一个水平滚动的容器中显示多个按钮。这对于需要在一行中显示多个按钮但屏幕空间有限的场景非常有用。

安装

首先,你需要在 pubspec.yaml 文件中添加 scrolling_buttons_bar 依赖:

dependencies:
  flutter:
    sdk: flutter
  scrolling_buttons_bar: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来安装依赖。

基本用法

以下是一个简单的示例,展示如何使用 scrolling_buttons_bar

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Scrolling Buttons Bar Example'),
        ),
        body: Center(
          child: ScrollingButtonsBar(
            buttons: [
              ButtonData(
                icon: Icons.home,
                label: 'Home',
                onPressed: () {
                  print('Home button pressed');
                },
              ),
              ButtonData(
                icon: Icons.settings,
                label: 'Settings',
                onPressed: () {
                  print('Settings button pressed');
                },
              ),
              ButtonData(
                icon: Icons.person,
                label: 'Profile',
                onPressed: () {
                  print('Profile button pressed');
                },
              ),
              ButtonData(
                icon: Icons.notifications,
                label: 'Notifications',
                onPressed: () {
                  print('Notifications button pressed');
                },
              ),
              ButtonData(
                icon: Icons.mail,
                label: 'Mail',
                onPressed: () {
                  print('Mail button pressed');
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

参数说明

  • buttons: 一个 List<ButtonData>,用于定义按钮栏中的按钮。每个 ButtonData 包含以下属性:

    • icon: 按钮的图标。
    • label: 按钮的标签文本。
    • onPressed: 按钮点击时的回调函数。
  • scrollDirection: 滚动方向,默认为 Axis.horizontal

  • buttonWidth: 按钮的宽度,默认为 100.0

  • buttonHeight: 按钮的高度,默认为 50.0

  • buttonSpacing: 按钮之间的间距,默认为 8.0

  • buttonDecoration: 按钮的装饰,例如背景颜色、边框等。

自定义按钮样式

你可以通过 buttonDecoration 参数来自定义按钮的样式。例如:

ScrollingButtonsBar(
  buttons: [
    ButtonData(
      icon: Icons.home,
      label: 'Home',
      onPressed: () {
        print('Home button pressed');
      },
    ),
    // 其他按钮...
  ],
  buttonDecoration: BoxDecoration(
    color: Colors.blue,
    borderRadius: BorderRadius.circular(10),
  ),
)
回到顶部