如何解决如何在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 举报,一经查实,本站将立刻删除。