1

original

https://medium.com/flutterdevs/explore-multi-select-items-in-flutter-a90665e17be

refer to

text

Flutter widgets are built using modern frameworks. This is like a reaction. Here, we start with widgets to create any application. Each component on the screen is a small part. This widget describes what his prospects should look like based on his current configuration and status. The widget display is similar to its idea and current settings and status.

Flutter automated testing enables you to meet the high responsiveness of your application because it helps to find bugs and various problems in your application. Flutter is a tool for developing mobile, desktop, web applications and code\& is a free and open source tool. Flutter has the ability to easily test any application. This ability is that they work the way we want on the target platform. Dart testing is suitable for unit testing and non-ui testing; it runs on the development machine and does not depend on the GUI of the Flutter application.

In this blog, we will explore the Flutter multi-choice project. We will also implement a demonstration of multi-project selection in Flutter. How to use them in your Flutter application. So let's get started.

  • Multiple Choices | Flutter Packaging

https://pub.dev/packages/multi_select_item

Multiple selection items:

Multiple options This is a Flutter library that handles multiple options. Using this library, we create a list. When we can delete items in this list, use this widget. We wrap it in a list view builder project so that we can Easily create a multi-choice list.

implementation plan:

You need to implement it in your code separately:

Step 1: Add dependencies.

Add dependencies to the pubspec ーyaml file.

First, add flutter localization and intl library in pubspec.yaml.

dependencies:
  multi_select_item: ^1.0.3

Step 2: Import package:

import 'package:multi_select_item/multi_select_item.dart';

Step 3: Enable AndriodX

org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true

Code steps:

To achieve this, you need to write "First define the controller".

MultiSelectController controller = new MultiSelectController();

After this, we create a list, the list defines its value in initState (), and after that, the set length of the controller is determined according to the length of the list. Reference code below is included.

@override
void initState() {
  super.initState();

  multiSelectList.add({"images": 'assets/images/resort_1.jpg', "desc":"Welcome to New York City!"});
  multiSelectList.add({"images":'assets/images/resort_2.jpg' ,"desc":"Welcome to Los Angeles!"});
  multiSelectList.add({"images":'assets/images/resort_3.jpg' ,"desc":"Welcome to Chicago!"});
  multiSelectList.add({"images":'assets/images/resort_4.jpg', "desc":"Welcome to Houston!"});

  controller.disableEditingWhenNoneSelected = true;
  controller.set(multiSelectList.length);
}

After this, we use a ListViewBuilder widget, where we use the MultiSelectItem () widget, where we use the card and its sub-attributes, and we use the row widget, where we initialize the list value defined above, the list value It has images and text, and the controller listed on its onSelected () is defined inside setState and is used to select values.

ListView.builder(
  itemCount: multiSelectList.length,
  itemBuilder: (context, index) {
    return MultiSelectItem(
      isSelecting: controller.isSelecting,
      onSelected: () {
        setState(() {
          controller.toggle(index);
        });
      },
      child:Container(
        height:75,
        margin: EdgeInsets.only(left:15,right:15,top:15),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(12),
          color: Colors._transparent_,
        ),
        child:Card(
          color:controller.isSelected(index)
              ? Colors._grey_.shade200:Colors._white_,
          shape: RoundedRectangleBorder(

            borderRadius: BorderRadius.all(Radius.circular(8.0)),

          ),
          child:Padding(
            padding:EdgeInsets.symmetric(vertical:10, horizontal: 12),
            child: Row(
              children: [
                _//contentPadding: EdgeInsets.symmetric(vertical: 12, horizontal: 16),_ ClipRRect(
                  borderRadius: BorderRadius.circular(12),
                  child:Image.asset(multiSelectList[index]['images'],fit:BoxFit.cover,width:60,height:60,),
                ),
                SizedBox(width:20,),
                Text(multiSelectList[index]['desc'], style: TextStyle(fontSize:14)),
              ],
            ),
          ),
        ),
      ),
    );
  },
),

After that, we will delete the selected items to delete this we take forEach and sort the value, which will sort our selected items first from the largest id to the smallest id, and then it will delete it one by one.

void delete() {
  var list = controller.selectedIndexes;
  list.sort((b, a) =>
      a.compareTo(b));
  list.forEach((element) {
    multiSelectList.removeAt(element);
  });

  setState(() {
    controller.set(multiSelectList.length);
  });
}

Full code:

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

class MultiSelectListDemo extends StatefulWidget {
  @override
  _MultiSelectListDemoState createState() => new _MultiSelectListDemoState();
}

class _MultiSelectListDemoState extends State<MultiSelectListDemo> {


  List multiSelectList = [];


  MultiSelectController controller = new MultiSelectController();

