我已经尝试了几个小时来解决这个问题,但没有成功,所以我认为通过询问 Stackoverflow 来节省一些头发是值得的。
问题: 我无法让 TextButton 占据其父 AlertDialog 小部件的全部宽度。当我尝试将 TextButton 包装在宽度为 double.infinity 的 SizedBox 中时,当我按下按钮时它会崩溃(这应该是一种让按钮根据网络获得全宽度的方法)。
预期效果: 让TextButton取父AlertDialog的宽度。
这是解决该问题的最小可行代码(请注意,我使用包 google fonts,我希望我已经摆脱了它们,但是这里已经很晚了。):
import 'package:flutter/material.dart';
// import 'package:google_fonts/google_fonts.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
inputDecorationTheme: InputDecorationTheme(
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Color(0xffB2413A),
),
),
),
),
home: LoginScreen(),
);
}
}
class LoginScreen extends StatelessWidget {
const LoginScreen({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
return Scaffold(
body: Container(
height: size.height,
child: Center(child: LoginWidget()),
decoration: BoxDecoration(
color: Colors.black54,
image: DecorationImage(
image: AssetImage('assets/background_image.jpeg'),
fit: BoxFit.cover,
colorFilter: ColorFilter.mode(
Colors.black.withOpacity(0.08), BlendMode.dstATop),
),
),
),
);
}
}
class LoginWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final _size = MediaQuery.of(context).size;
final double _height = (_size.height) * 0.40;
final double _width = _height * 0.8;
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: Center(
child: Text(
'Login',
style: TextStyle(
color: Colors.white,
fontSize: 26,
fontWeight: FontWeight.bold),
),
),
height: _size.height * 0.085,
width: _width,
decoration: BoxDecoration(
color: Color(0xffB2413A),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(15), topRight: Radius.circular(15)),
),
),
Container(
height: _size.height * 0.38,
width: _width,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(15),
bottomRight: Radius.circular(15)),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(12.0),
child: TextFieldAlertDialog(),
),
],
),
),
],
);
}
}
class TextFieldAlertDialog extends StatelessWidget {
_displayDialog(BuildContext context) async {
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
titlePadding: EdgeInsets.all(0),
actionsPadding: EdgeInsets.all(0),
buttonPadding: EdgeInsets.all(0),
//TODO: THIS TEXTBUTTON WON'T TAKE THE SIZE OF ITS PARENT //.
actions: <Widget>[
SizedBox(
width: double.infinity,
child: TextButton(
onPressed: () {},
child: Text("Reset Password"),
// style: TextButton.styleFrom(
// textStyle: GoogleFonts.rubik(fontSize: 25),
// primary: Colors.white,
// backgroundColor: Color(0xffB2413A),
// ),
),
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Center(
child: TextButton(
child: Text(
'FORGET PASSWORD',
// style: GoogleFonts.rubik(
// color: Colors.black, fontSize: 18, fontWeight: FontWeight.w500),
),
onPressed: () => _displayDialog(context),
),
);
}
}
任何解决此问题的帮助将不胜感激。
我添加了崩溃报告的屏幕截图:
问题:
- 如何让按钮占据父 AlertDialog 的整个宽度?