【 Flutter 】Flutter を 基礎 から 学習 ( ウィジェット編 ) part100 Interaction model widgets

基礎 から 学ぶ Flutter 」という書籍で  学習 したことを ブログでアウトプットしていこうと思います。今回は ウィジェット編 ( part100 )です。

前回

【 Flutter 】Flutter を 基礎 から 学習 ( ウィジェット編 ) part99 Interaction model widgets

引き続き、Interaction model widgetsについて学びます。

Interaction model widgets

ReorderableListViewウィジェット

ReorderableListViewウィジェットはユーザーがリストを任意の順番で並び変えることができるウィジェットです。
リストアイテムを長押しして好きな位置にアイテムを移動できます。

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

void main() {
  // runApp(new MaterialApp(home: new MyApp()));
  runApp(new MaterialApp(home: new SamplePage()));
}

class SamplePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _SamplePageState();
  }
}

class _SamplePageState extends State<SamplePage> {
  List<String> _items = ['いち', 'に', 'さん'];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('テスト中!')),
        body: ReorderableListView(
          // 入れ替えるロジックです。
          onReorder: (oldIndex, newIndex) {
            setState(() {
              if (newIndex > _items.length) {
                newIndex = _items.length;
              }
              if (oldIndex < newIndex) {
                newIndex--;
              }
              var item = _items[oldIndex];
              _items.remove(item);
              _items.insert(newIndex, item);
            });
          },
          // リストのタイトルです。
          header: const Text('ReorderableListViewのサンプル'),
          // アイテム定義です。
          children: <Widget>[
            for (var item in _items)
              ListTile(
                key: ValueKey(item),
                title: Text(item.toString()),
              )
          ],
        ),
      ),
    );
  }
}

アイテムを長押ししてアイテムを任意の位置に移動できます。

最後に

iPhoneのメールボックスリストで見たことあります😀

今日はここまで!