  @override
  void initState() {
    super.initState();

    multiSelectList.add({"images": 'assets/images/resort_1.jpg', "desc":"Welcome to New York City!"});
    multiSelectList.add({"images":'assets/images/resort_2.jpg' ,"desc":"Welcome to Los Angeles!"});
    multiSelectList.add({"images":'assets/images/resort_3.jpg' ,"desc":"Welcome to Chicago!"});
    multiSelectList.add({"images":'assets/images/resort_4.jpg', "desc":"Welcome to Houston!"});
    multiSelectList.add({"images":'assets/images/sanfrancisco.jpg', "desc":"Welcome to San francisco!"});

    controller.disableEditingWhenNoneSelected = true;
    controller.set(multiSelectList.length);
  }

  void add() {
    multiSelectList.add({"images": multiSelectList.length});
    multiSelectList.add({"desc": multiSelectList.length});

    setState(() {
      controller.set(multiSelectList.length);
    });
  }

  void delete() {
    var list = controller.selectedIndexes;
    list.sort((b, a) =>
        a.compareTo(b));
    list.forEach((element) {
      multiSelectList.removeAt(element);
    });

    setState(() {
      controller.set(multiSelectList.length);
    });
  }

  void selectAll() {
    setState(() {
      controller.toggleAll();
    });
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        var before = !controller.isSelecting;
        setState(() {
          controller.deselectAll();
        });
        return before;
      },
      child: new Scaffold(
        appBar: new AppBar(
          title: new Text('Selected ${controller.selectedIndexes.length}' ),
          actions: (controller.isSelecting)
              ? <Widget>[
            IconButton(
              icon: Icon(Icons._select_all_),
              onPressed: selectAll,
            ),
            IconButton(
              icon: Icon(Icons._delete_),
              onPressed: delete,
            )
          ]
              : <Widget>[],
        ),
        body:Container(

          child: ListView.builder(
            itemCount: multiSelectList.length,
            itemBuilder: (context, index) {
              return InkWell(
                onTap: () {},
                child: MultiSelectItem(
                  isSelecting: controller.isSelecting,
                  onSelected: () {
                    setState(() {
                      controller.toggle(index);
                    });
                  },
                  child:Container(
                      height:75,
                    margin: EdgeInsets.only(left:15,right:15,top:15),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(12),
                      color: Colors._transparent_,
                    ),
                    child:Card(
                      color:controller.isSelected(index)
                          ? Colors._grey_.shade200:Colors._white_,
                      shape: RoundedRectangleBorder(

                        borderRadius: BorderRadius.all(Radius.circular(8.0)),

                      ),
                      child:Padding(
                        padding:EdgeInsets.symmetric(vertical:10, horizontal: 12),
                        child: Row(
                          children: [
                            _//contentPadding: EdgeInsets.symmetric(vertical: 12, horizontal: 16),_ ClipRRect(
                              borderRadius: BorderRadius.circular(12),
                              child:Image.asset(multiSelectList[index]['images'],fit:BoxFit.cover,width:60,height:60,),
                            ),
                            SizedBox(width:20,),
                            Text(multiSelectList[index]['desc'], style: TextStyle(fontSize:14)),
                          ],
                        ),
                      ),
                    ),
                  ),
                ),
              );
            },
          ),
        ),
      ),
    );
  }
}

Conclusion:

In this article, I explained Explore GetIt In Flutter. You can modify and experiment according to your own. This small introduction comes from Explore GetIt In Flutter's demo from our side.

I hope this blog will provide you with sufficient information, while trying to explore GetIt in Flutter in your Flutter project. We showed you what exploration is and what Flutter is working in your Flutter app, so please try it.

If I did something wrong, please let me know in the comments and I am happy to improve.


© Cat brother

Past

Open source

GetX Quick Start

https://github.com/ducafecat/getx_quick_start

News client

https://github.com/ducafecat/flutter_learn_news

strapi manual translation

https://getstrapi.cn

WeChat discussion group ducafecat

Series collection

Translation

https://ducafecat.tech/categories/%E8%AF%91%E6%96%87/

Open source project

https://ducafecat.tech/categories/%E5%BC%80%E6%BA%90/

Dart programming language basics

https://space.bilibili.com/404904528/channel/detail?cid=111585

Getting started with Flutter zero foundation

https://space.bilibili.com/404904528/channel/detail?cid=123470

Flutter actual combat from scratch news client

https://space.bilibili.com/404904528/channel/detail?cid=106755

Flutter component development

https://space.bilibili.com/404904528/channel/detail?cid=144262

Flutter Bloc

https://space.bilibili.com/404904528/channel/detail?cid=177519

Flutter Getx4

https://space.bilibili.com/404904528/channel/detail?cid=177514

Docker Yapi

https://space.bilibili.com/404904528/channel/detail?cid=130578


独立开发者_猫哥
666 声望126 粉丝