最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

flutter封裝單選點(diǎn)擊菜單工具欄組件

 更新時(shí)間:2022年05月16日 16:30:52   作者:編程小龍  
這篇文章主要介紹了flutter封裝單選點(diǎn)擊菜單工具欄組件,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

效果展示

CHeckbox多選版 flutter封裝點(diǎn)擊菜單工具欄組件

本文是單選版

效果如圖所示,點(diǎn)擊選項(xiàng)回調(diào)選中的index,可以自定義橫向縱向,傳遞寬高后自動(dòng)計(jì)算子項(xiàng)寬高,自定義邊框、背景、選中的樣式

實(shí)現(xiàn)代碼

第一部分是封裝子項(xiàng)組件, ToolMenuItemWidget組件如下:

import 'dart:core';
import 'package:flutter/material.dart';
/// @author 編程小龍
/// @創(chuàng)建時(shí)間:2022/3/8
/// 工具菜單子項(xiàng)
class ToolMenuItemWidget extends StatelessWidget {
  /// 顯示的title
  final String title;
  /// 當(dāng)前選中
  final int index;
  /// 點(diǎn)擊回調(diào)
  final ValueChanged<int> click;
  final double width;
  final double height;
  final bool isActive;
  final bool isHorizontal; // 是否橫向
  final bool isEnd; // 是否為末尾
  final Color? activeColor; // 點(diǎn)擊后的顏色
  final Color? backgroundColor; // 背景色
  final Color? borderColor; // 邊框色
  final TextStyle? textStyle; // 文字樣式
  final TextStyle? activeTextStyle; //  選中的文字樣式
  const ToolMenuItemWidget({
    Key? key,
    this.isActive = false,
    required this.title,
    required this.index,
    required this.click,
    this.activeColor,
    this.backgroundColor,
    this.borderColor,
    this.textStyle,
    this.activeTextStyle,
    this.isHorizontal = false,
    this.width = 100,
    this.isEnd = false,
    this.height = 40,
  }) : super(key: key);
  @override
  Widget build(BuildContext context) {
    var defaultTextStyle = TextStyle(
        fontSize: 16, color: isActive ? Colors.white : Colors.black87);
    return Material(
      child: Ink( // 點(diǎn)擊右波紋效果
        width: width,
        height: height,
        decoration: BoxDecoration(
            color: isActive
                ? activeColor ?? Theme.of(context).primaryColor
                : backgroundColor ?? Colors.white30,
            border: isHorizontal
                ? isEnd
                    ? const Border()
                    : Border(
                        right: BorderSide(
                            width: 1, color: borderColor ?? Colors.grey))
                : Border(
                    bottom: BorderSide(
                        width: 1, color: borderColor ?? Colors.grey))),
        child: InkWell(
            onTap: () {
              click(index);
            },
            child: Center(
              child: Text(title,
                  style: isActive
                      ? activeTextStyle ?? defaultTextStyle
                      : textStyle ?? defaultTextStyle),
            )),
      ),
    );
  }
}

第二部分是封裝工具欄部分, ToolMenuItemWidget組件如下:

import 'package:demo/widgets/tool_menu_item_widget.dart';
import 'package:flutter/material.dart';
/// @author 編程小龍
/// @創(chuàng)建時(shí)間:2022/3/8
/// 工具菜單
class ToolMenuWidget extends StatefulWidget {
  final List<String> titles;
  final ValueChanged<int> click; // 點(diǎn)擊回調(diào)
  final double? width;
  final double? height;
  final int currentIndex; // 當(dāng)前選中
  final bool isHorizontal; // 橫向
  final Color? activeColor; // 點(diǎn)擊后的顏色 沒(méi)傳取主題色
  final Color? backgroundColor; // 背景色
  final Color? borderColor; // 邊框色
  final TextStyle? textStyle; // 文字樣式
  final TextStyle? activeTextStyle; //  選中的文字樣式
  const ToolMenuWidget(
      {Key? key,
      this.currentIndex = 0,
      required this.titles,
      required this.click,
      this.width,
      this.height,
      this.isHorizontal = false,
      this.activeColor,
      this.backgroundColor,
      this.borderColor,
      this.textStyle,
      this.activeTextStyle,
      })
      : super(key: key);
  @override
  State<ToolMenuWidget> createState() => _ToolMenuWidgetState();
}
class _ToolMenuWidgetState extends State<ToolMenuWidget> {
  int currentIndex = 0; // 當(dāng)前選中
  bool isHorizontal = false; // 是否橫向
  @override
  void initState() {
    // 初始化當(dāng)前選中
    currentIndex = widget.currentIndex;
    isHorizontal = widget.isHorizontal;
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    int index = 0; // 用于遍歷計(jì)數(shù)
    int size = widget.titles.length;
    double height = widget.height ?? (isHorizontal ? 50 : 200); //設(shè)置水平和豎直時(shí)的默認(rèn)值
    double width = widget.width ?? (isHorizontal ? 400 : 100);
    return Container(
      height: height,
      width: width,
      decoration: BoxDecoration(
        color: widget.backgroundColor ?? Colors.white30,
        border: Border.all(color: widget.borderColor ?? Colors.grey, width: 1),
      ),
      child: Wrap(
        children: widget.titles.map((title) {
          return ToolMenuItemWidget(
            title: title,
            index: index,
            isHorizontal: widget.isHorizontal,
            click: (index) {
              setState(() {
                currentIndex = index;
              });
              widget.click(index);
            },
            activeColor: widget.activeColor,
            backgroundColor: widget.backgroundColor,
            borderColor: widget.borderColor,
            textStyle: widget.textStyle,
            height: widget.isHorizontal ? height - 2 : height / size,
            // 豎直狀態(tài)-2 是去掉邊框所占像素
            isActive: index == currentIndex,
            width: widget.isHorizontal ? width / size - 1 : width,
            isEnd: index++ == size - 1,
          );
        }).toList(),
      ),
    );
  }
}

