如何在flutter中导入和使用另一个dart文件中的函数
Posted
技术标签:
【中文标题】如何在flutter中导入和使用另一个dart文件中的函数【英文标题】:How to import and use a function from another dart file in flutter 【发布时间】:2022-01-05 16:56:01 【问题描述】:我正在制作一个应用程序,其中我使用包蛇底部导航和高级抽屉。两者都在文件中配置得很好,但是当我想在新页面上使用该功能打开和关闭抽屉时遇到问题。我无法将它导入到我正在制作应用程序主页的主页文件中。这段代码
void _handleMenuButtonPressed()
_advancedDrawerController.showDrawer();
是抽屉屏幕 onpressed 和 valuelistener 中的一个创建问题。 这是我的仪表板代码,其中包含底部导航和抽屉:
class Dashboard extends StatefulWidget
const Dashboard(Key? key) : super(key: key);
@override
State<Dashboard> createState() => _DashboardState();
class _DashboardState extends State<Dashboard>
final _advancedDrawerController = AdvancedDrawerController();
int _selectedIndex = 0;
final PageController _pageController = PageController();
ShapeBorder? bottomBarShape = const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(25)),
);
SnakeBarBehaviour snakeBarStyle = SnakeBarBehaviour.floating;
EdgeInsets padding = const EdgeInsets.all(12);
SnakeShape snakeShape = SnakeShape.circle;
bool showSelectedLabels = true;
bool showUnselectedLabels = true;
Color selectedColor = Colors.black;
Gradient selectedGradient =
const LinearGradient(colors: [Colors.red, Colors.amber]);
Color unselectedColor = Colors.black;
Gradient unselectedGradient =
const LinearGradient(colors: [Colors.black, Colors.black]);
Color? containerColor;
List<Color> containerColors = [
const Color(0xFFFDE1D7),
const Color(0xFFE4EDF5),
const Color(0xFFE7EEED),
const Color(0xFFF4E4CE),
];
@override
Widget build(BuildContext context)
return AdvancedDrawer(
backdropColor: kblue,
controller: _advancedDrawerController,
animationCurve: Curves.easeInOut,
animationDuration: const Duration(milliseconds: 300),
animateChildDecoration: true,
rtlOpening: false,
disabledGestures: false,
childDecoration: const BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(16)),
),
drawer: SafeArea(
child: Container(
color: kblue,
child: ListTileTheme(
textColor: Colors.white,
iconColor: Colors.white,
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
Container(
width: 128.0,
height: 128.0,
margin: const EdgeInsets.only(
top: 24.0,
bottom: 64.0,
),
clipBehavior: Clip.antiAlias,
decoration: const BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
),
child: Image.asset(
'assets/logo.png',
scale: 3,
),
),
ListTile(
onTap: () ,
leading: const Icon(Icons.home),
title: const Text(
'Home',
style: TextStyle(),
),
),
ListTile(
onTap: () ,
leading: const Icon(Icons.account_circle_rounded),
title: const Text(
'Profile',
style: TextStyle(),
),
),
ListTile(
onTap: () ,
leading: const Icon(Icons.notifications),
title: const Text(
'Notifications',
style: TextStyle(),
),
),
ListTile(
onTap: () ,
leading: const Icon(Icons.bug_report),
title: const Text(
'Report a bug',
style: TextStyle(),
),
),
ListTile(
onTap: () ,
leading: const Icon(Icons.logout),
title: const Text(
'Logout',
style: TextStyle(),
),
),
const Spacer(),
DefaultTextStyle(
style: const TextStyle(
fontSize: 12,
),
child: GestureDetector(
onTap: () ,
child: Container(
margin: const EdgeInsets.symmetric(
vertical: 16.0,
),
child: const Text(
'Terms of Service | Privacy Policy',
style: TextStyle(),
),
),
),
),
],
),
),
),
),
child: Scaffold(
extendBodyBehindAppBar: true,
resizeToAvoidBottomInset: true,
extendBody: true,
bottomNavigationBar: SnakeNavigationBar.color(
behaviour: snakeBarStyle,
snakeShape: snakeShape,
shape: bottomBarShape,
padding: padding,
snakeViewColor: selectedColor,
selectedItemColor: snakeShape == SnakeShape.indicator
? selectedColor
: Colors.orange,
unselectedItemColor: Colors.black,
showUnselectedLabels: showUnselectedLabels,
showSelectedLabels: showSelectedLabels,
currentIndex: _selectedIndex,
onTap: (index) => setState(() => _selectedIndex = index),
items: const [
BottomNavigationBarItem(icon: Icon(Icons.person), label: 'tickets'),
BottomNavigationBarItem(
icon: Icon(Icons.person), label: 'calendar'),
BottomNavigationBarItem(icon: Icon(Icons.person), label: 'home'),
BottomNavigationBarItem(
icon: Icon(Icons.person), label: 'microphone'),
BottomNavigationBarItem(icon: Icon(Icons.person), label: 'search'),
],
selectedLabelStyle: const TextStyle(fontSize: 14),
unselectedLabelStyle: const TextStyle(fontSize: 10),
),
body: PageView(
controller: _pageController,
children: const <Widget>[
HomePage(),
HomePage(),
HomePage(),
HomePage(),
HomePage(),
],
onPageChanged: (page)
setState(()
_selectedIndex = page;
);
,
),
),
);
void _handleMenuButtonPressed()
_advancedDrawerController.showDrawer();
主页的代码,其中包含打开和关闭抽屉的按钮,这就是问题所在:
class HomePage extends StatefulWidget
const HomePage(Key? key) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
class _HomePageState extends State<HomePage>
@override
Widget build(BuildContext context)
return Scaffold(
body: SingleChildScrollView(
child: SafeArea(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(12.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
IconButton(
onPressed: _handleMenuButtonPressed,
icon: ValueListenableBuilder<AdvancedDrawerValue>(
valueListenable: _advancedDrawerController,
builder: (_, value, __)
return AnimatedSwitcher(
duration: const Duration(milliseconds: 250),
child: Icon(
value.visible ? Icons.clear : Icons.menu,
key: ValueKey<bool>(value.visible),
),
);
,
),
),
],
),
),
],
),
),
),
);
【问题讨论】:
能否提供您上面提到的项目文件结构或文件的位置和名称,以便我们提供示例。 我已经提到了文件名,它们不需要任何以前的文件,除了颜色一可以被任何人忽略,标记颜色太基本了,所以我不知道这两个文件还需要什么充足的。从仪表板类文件代码连接主页问题来自仪表板类文件 【参考方案1】:你可以这样做。一个文件就不需要分开了
class Dashboard extends StatefulWidget
const Dashboard(Key? key) : super(key: key);
@override
State<Dashboard> createState() => _DashboardState();
class _DashboardState extends State<Dashboard>
final _advancedDrawerController = AdvancedDrawerController();
int _selectedIndex = 0;
final PageController _pageController = PageController();
ShapeBorder? bottomBarShape = const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10)),
);
SnakeBarBehaviour snakeBarStyle = SnakeBarBehaviour.floating;
EdgeInsets padding = const EdgeInsets.only(top: 0);
SnakeShape snakeShape = SnakeShape.circle;
final double elevation = 4;
bool showSelectedLabels = true;
bool showUnselectedLabels = true;
Color selectedColor = Colors.white;
Gradient selectedGradient =
const LinearGradient(colors: [Colors.blue, Colors.pink]);
Color unselectedColor = Colors.black;
Gradient unselectedGradient =
const LinearGradient(colors: [Colors.black, Colors.black]);
Color? containerColor;
List<Color> containerColors = [
const Color(0xFFFDE1D7),
const Color(0xFFE4EDF5),
const Color(0xFFE7EEED),
const Color(0xFFF4E4CE),
];
@override
Widget build(BuildContext context)
return AdvancedDrawer(
backdropColor: kblue,
controller: _advancedDrawerController,
animationCurve: Curves.easeInOut,
animationDuration: const Duration(milliseconds: 300),
animateChildDecoration: true,
rtlOpening: false,
disabledGestures: false,
childDecoration: const BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(16)),
),
drawer: SafeArea(
child: Container(
color: kblue,
child: ListTileTheme(
textColor: Colors.white,
iconColor: Colors.white,
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
Container(
width: 128.0,
height: 128.0,
margin: const EdgeInsets.only(
top: 24.0,
bottom: 64.0,
),
clipBehavior: Clip.antiAlias,
decoration: const BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
),
child: Image.asset(
'assets/logo.png',
scale: 3,
),
),
ListTile(
onTap: ()
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => const Dashboard()));
,
leading: const Icon(Icons.home),
title: const Text(
'Home',
style: TextStyle(),
),
),
ListTile(
onTap: () ,
leading: const Icon(Icons.account_circle_rounded),
title: const Text(
'Profile',
style: TextStyle(),
),
),
ListTile(
onTap: ()
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const NotificationsScreen()));
,
leading: const Icon(Icons.notifications),
title: const Text(
'Notifications',
style: TextStyle(),
),
),
ListTile(
onTap: ()
Navigator.push(context,
MaterialPageRoute(builder: (context) => ReportBug()));
,
leading: const Icon(Icons.bug_report),
title: const Text(
'Report a bug',
style: TextStyle(),
),
),
ListTile(
onTap: ()
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => const LoginPage()));
,
leading: const Icon(Icons.logout),
title: const Text(
'Logout',
style: TextStyle(),
),
),
const Spacer(),
DefaultTextStyle(
style: const TextStyle(
fontSize: 12,
),
child: GestureDetector(
onTap: ()
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => const TermsandPolicies()));
,
child: Container(
margin: const EdgeInsets.symmetric(
vertical: 16.0,
),
child: const Text(
'Terms of Service | Privacy Policy',
style: TextStyle(),
),
),
),
),
],
),
),
),
),
child: Scaffold(
appBar: AppBar(
backgroundColor: const Color(0xFFFAFAFA),
elevation: 0,
centerTitle: true,
title: const Text(
'Dhrop',
style: TextStyle(
letterSpacing: 1,
color: Colors.black,
),
),
automaticallyImplyLeading: false,
leading: IconButton(
onPressed: _handleMenuButtonPressed,
icon: ValueListenableBuilder<AdvancedDrawerValue>(
valueListenable: _advancedDrawerController,
builder: (_, value, __)
return AnimatedSwitcher(
duration: const Duration(milliseconds: 250),
child: Icon(
value.visible ? Icons.clear : Icons.menu,
color: Colors.black,
key: ValueKey<bool>(value.visible),
),
);
,
),
),
),
extendBodyBehindAppBar: true,
resizeToAvoidBottomInset: true,
extendBody: true,
bottomNavigationBar: SnakeNavigationBar.color(
backgroundColor: kblue,
behaviour: snakeBarStyle,
snakeShape: snakeShape,
shape: bottomBarShape,
padding: const EdgeInsets.only(
bottom: 15,
left: 10,
right: 10,
),
snakeViewColor: selectedColor,
selectedItemColor:
snakeShape == SnakeShape.indicator ? selectedColor : kblue,
unselectedItemColor: Colors.black,
showUnselectedLabels: showUnselectedLabels,
showSelectedLabels: showSelectedLabels,
currentIndex: _selectedIndex,
onTap: (index) => setState(() => _selectedIndex = index),
items: [
BottomNavigationBarItem(
icon: IconButton(
icon: const Icon(
Icons.home,
),
onPressed: ()
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const HomePage()));
,
),
// label: 'Home',
),
BottomNavigationBarItem(
icon: IconButton(
icon: const Icon(
Icons.search,
),
onPressed: ()
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const SearchPage()));
,
),
// label: 'Search',
),
BottomNavigationBarItem(
icon: IconButton(
icon: const Icon(
Icons.message,
),
onPressed: ()
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const MessagesPage()));
,
),
// label: 'Messages',
),
BottomNavigationBarItem(
icon: IconButton(
icon: const Icon(
Icons.luggage,
),
onPressed: ()
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const DropPage()));
,
),
// label: 'Drop',
),
],
selectedLabelStyle: const TextStyle(fontSize: 12),
unselectedLabelStyle: const TextStyle(fontSize: 14),
),
body: PageView(
controller: _pageController,
children: const <Widget>[
HomePage(),
SearchPage(),
MessagesPage(),
DropPage()
],
onPageChanged: (page)
setState(()
_selectedIndex = page;
);
,
),
),
);
void _handleMenuButtonPressed()
_advancedDrawerController.showDrawer();
【讨论】:
谢谢它成功了以上是关于如何在flutter中导入和使用另一个dart文件中的函数的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Dart / Flutter 中使用另一个文件的函数?
如何使用 JAVA 在 Web App 中导入和显示 3D 文件 (.stl)?