gpt4 book ai didi

Flutter ElevatedButton 不继承 ButtonThemeData 形状

转载 作者:行者123 更新时间:2023-12-05 04:52:58 25 4
gpt4 key购买 nike

我已经为我的顶级 ThemeData 提供了一个具有特定形状的 ButtonThemeData。不管怎样,ElevatedButton 具有默认形状。

我的顶级主题是这样定义的...

return ScopedModel<MyModel>(
model: _model,
child: MaterialApp(
title: "MyApp",
theme: ThemeData(
fontFamily: 'Din',
colorScheme: MyLightTheme.colorScheme,
inputDecorationTheme: MyLightTheme.inputDecorationTheme,
accentColor: MyColors.secondary,
errorColor: MyLightTheme.colorScheme.error,
primarySwatch: MyColors.primarySwatch,
buttonTheme: ButtonThemeData(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(color: Colors.red)
)
)
),
debugShowCheckedModeBanner: false,
home: EntryScreen(),
routes: <String, WidgetBuilder>{

}
)
);

我定义了一个简单的 FlatButton 如下...

ElevatedButton(
onPressed: () {
/*...*/
},
child: Text(
"Flat Button",
),
)

当 buttonColor 不是白色时,FlatButton 只是绘制白色。

最佳答案

您可以在下面复制粘贴运行完整代码
您可以使用 elevatedButtonTheme 并设置 ButtonStyleshape
代码片段

ThemeData(
fontFamily: 'Din',
elevatedButtonTheme: ElevatedButtonThemeData(
style: ButtonStyle(
shape: MaterialStateProperty.all<OutlinedBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(color: Colors.red))))))

工作演示

enter image description here

完整代码

import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "MyApp",
theme: ThemeData(
fontFamily: 'Din',
elevatedButtonTheme: ElevatedButtonThemeData(
style: ButtonStyle(
shape: MaterialStateProperty.all<OutlinedBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(color: Colors.red)))))),
debugShowCheckedModeBanner: false,
home: EntryScreen(),
);
}
}

class EntryScreen extends StatefulWidget {
EntryScreen({Key key}) : super(key: key);

@override
_EntryScreenState createState() => _EntryScreenState();
}

class _EntryScreenState extends State<EntryScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("test"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: () {
/*...*/
},
child: Text(
"Flat Button",
),
)
],
),
),
);
}
}

关于Flutter ElevatedButton 不继承 ButtonThemeData 形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66340574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com