【问题标题】:Flutter how to use drag and drop on containerFlutter如何在容器上使用拖放
【发布时间】:2021-08-06 18:57:29
【问题描述】:

我有我的小部件,我需要像拖放一样使用它。就像我可以将其位置替换为一到二或二到四并重新排序索引。

我的代码

  var BlocksList = [
    {'Name': 'Get Out', 'Link': 'https://google.com'},
    {'Name': 'Get IN', 'Link': 'https://google.com'}
  ];

  ListView.builder(
    shrinkWrap: true,
    itemCount: BlocksList.length,
    itemBuilder: (context, i) {
      return Padding(
        padding: const EdgeInsets.only(
            left: 13, right: 13, bottom: 13),
        child: Container(
          child: Row(
            children: [
              Container(
                  child: SvgPicture.asset(
                      'images/menu.svg')),
              SizedBox(
                width: 14,
              ),
              Expanded(
                child: Container(
                  height: Height * 0.07,
                  decoration: BoxDecoration(
                      borderRadius:
                          BorderRadius.circular(5),
                      border: Border.all(
                          color: Color(0xffE6E6E6))),
                  child: Padding(
                    padding: const EdgeInsets.only(
                        left: 13, right: 13),
                    child: Row(
                        mainAxisAlignment:
                            MainAxisAlignment
                                .spaceBetween,
                        children: [
                          Row(
                            children: [
                              Container(
                                  child: SvgPicture.asset(
                                      'images/clip.svg')),
                              SizedBox(
                                width: 7,
                              ),
                              Column(
                                mainAxisAlignment:
                                    MainAxisAlignment
                                        .center,
                                crossAxisAlignment:
                                    CrossAxisAlignment
                                        .start,
                                children: [
                                  Text(
                                    BlocksList[i]['Name'].toString(),
                                    style: TextStyle(
                                      fontSize: 16,
                                      fontFamily:
                                          'SegoeUI-SemiBold',
                                      color:
                                          textGreyColor,
                                    ),
                                  ),
                                  SizedBox(
                                    height: 4,
                                  ),
                                  Text(
                                    BlocksList[i]['Link'].toString(),
                                    style: TextStyle(
                                        fontSize: 12,
                                        fontFamily:
                                            'SemiBold',
                                        color:
                                            textGreyColor),
                                  )
                                ],
                              ),
                            ],
                          ),
                          FlutterSwitch(
                            width: 52,
                            height: 32.5,
                            valueFontSize: 12.0,
                            toggleSize: 20.0,
                            toggleColor: _switchValue
                                ? Colors.white
                                : Color(0xffE6E6E6),
                            switchBorder: Border.all(
                              color: Color(0xffE6E6E6),
                              width: _switchValue
                                  ? 0
                                  : 1.0,
                            ),
                            activeColor: kPrimaryColor,
                            inactiveColor: Colors.white,
                            value: _switchValue,
                            onToggle: (val) {
                              setState(() {
                                _switchValue = val;
                              });
                            },
                          )
                        ]),
                  ),
                ),
              ),
            ],
          ),
        ),
      );
    })

它看起来像这样

我需要做的是添加 drop 和 drop,以便我可以将第二个容器移动到第一个容器,依此类推。找不到任何只是拖动自定义容器的小部件

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    以上答案也是正确的,只是不适合您的代码。如上所述,您需要使用ReorderableListView

    所以你的代码会是这样的

       Container(
        height: 300,
        child: ReorderableListView(
          children: <Widget>[
            for (int index = 0;
                index < BlocksList.length;
                index++)
              Container(
                key: Key('$index'),
                child: Padding(
                  padding: const EdgeInsets.only(
                      left: 13, right: 13, bottom: 13),
                  child: Row(
                    children: [
                      Container(
                          child: SvgPicture.asset(
                              'images/menu.svg')),
                      SizedBox(
                        width: 14,
                      ),
                      Expanded(
                        child: Container(
                          height: Height * 0.07,
                          decoration: BoxDecoration(
                              borderRadius:
                                  BorderRadius.circular(5),
                              border: Border.all(
                                  color: Color(0xffE6E6E6))),
                          child: Padding(
                            padding: const EdgeInsets.only(
                                left: 13, right: 13),
                            child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment
                                        .spaceBetween,
                                children: [
                                  Row(
                                    children: [
                                      Container(
                                          child: SvgPicture.asset(
                                              'images/clip.svg')),
                                      SizedBox(
                                        width: 7,
                                      ),
                                      Column(
                                        mainAxisAlignment:
                                            MainAxisAlignment
                                                .center,
                                        crossAxisAlignment:
                                            CrossAxisAlignment
                                                .start,
                                        children: [
                                          Text(
                                            BlocksList[index]
                                                    ['Name']
                                                .toString(),
                                            style: TextStyle(
                                              fontSize: 16,
                                              fontFamily:
                                                  'SegoeUI-SemiBold',
                                              color:
                                                  textGreyColor,
                                            ),
                                          ),
                                          SizedBox(
                                            height: 4,
                                          ),
                                          Text(
                                            BlocksList[index]
                                                    ['Link']
                                                .toString(),
                                            style: TextStyle(
                                                fontSize: 12,
                                                fontFamily:
                                                    'SemiBold',
                                                color:
                                                    textGreyColor),
                                          )
                                        ],
                                      ),
                                    ],
                                  ),
                                  FlutterSwitch(
                                    width: 52,
                                    height: 32.5,
                                    valueFontSize: 12.0,
                                    toggleSize: 20.0,
                                    toggleColor: _switchValue
                                        ? Colors.white
                                        : Color(0xffE6E6E6),
                                    switchBorder: Border.all(
                                      color: Color(0xffE6E6E6),
                                      width: _switchValue
                                          ? 0
                                          : 1.0,
                                    ),
                                    activeColor: kPrimaryColor,
                                    inactiveColor: Colors.white,
                                    value: _switchValue,
                                    onToggle: (val) {
                                      setState(() {
                                        _switchValue = val;
                                      });
                                    },
                                  )
                                ]),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
          ],
          onReorder: (int oldIndex, int newIndex) {
            setState(() {
              if (oldIndex < newIndex) {
                newIndex -= 1;
              }
              final item = BlocksList.removeAt(oldIndex);
              BlocksList.insert(newIndex, item);
            });
          },
        ),
      ),
    

    【讨论】:

      【解决方案2】:

      试试ReorderableListView。应用示例如下:

      class MyApp extends StatefulWidget {
        const MyApp({Key? key}) : super(key: key);
      
        @override
        _MyAppState createState() => _MyAppState();
      }
      
      class _MyAppState extends State<MyApp> {
        final List<int> _items = List<int>.generate(5, (int index) => index);
      
        @override
        Widget build(BuildContext context) {
          return MaterialApp(
            home: Scaffold(
              body: SafeArea(
                child: ReorderableListView(
                  children: _items
                      .map(
                        (index) => MyContainer(
                          key: ValueKey(index), // Make sure you pass a unique key through here
                        ),
                      )
                      .toList(),
                  onReorder: (int oldIndex, int newIndex) {
                    setState(() {
                      if (oldIndex < newIndex) {
                        newIndex -= 1;
                      }
                      final int item = _items.removeAt(oldIndex);
                      _items.insert(newIndex, item);
                    });
                  },
                ),
              ),
            ),
          );
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-19
        • 2019-06-29
        • 2017-04-22
        • 2013-08-12
        • 1970-01-01
        • 2018-02-16
        • 1970-01-01
        相关资源
        最近更新 更多