我正在尝试通过简单的按钮按下来在另一个类中执行某些操作。关键是要为多个不同的功能使用一页,而不是为每个功能使用一页。
现在,导航到textDeclaration页的工作正常,但是我无法获得正确的功能来完成其工作。
如果按“第一个按钮”,则FirstButton小部件的信息应显示在textDeclaration页上;如果按“第二个按钮”,则SecondButton小部件的信息应弹出在displayText-page上。
主要:
import 'package:flutter/material.dart';
import 'homePage.dart';
void main()=>runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
首页:
import 'package:flutter/material.dart';
import 'textDeclaration.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Function buttonChoice;
Widget outlineButton(String buttonName) {
return SizedBox(
height: 60.0,
width: 225.0,
child: OutlineButton(
highlightedBorderColor: Colors.lightBlueAccent,
child: Text(buttonName,
style: TextStyle(fontSize: 17.0, fontWeight:
FontWeight.bold)),
borderSide: BorderSide(
color: Colors.blue,
width: 2.0,
),
onPressed: () {
navigate(buttonName, buttonChoice);
}),
);
}
void navigate(String buttonName, Function buttonChoice) {
setState(() {
Navigator.of(context).popUntil(ModalRoute.withName('/'));
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => TextDeclaration(
buttonName: buttonName,
buttonChoice: buttonChoice,
)));
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('HOME PAGE')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
outlineButton('First Button'),
outlineButton('Second Button'),
],
),
),
);
}
}
textDeclarationPage:
import 'package:flutter/material.dart';
class TextDeclaration extends StatefulWidget {
final String buttonName;
final Function buttonChoice;
TextDeclaration({this.buttonName, this.buttonChoice});
@override
_TextDeclarationState createState() => _TextDeclarationState();
}
class _TextDeclarationState extends State<TextDeclaration> {
String buttonName = "";
buttonChoice(String buttonName) {
if (buttonName == 'First Button') {
firstButton(buttonName);
} else if (buttonName == 'Second Button') {
secondButton(buttonName);
}
}
Widget firstButton(buttonName) {
return Scaffold(
appBar: AppBar(
title: Text('First Button'),
),
body: Center(
child: Container(
height: 200.0,
width: 200.0,
child: Center(child: Text('First Button Text')),
),
),
);
}
Widget secondButton(buttonName) {
return Scaffold(
appBar: AppBar(
title: Text('Second Button'),
),
body: Center(
child: SizedBox(
height: 200.0,
width: 200.0,
child: Center(
child: Text('Second Button Text'),
),
),
),
);
}
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(color: Colors.blue),
child: buttonChoice(buttonName),
);
}
}
代码可以运行,但是不同按钮上的功能不起作用。
自从我坚持很长时间以来,任何帮助都将非常有用。谢谢。