I want your opinion, I will try to explain. I have a home.dart page that includes an appbar widget that I'm looking for in a file called fc_commun.dart. In fc_commun.dart I'm going to put the functions common to all my pages, and I have for example a function that retrieves the dimensions of the screen (recupdimensionecran) but for example, I want to use this function in my showdialogue of my appbar and also in my home.dart, common do?
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(),
),
),
],
);
});
}
}
Thank you
CodePudding user response:
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,
),
),
),
),
);
}
This is just a widget, whenever you want you can place it....or you can create a class as well to achieve the same.
