微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在flutter(dart)

如何解决如何在flutter(dart)

我是 dart 的新手,我花了好几个小时试图列出每个索引都调用其他函数的列表。我没有找到解决我的问题的方法。我尝试了很多东西,但没有一个我有用 我想从 search.dart 插入文本“索引 2:搜索”中调用“DataSearch”类。有人知道我该怎么做吗?。

import 'package:Flutter/material.dart';
import 'search.dart';

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

/// This is the main application widget.
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,title: _title,home: MyStatefulWidget(),);
  }
}

/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key? key}) : super(key: key);

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  int _selectedindex = 0;
  static const TextStyle optionStyle =
      TextStyle(fontSize: 30,fontWeight: FontWeight.bold);
  static const List<Widget> _widgetoptions = <Widget>[
    Text(
      'Index 0: Home',style: optionStyle,),Text(
      'Index 1: Profile',Text(
      'Index 2: Search',];

  void _onItemTapped(int index) {
    setState(() {
      _selectedindex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Temp'),centerTitle: true,actions: <Widget>[
          IconButton(
              icon: Icon(Icons.search),onpressed: () {
                showSearch(context: context,delegate: DataSearch());
              }),],body: Center(
        child: _widgetoptions.elementAt(_selectedindex),bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),label: 'home',BottomNavigationBarItem(
            icon: Icon(Icons.person),label: 'Profile',BottomNavigationBarItem(
            icon: Icon(Icons.search),label: 'Search',currentIndex: _selectedindex,selectedItemColor: Colors.lightBlue[800],onTap: _onItemTapped,);
  }
}

解决方法

void func1() { 
  print("function 1 called"); 
}

void func2() { 
  print("function 2 called"); 
}

void func3() { 
  print("function 3 called"); 
}

void main() {
  final List<Function> _widgetOptions = [
    func1,func2,func3,];
  
  // this prints "function 2 called",since it's the second in the list
  _widgetOptions[1]();
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?