Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振-如何在下拉菜单和其他颤振功能中包含其他值?_Flutter_Dart_Drop Down Menu_Arguments_Instance - Fatal编程技术网

Flutter 颤振-如何在下拉菜单和其他颤振功能中包含其他值?

Flutter 颤振-如何在下拉菜单和其他颤振功能中包含其他值?,flutter,dart,drop-down-menu,arguments,instance,Flutter,Dart,Drop Down Menu,Arguments,Instance,嗨,我一直在想这个问题,但仍然不知道如何在下拉列表中传递值 提供商类别: import 'package:flutter/foundation.dart'; class Category with ChangeNotifier { final String id; final String title; Category({ @required this.id, @required this.title, }); } 提供商类别: import 'package

嗨,我一直在想这个问题,但仍然不知道如何在下拉列表中传递值

提供商类别:

import 'package:flutter/foundation.dart';
class Category with ChangeNotifier {
  final String id;
  final String title;
  Category({
    @required this.id,
    @required this.title,
  });
}
提供商类别:

import 'package:flutter/material.dart';
import './category.dart';
class Categories with ChangeNotifier {
  List < Category > _items = [
    Category(
      id: 'id1',
      title: 'Default',
    ),
    Category(
      id: 'id2',
      title: 'French Lesson',
    ),
    Category(
      id: 'id3',
      title: 'Spanish Lesson',
    ),
  ];
}
导入“包装:颤振/材料.省道”;
导入“/category.dart”;
使用ChangeNotifier类类别{
列表<类别>\项目=[
类别(
id:'id1',
标题:“默认值”,
),
类别(
id:'id2',
标题:“法语课”,
),
类别(
id:'id3',
标题:“西班牙语课”,
),
];
}
屏幕:

1.如果没有其他类别,我需要初始选择默认选项,然后选择列表中的最后一个

2.如果更改下拉选择选项并按下图标列表,则应传递所选值(id和标题)

导入“包装:颤振/材料.省道”;
导入“包:provider/provider.dart”;
导入“../providers/categories.dart”;
...
void getDropDownItem(BuildContext ctx){
设置状态(){
Navigator.of(ctx.pushName)(
CategoryNotesScreen.routeName,
参数:{//在这部分中,我不确定如何在按下图标的下拉列表(ID和标题)中获取所选选项的值?
“id”:catId,
“标题”:卡特尔
}
);
});
}
@凌驾
小部件构建(构建上下文){
...
final categoriesData=Provider.of(上下文);
最终分类数据项=分类数据项;
...
appBar:appBar(
标题:常量文本(“新注释”),
操作:[
图标按钮(
图标:常量图标(
图标列表),
按下:()=>getDropDownItem(上下文),
),
],
),
...
子项:DropdownButtonFormField(
items:categoriesDataItems.map((categoriesDataItemsMap)=>DropdownMenuItem(
子项:文本(categoriesDataItemsMap.title),
值:categoriesDataItemsMap.id,
),).toList(),
值:selectedCatId,
一旦更改:(newValue){
设置状态(){
打印(新值);
selectedCatId=newValue;
});
},
),
}
新屏幕:我需要获得所选类别参数以显示类别标题,并能够使用类别ID从列表中筛选

