使用小部件颤动之间的通用功能



我想听听你的意见,我会尽力解释的。我有一个home.dart页面,其中包含一个appbar小部件,我正在一个名为fc_commun.dart的文件中查找该小部件。在fc_commun.dart中,我将放置所有页面通用的函数,例如,我有一种检索屏幕尺寸的函数(recipdimensionecran(,但例如,我想在我的appbar的showdialogue中以及在我的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,
),
),
),
),
);
}

这只是一个小部件,无论何时,你都可以放置它……或者你也可以创建一个类来实现同样的目的。

最新更新