如何在fluttter中使用DropdownButton创建树选择框

问题描述 投票:-1回答:1

我想在Flutter中使用DropDownButton来选择框。但我无法为此制定完美的算法。我希望能从Flutter专家那里得到很大的帮助。谢谢。

Screenshot of current implementation

algorithm flutter drop-down-menu treeview dropdown
1个回答
0
投票

您可以使用如下所示的下拉代码:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: Center(
          child: MyStatefulWidget(),
        ),
      ),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  MyStatefulWidget({Key key}) : super(key: key);

  @override
  _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  String dropdownValue = 'Select One';

  @override
  Widget build(BuildContext context) {
    return DropdownButton<String>(
      value: dropdownValue,
      icon: Icon(Icons.arrow_downward),
      iconSize: 24,
      elevation: 16,
      style: TextStyle(color: Colors.deepPurple),
      underline: Container(
        height: 2,
        color: Colors.deepPurpleAccent,
      ),
      onChanged: (String newValue) {
        setState(() {
          dropdownValue = newValue;
        });
      },
      items: <String>['Select One', 'Select Two', 'Select Three', 'Select Four']
          .map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Text(value),
        );
      }).toList(),
    );
  }
}

文档在这里:https://api.flutter.dev/flutter/material/DropdownButton-class.html

我在这里做了一个dartpad示例:https://dartpad.dev/de4630aaa4d3a071e625da59c45f10cf

© www.soinside.com 2019 - 2024. All rights reserved.