import 'package:flutter/material.dart';
class CategoryNotesScreen extends StatelessWidget {
  static
  const routeName = '/category-notes';
  @override
  Widget build(BuildContext context) {
    final routeArgs =
      ModalRoute.of(context).settings.arguments as Map < String, String > ;
    print('cat_notes_screen');
    print(routeArgs);
    final catId = routeArgs['id'];
    final catTitle = routeArgs['title'];
    return Scaffold(
      appBar: AppBar(
        title: Text('catTitle'),
      ),
      body: Center(
        child: Text(
          'The Recipes For The Category!',
        ),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
类CategoryNotesScreen扩展了无状态小部件{
静止的
常量routeName='/category notes';
@凌驾
小部件构建(构建上下文){
最终路线=
ModalRoute.of(context).settings.arguments作为映射;
打印(“cat_注释_屏幕”);
印刷品(常规);
最终catId=routeArgs['id'];
最终证书=routeArgs['title'];
返回脚手架(
appBar:appBar(
标题:文本(“卡特尔”),
),
正文:中(
子:文本(
“该类别的配方!”,
),
),
);
}
}

抱歉,我对flutter还很陌生,还在学习…

也许可以使用FutureBuilder和ListView

您可以复制粘贴运行下面的完整代码
您可以看到下面的工作演示
步骤1:
CategoryNotesScreen
get
arguments

Category routeArgs = ModalRoute.of(context).settings.arguments;
第二步:
getDropDownItem
pass
selectedCategory

Category selectedCategory;

void getDropDownItem(BuildContext ctx) {
    setState(() {
      Navigator.of(ctx).pushNamed(CategoryNotesScreen.routeName,
          arguments: selectedCategory);
    });
  }
第3步:设置默认的
selectedCategory
,您可以使用
categoriesDataItems.first
last

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      final categoriesData = Provider.of<Categories>(context, listen: false);
      final categoriesDataItems = categoriesData.items;
      setState(() {
        selectedCategory = categoriesDataItems.first;
      });
    });
    super.initState();
  }
@覆盖
void initState(){
WidgetsBinding.instance.addPostFrameCallback((){
final categoriesData=Provider.of(上下文,listen:false);
最终分类数据项=分类数据项;
设置状态(){
selectedCategory=categoriesDataItems.first;
});
});
super.initState();
}
步骤4:使用
dropdownbutonformfield

DropdownButtonFormField(
项目:分类数据项目
.地图(
(categoriesDataItemsMap)=>下拉菜单项(
子项:文本(categoriesDataItemsMap.title),
值:categoriesDataItemsMap,
),
)
.toList(),
值:selectedCategory,
一旦更改:(newValue){
设置状态(){
打印(新值);
selectedCategory=newValue;
});
},
),
工作演示

完整代码

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

class Category {
  final String id;
  final String title;
  Category({
    @required this.id,
    @required this.title,
  });
}

class Categories with ChangeNotifier {
  List<Category> items = [
    Category(
      id: 'id1',
      title: 'Default',
    ),
    Category(
      id: 'id2',
      title: 'French Lesson',
    ),
    Category(
      id: 'id3',
      title: 'Spanish Lesson',
    ),
  ];
}

void main() {
  runApp(
    ChangeNotifierProvider(
      create: (context) => Categories(),
      child: MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      initialRoute: '/',
      routes: {
        '/': (context) => MyHomePage(
              title: "test",
            ),
        '/category-notes': (context) => CategoryNotesScreen(),
      },
    );
  }
}

class CategoryNotesScreen extends StatelessWidget {
  static const routeName = '/category-notes';

