【问题标题】:Appbar color, toolbar color, text, font and buttons are not changing by applying Theme Data in flutter App在flutter App中应用主题数据不会改变Appbar颜色,工具栏颜色,文本,字体和按钮
【发布时间】:2020-11-18 23:27:10
【问题描述】:
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Personal Expenses',
      theme: ThemeData(
    
        brightness: Brightness.light,
        primaryColor: Colors.lightGreen[800],
        primarySwatch: Colors.amber,
        errorColor: Colors.red,
        accentColor: Colors.cyan[600],
        fontFamily: 'Quicksand',
        textTheme: ThemeData.light().textTheme.copyWith(
            title: TextStyle(
              fontFamily: 'Quicksand',
              backgroundColor: Colors.grey,
              fontSize: 18,
            ),
        ),
        appBarTheme: AppBarTheme(
          textTheme: ThemeData.light().textTheme.copyWith(
              title: TextStyle(
                  fontFamily: 'OpenSans',
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                  fontStyle: FontStyle.italic),
                  button: TextStyle(color: Colors.white),
              ),
          )
        ),
        home: MyHomePage(),
    );
  }

例如,要在 Button 上应用主题,我正在这样做,但主题不适用于它

FlatButton(
  color: Colors.white,
  textColor: Theme.of(context).textTheme.button.color,
  onPressed: _presentDatePicker,
  child: Text(
    'Choose Date',
    style: TextStyle(fontWeight: FontWeight.bold),
  ),
)

【问题讨论】:

    标签: flutter flutter-theme


    【解决方案1】:

    您在AppBarTheme 下设置按钮文本颜色,而不是在您在FlatButton 中引用的textTheme

    这可能会解决您的问题。

    FlatButton(
      color: Colors.white,
      textColor: Theme.of(context).AppBarTheme.textTheme.button.color,
      onPressed: _presentDatePicker,
      child: Text(
        'Choose Date',
        style: TextStyle(fontWeight: FontWeight.bold),
      ),
    )
    

    或者你可以像这样设置buttonColor

    theme: ThemeData(
        primarySwatch: Colors.purple,
        accentColor: Colors.amber,
        buttonColor: Colors.white,
    ),
    

    并像这样在FlatButton 中使用该按钮颜色

    FlatButton(
      color: Colors.white,
      textColor: Theme.of(context).buttonColor,
      onPressed: _presentDatePicker,
      child: Text(
        'Choose Date',
        style: TextStyle(fontWeight: FontWeight.bold),
      ),
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-05
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 2016-11-07
      • 2021-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多