在 widget flutter 之间使用通用函数
Use common function between widget flutter
我想要你的意见,我会尽力解释。我有一个 home.dart 页面,其中包含我在名为 fc_commun.dart 的文件中查找的应用栏小部件。在 fc_commun.dart 中,我将放置我所有页面共有的功能,例如,我有一个检索屏幕尺寸的功能 (recupdimensionecran),但是例如,我想在我的 showdialogue 中使用这个功能我的 appbar 和我的 home.dart,常见吗?
home.dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'fc_commun.dart';
class Homepage extends StatefulWidget {
const Homepage({Key? key}) : super(key: key);
@override
_HomepageState createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
Color mainColor = const Color(0xff3366FF);
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
recupdimensionecran(context);
//var test = heightmobile;
return Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Colors.transparent,
appBar: const Appbarjeu(),
body: Container(
alignment: Alignment.topCenter,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Colors.black,
mainColor,
Colors.black,
],
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: const [
Text("Test Texte"),
]),
),
);
}
}
fc_commun.dart
import 'package:flutter/material.dart';
void recupdimensionecran(BuildContext context) {
double heightmobile = MediaQuery.of(context).size.height;
double widthmobile = MediaQuery.of(context).size.width;
}
class Appbarjeu extends StatelessWidget implements PreferredSizeWidget {
const Appbarjeu({Key? key}) : super(key: key);
@override
Size get preferredSize => const Size.fromHeight(100);
@override
Widget build(BuildContext context) {
return AppBar(
title: Row(mainAxisAlignment: MainAxisAlignment.center, children: const [
Text(
'My',
style: TextStyle(
fontSize: 40,
fontWeight: FontWeight.w900,
color: Color(0xff3366FF),
),
),
SizedBox(width: 10),
Text(
'Appi',
style: TextStyle(
fontSize: 40, fontWeight: FontWeight.w900, color: Colors.white),
),
SizedBox(width: 10),
Text(
'!',
style: TextStyle(
fontSize: 40,
fontWeight: FontWeight.w900,
color: Color(0xff3366FF)),
)
]),
backgroundColor: Colors.transparent,
elevation: 0.0,
actions: <Widget>[
IconButton(
icon: const Icon(
Icons.settings,
color: Colors.white,
),
onPressed: () {
_buildPopupDialog(context);
},
)
],
);
}
void _buildPopupDialog(BuildContext context) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Container(),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
padding: const EdgeInsets.all(5.0),
child: Align(
alignment: Alignment.topLeft,
child: Column(
children: const <Widget>[
Text(
'Show dialogue :',
textAlign: TextAlign.left,
style: TextStyle(
decoration: TextDecoration.underline,
color: Colors.red,
fontWeight: FontWeight.bold,
fontSize: 14,
),
),
Text(
' Test',
textAlign: TextAlign.left,
style: TextStyle(
color: Colors.red,
fontWeight: FontWeight.bold,
fontSize: 14,
),
),
],
),
),
)
],
),
actions: <Widget>[
// ignore: unnecessary_new
ElevatedButton(
child: const Text(
"X",
style: TextStyle(fontSize: 24),
),
onPressed: () {
Navigator.of(context).pop();
},
style: ElevatedButton.styleFrom(
shape: const CircleBorder(),
),
),
],
);
});
}
}
谢谢
Widget customCircularIcon(Color bgcolor, Color iconColor, IconData icon,
{double size: 22}) {
return Padding(
padding: const EdgeInsets.only(right: 8),
child: Container(
decoration: BoxDecoration(color: bgcolor, shape: BoxShape.circle),
child: Padding(
padding: const EdgeInsets.all(6.0),
child: Center(
child: FaIcon(
icon,
size: size,
color: iconColor,
// size: 40,
// color: Colors.white,
),
),
),
),
);
}
这只是一个小部件,您可以随时放置它....或者您也可以创建一个 class 来实现同样的效果。
我想要你的意见,我会尽力解释。我有一个 home.dart 页面,其中包含我在名为 fc_commun.dart 的文件中查找的应用栏小部件。在 fc_commun.dart 中,我将放置我所有页面共有的功能,例如,我有一个检索屏幕尺寸的功能 (recupdimensionecran),但是例如,我想在我的 showdialogue 中使用这个功能我的 appbar 和我的 home.dart,常见吗?
home.dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'fc_commun.dart';
class Homepage extends StatefulWidget {
const Homepage({Key? key}) : super(key: key);
@override
_HomepageState createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
Color mainColor = const Color(0xff3366FF);
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
recupdimensionecran(context);
//var test = heightmobile;
return Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Colors.transparent,
appBar: const Appbarjeu(),
body: Container(
alignment: Alignment.topCenter,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Colors.black,
mainColor,
Colors.black,
],
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: const [
Text("Test Texte"),
]),
),
);
}
}
fc_commun.dart
import 'package:flutter/material.dart';
void recupdimensionecran(BuildContext context) {
double heightmobile = MediaQuery.of(context).size.height;
double widthmobile = MediaQuery.of(context).size.width;
}
class Appbarjeu extends StatelessWidget implements PreferredSizeWidget {
const Appbarjeu({Key? key}) : super(key: key);
@override
Size get preferredSize => const Size.fromHeight(100);
@override
Widget build(BuildContext context) {
return AppBar(
title: Row(mainAxisAlignment: MainAxisAlignment.center, children: const [
Text(
'My',
style: TextStyle(
fontSize: 40,
fontWeight: FontWeight.w900,
color: Color(0xff3366FF),
),
),
SizedBox(width: 10),
Text(
'Appi',
style: TextStyle(
fontSize: 40, fontWeight: FontWeight.w900, color: Colors.white),
),
SizedBox(width: 10),
Text(
'!',
style: TextStyle(
fontSize: 40,
fontWeight: FontWeight.w900,
color: Color(0xff3366FF)),
)
]),
backgroundColor: Colors.transparent,
elevation: 0.0,
actions: <Widget>[
IconButton(
icon: const Icon(
Icons.settings,
color: Colors.white,
),
onPressed: () {
_buildPopupDialog(context);
},
)
],
);
}
void _buildPopupDialog(BuildContext context) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Container(),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
padding: const EdgeInsets.all(5.0),
child: Align(
alignment: Alignment.topLeft,
child: Column(
children: const <Widget>[
Text(
'Show dialogue :',
textAlign: TextAlign.left,
style: TextStyle(
decoration: TextDecoration.underline,
color: Colors.red,
fontWeight: FontWeight.bold,
fontSize: 14,
),
),
Text(
' Test',
textAlign: TextAlign.left,
style: TextStyle(
color: Colors.red,
fontWeight: FontWeight.bold,
fontSize: 14,
),
),
],
),
),
)
],
),
actions: <Widget>[
// ignore: unnecessary_new
ElevatedButton(
child: const Text(
"X",
style: TextStyle(fontSize: 24),
),
onPressed: () {
Navigator.of(context).pop();
},
style: ElevatedButton.styleFrom(
shape: const CircleBorder(),
),
),
],
);
});
}
}
谢谢
Widget customCircularIcon(Color bgcolor, Color iconColor, IconData icon,
{double size: 22}) {
return Padding(
padding: const EdgeInsets.only(right: 8),
child: Container(
decoration: BoxDecoration(color: bgcolor, shape: BoxShape.circle),
child: Padding(
padding: const EdgeInsets.all(6.0),
child: Center(
child: FaIcon(
icon,
size: size,
color: iconColor,
// size: 40,
// color: Colors.white,
),
),
),
),
);
}
这只是一个小部件,您可以随时放置它....或者您也可以创建一个 class 来实现同样的效果。