  @override
  Widget build(BuildContext context) {
    Category routeArgs = ModalRoute.of(context).settings.arguments;
    print('cat_notes_screen');
    print(routeArgs);
    final catId = routeArgs.id;
    final catTitle = routeArgs.title;
    return Scaffold(
      appBar: AppBar(
        title: Text('catTitle $catTitle'),
      ),
      body: Center(
        child: Text(
          '$catId $catTitle The Recipes For The Category!',
        ),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  Category selectedCategory;

  void getDropDownItem(BuildContext ctx) {
    setState(() {
      Navigator.of(ctx).pushNamed(CategoryNotesScreen.routeName,
          arguments: selectedCategory);
    });
  }

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      final categoriesData = Provider.of<Categories>(context, listen: false);
      final categoriesDataItems = categoriesData.items;
      setState(() {
        selectedCategory = categoriesDataItems.first;
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final categoriesData = Provider.of<Categories>(context);
    final categoriesDataItems = categoriesData.items;

    return Scaffold(
      appBar: AppBar(
        title: const Text("New Note"),
        actions: <Widget>[
          IconButton(
            icon: const Icon(Icons.list),
            onPressed: () => getDropDownItem(context),
          ),
        ],
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            DropdownButtonFormField<Category>(
              items: categoriesDataItems
                  .map(
                    (categoriesDataItemsMap) => DropdownMenuItem(
                      child: Text(categoriesDataItemsMap.title),
                      value: categoriesDataItemsMap,
                    ),
                  )
                  .toList(),
              value: selectedCategory,
              onChanged: (newValue) {
                setState(() {
                  print(newValue);
                  selectedCategory = newValue;
                });
              },
            ),
          ],
        ),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
导入“包:provider/provider.dart”;
类别{
最终字符串id;
最后的字符串标题;
类别({
@需要这个.id,
@需要这个标题,
});
}
使用ChangeNotifier类类别{
列表项=[
类别(
id:'id1',
标题:“默认值”,
),
类别(
id:'id2',
标题:“法语课”,
),
类别(
id:'id3',
标题:“西班牙语课”,
),
];
}
void main(){
runApp(
变更通知提供者(
创建:(上下文)=>Categories(),
子项:MyApp(),
),
);
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
initialRoute:“/”,
路线:{
“/”:(上下文)=>MyHomePage(
标题:“测试”,
),
“/category notes”:(上下文)=>CategoryNotesScreen(),
},
);
}
}
类CategoryNotesScreen扩展了无状态小部件{
静态常量routeName='/category notes';
@凌驾
小部件构建(构建上下文){
类别routeArgs=ModalRoute.of(context.settings.arguments);
打印(“cat_注释_屏幕”);
印刷品(常规);
最终catId=routeArgs.id;
最终catTitle=routeArgs.title;
返回脚手架(
appBar:appBar(
标题:文本('catTitle$catTitle'),
),
正文:中(
子:文本(
“$catId$catTitle该类别的配方!”,
),
),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最终的
DropdownButtonFormField<Category>(
      items: categoriesDataItems
          .map(
            (categoriesDataItemsMap) => DropdownMenuItem(
              child: Text(categoriesDataItemsMap.title),
              value: categoriesDataItemsMap,
            ),
          )
          .toList(),
      value: selectedCategory,
      onChanged: (newValue) {
        setState(() {
          print(newValue);
          selectedCategory = newValue;
        });
      },
    ),
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class Category {
  final String id;
  final String title;
  Category({
    @required this.id,
    @required this.title,
  });
}

class Categories with ChangeNotifier {
  List<Category> items = [
    Category(
      id: 'id1',
      title: 'Default',
    ),
    Category(
      id: 'id2',
      title: 'French Lesson',
    ),
    Category(
      id: 'id3',
      title: 'Spanish Lesson',
    ),
  ];
}

void main() {
  runApp(
    ChangeNotifierProvider(
      create: (context) => Categories(),
      child: MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      initialRoute: '/',
      routes: {
        '/': (context) => MyHomePage(
              title: "test",
            ),
        '/category-notes': (context) => CategoryNotesScreen(),
      },
    );
  }
}

class CategoryNotesScreen extends StatelessWidget {
  static const routeName = '/category-notes';

  @override
  Widget build(BuildContext context) {
    Category routeArgs = ModalRoute.of(context).settings.arguments;
    print('cat_notes_screen');
    print(routeArgs);
    final catId = routeArgs.id;
    final catTitle = routeArgs.title;
    return Scaffold(
      appBar: AppBar(
        title: Text('catTitle $catTitle'),
      ),
      body: Center(
        child: Text(
          '$catId $catTitle The Recipes For The Category!',
        ),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  Category selectedCategory;

  void getDropDownItem(BuildContext ctx) {
    setState(() {
      Navigator.of(ctx).pushNamed(CategoryNotesScreen.routeName,
          arguments: selectedCategory);
    });
  }

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      final categoriesData = Provider.of<Categories>(context, listen: false);
      final categoriesDataItems = categoriesData.items;
      setState(() {
        selectedCategory = categoriesDataItems.first;
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final categoriesData = Provider.of<Categories>(context);
    final categoriesDataItems = categoriesData.items;

    return Scaffold(
      appBar: AppBar(
        title: const Text("New Note"),
        actions: <Widget>[
          IconButton(
            icon: const Icon(Icons.list),
            onPressed: () => getDropDownItem(context),
          ),
        ],
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            DropdownButtonFormField<Category>(
              items: categoriesDataItems
                  .map(
                    (categoriesDataItemsMap) => DropdownMenuItem(
                      child: Text(categoriesDataItemsMap.title),
                      value: categoriesDataItemsMap,
                    ),
                  )
                  .toList(),
              value: selectedCategory,
              onChanged: (newValue) {
                setState(() {
                  print(newValue);
                  selectedCategory = newValue;
                });
              },
            ),
          ],
        ),
      ),
    );
  }
}