代碼調(diào)用

最簡(jiǎn)單案例只需傳入titles即可,選中顏色默認(rèn)取主題顏色,后續(xù)再弄一個(gè)chekbox版的,可多選菜單

/// 豎向,默認(rèn)樣式
ToolMenuWidget(
   titles: const ["選項(xiàng)1", "選項(xiàng)2", "選項(xiàng)3", "選項(xiàng)4"],
   click: (index) {
     print(" 豎向選中的是 $index");
   },
 ),
/// 自定義樣式橫向
ToolMenuWidget(
  titles: const ["選項(xiàng)1", "選項(xiàng)2", "選項(xiàng)3", "選項(xiàng)4","選項(xiàng)5"],
   isHorizontal: true,
   activeColor: Colors.green,
   backgroundColor: Colors.black,
   textStyle: const TextStyle(color: Colors.white),
   activeTextStyle: const TextStyle(color: Colors.white,fontSize: 18),
   borderColor: Colors.orange,
   click: (index) {
     print("橫向選中的是 $index");
   },
 )

以上就是flutter封裝單選點(diǎn)擊菜單工具欄組件的詳細(xì)內(nèi)容,更多關(guān)于flutter封裝點(diǎn)擊菜單工具欄組件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Android中利用xml文件布局修改Helloworld程序

    Android中利用xml文件布局修改Helloworld程序

    這篇文章主要介紹了Android中利用xml文件布局修改Helloworld程序 的相關(guān)資料,需要的朋友可以參考下
    2016-07-07
  • Android編程開(kāi)發(fā)之TextView文字顯示和修改方法(附TextView屬性介紹)

    Android編程開(kāi)發(fā)之TextView文字顯示和修改方法(附TextView屬性介紹)

    這篇文章主要介紹了Android編程開(kāi)發(fā)之TextView文字顯示和修改方法,結(jié)合實(shí)例詳細(xì)分析了Android中TextView控件關(guān)于文字的顯示及修改技巧,并附帶了TextView屬性介紹,需要的朋友可以參考下
    2015-12-12
  • Android進(jìn)度條控件progressbar使用方法詳解

    Android進(jìn)度條控件progressbar使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Android進(jìn)度條控件progressbar的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android仿美團(tuán)拖拽效果實(shí)例代碼

    Android仿美團(tuán)拖拽效果實(shí)例代碼

    這篇文章主要給大家介紹了關(guān)于Android仿美團(tuán)拖拽效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • 捕獲與解析Android NativeCrash

    捕獲與解析Android NativeCrash

    Android 開(kāi)發(fā)中,NE一直是不可忽略卻又異常難解的一個(gè)問(wèn)題,原因是這里面涉及到了跨端開(kāi)發(fā)和分析,需要同時(shí)熟悉 Java,C&C++,并且需要熟悉 NDK開(kāi)發(fā),并且解決起來(lái)不像 Java異常那么明了,本文為了解決部分疑惑,將從NE的捕獲,解析與還原等三個(gè)方面進(jìn)行探索
    2021-06-06
  • Android開(kāi)發(fā)實(shí)現(xiàn)布局中為控件添加選擇器的方法

    Android開(kāi)發(fā)實(shí)現(xiàn)布局中為控件添加選擇器的方法

    這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)布局中為控件添加選擇器的方法,涉及Android開(kāi)發(fā)中布局設(shè)置的相關(guān)操作技巧,需要的朋友可以參考下
    2017-10-10
  • Android實(shí)現(xiàn)橡皮筋回彈和平移縮放效果

    Android實(shí)現(xiàn)橡皮筋回彈和平移縮放效果

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)橡皮筋回彈和平移縮放效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Android scrollview實(shí)現(xiàn)底部繼續(xù)拖動(dòng)查看圖文詳情

    Android scrollview實(shí)現(xiàn)底部繼續(xù)拖動(dòng)查看圖文詳情

    這篇文章主要為大家詳細(xì)介紹了Android scrollview實(shí)現(xiàn)底部繼續(xù)拖動(dòng)查看圖文詳情,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • Android Koin2基本使用的那件事兒

    Android Koin2基本使用的那件事兒

    這篇文章主要給大家介紹了關(guān)于Android Koin2基本使用的那件事兒,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Android自定義dialog可選擇展示年月日時(shí)間選擇欄

    Android自定義dialog可選擇展示年月日時(shí)間選擇欄

    這篇文章主要介紹了Android自定義dialog可選擇展示年月日時(shí)間選擇欄,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下
    2017-03-03

最新評(píng)論

理塘县| 东宁县| 吐鲁番市| 内丘县| 全州县| 德安县| 五家渠市| 中牟县| 莫力| 长岭县| 曲麻莱县| 大丰市| 微山县| 伊通| 五华县| 策勒县| 棋牌| 孙吴县| 吕梁市| 无棣县| 汕头市| 法库县| 肃宁县| 都安| 乌兰县| 华阴市| 耒阳市| 靖宇县| 淳化县| 泰顺县| 云林县| 蓝山县| 黔西| 黄大仙区| 甘南县| 永新县| 曲阜市| 嘉禾县| 沽源县| 水富县| 双鸭山市|