Я хочу передать переключатель контроллера (который находится в моем примере сценария пользовательского меню) в другой файл, чтобы он открывал и закрывал мое скользящее меню.
Например:
Первый скрипт мой ExampleCustomMenu с переключателем контроллера, а второй (который является частью сценария) без.
import 'package:flutter/material.dart';
import 'package:mykitchen/Main Controllers/My Recipes/My Recipes.dart';
import 'hidden_drawer_menu.dart';
import 'package:mykitchen/Main Controllers/Settings+Acount/Settings.dart';
import 'package:mykitchen/Main Controllers/GroceryList/GroceryList.dart';
import 'package:mykitchen/Main Controllers/Pantry/Pantry.dart';
import 'package:mykitchen/Main Controllers/Meal Plan/MealPlan.dart';
import 'package:mykitchen/Main Controllers/Recipets/Recipets.dart';
class ExampleCustomMenu extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SimpleHiddenDrawer(
menu: Menu(),
screenSelectedBuilder: (position, controller) {
Widget screenCurrent;
switch (position) {
case 0:
screenCurrent = Settings();
break;
case 1:
screenCurrent = MyRecipes();
break;
case 2:
screenCurrent = GroceryList();
break;
case 3:
screenCurrent = Pantry();
break;
case 4:
screenCurrent = MealPlan();
break;
case 5:
screenCurrent = Recipets();
break;
}
return Scaffold(
//appBar: AppBar(
//title: Text("Look at this "),
//leading: IconButton(
// icon: Icon(Icons.menu),
// onPressed: () {
// controller.toggle();
// }),
//),
backgroundColor: Color(0xFFFF1744),
body: screenCurrent,
);
},
);
}
}
class Menu extends StatefulWidget {
@override
_MenuState createState() => _MenuState();
}
class _MenuState extends State<Menu> with TickerProviderStateMixin {
AnimationController _animationController;
bool initConfigState = false;
@override
void initState() {
_animationController =
AnimationController(vsync: this, duration: Duration(milliseconds: 100));
super.initState();
}
@override
Widget build(BuildContext context) {
confListenerState(context);
return Container(
width: double.maxFinite,
height: double.maxFinite,
color: Colors.blue,
child: Stack(
children: <Widget>[
Container(
width: double.maxFinite,
height: double.maxFinite,
),
FadeTransition(
opacity: _animationController,
child: Padding(
padding: const EdgeInsets.all(50.0),
child: Align(
alignment: Alignment.centerLeft,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(
width: 200.0,
height: 150,
child: RaisedButton(
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.all(Radius.circular(10.0))),
onPressed: () {
SimpleHiddenDrawerProvider.of(context)
.setSelectedMenuPosition(0);
},
child: Text(
"Settings",
style: TextStyle(color: Colors.black),
),
),
),
SizedBox(
width: 200.0,
child: RaisedButton(
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.all(Radius.circular(10.0))),
onPressed: () {
SimpleHiddenDrawerProvider.of(context)
.setSelectedMenuPosition(1);
},
child: Text(
"My Recipes",
style: TextStyle(color: Colors.black),
)),
),
SizedBox(
width: 200.0,
child: RaisedButton(
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.all(Radius.circular(10.0))),
onPressed: () {
SimpleHiddenDrawerProvider.of(context)
.setSelectedMenuPosition(2);
},
child: Text(
"Grocery List",
style: TextStyle(color: Colors.black),
)),
),
SizedBox(
width: 200.0,
child: RaisedButton(
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.all(Radius.circular(10.0))),
onPressed: () {
SimpleHiddenDrawerProvider.of(context)
.setSelectedMenuPosition(3);
},
child: Text(
"Pantry",
style: TextStyle(color: Colors.black),
)),
),
SizedBox(
width: 200.0,
child: RaisedButton(
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.all(Radius.circular(10.0))),
onPressed: () {
SimpleHiddenDrawerProvider.of(context)
.setSelectedMenuPosition(4);
},
child: Text(
"Meal Plan",
style: TextStyle(color: Colors.black),
)),
),
SizedBox(
width: 200.0,
child: RaisedButton(
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.all(Radius.circular(10.0))),
onPressed: () {
SimpleHiddenDrawerProvider.of(context)
.setSelectedMenuPosition(5);
},
child: Text(
"Recipets",
style: TextStyle(color: Colors.black),
)),
),
],
),
),
),
),
],
),
);
}
void confListenerState(BuildContext context) {
if (!initConfigState) {
initConfigState = true;
SimpleHiddenDrawerProvider.of(context)
.getMenuStateListener()
.listen((state) {
if (state == MenuState.open) {
_animationController.forward();
}
if (state == MenuState.closing) {
_animationController.reverse();
}
});
}
}
}
Второй сценарий:
import 'package:mykitchen/Backend/SlideOutMenu/ExampleCustomMenu.dart';
import 'Account.dart';
import 'package:mykitchen/Backend/AnimtedButton/AnimatedButton.dart';
import 'package:mykitchen/Sliverappbartry.dart';
const PrimaryColor = const Color(0xFFFFFFFF);
class Settings extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(title: 'Hello there'),
debugShowCheckedModeBanner: false);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(slivers: <Widget>[
SliverAppBar(
title: Row(
children: <Widget>[
Column(
children: <Widget>[
Container(
margin: EdgeInsets.fromLTRB(0, 0, 0, 0),
child: IconButton(
onPressed: () =>
Navigator.pop(context),
//controller.toggle();
icon: Image.asset('assets/UI/menu.png')),
),
],
)
],
),
expandedHeight: 145.0,
backgroundColor: Colors.white,
//floating: true,
pinned: true,
elevation: 0.0,
flexibleSpace: FlexibleSpaceBar(
title: Row(
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(80, 0, 0, 0),
child: Text(
'Settings',
textAlign: TextAlign.left,
style: TextStyle(
color: Colors.black,
fontFamily: 'AvenirNext',
fontWeight: FontWeight.w600,
fontSize: 22.0),
)),
],
),
background: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(20, 40, 32, 0),
child: Text(
'Change Settings',
textAlign: TextAlign.left,
style: TextStyle(
color: Colors.black54,
fontFamily: 'AvenirNext',
fontWeight: FontWeight.w300,
fontSize: 16.0),
)),
],
),
),
),
Так что вместо Navigator.pop (context) это будет controller.toggle для второго сценария, как мне сделать эту работу?
Спасибо за вашу помощь!