颤振按钮新主题复制不改变颜色

时间:2019-10-18 19:19:46

标签: flutter themes

下面有一些代码。它应该显示带有蓝色的“添加图标”。而是显示带有黑色的按钮(在MaterialApp中为accentColor)。 我在“ floatingActionButton”中做错什么了吗?

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    final appName = "Custom Themes";

    return new MaterialApp(
      title: appName,
      theme: new ThemeData(
        brightness: Brightness.dark,
        primaryColor: Colors.red[300],
        accentColor: Colors.black,
      ),
      home: new MyHomePage(
        title: appName
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  final String title;

  MyHomePage({Key key, @required this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      appBar: AppBar(
        title: new Text(title),
      ),
      body: new Center(
        child: new Container(
          color: Theme.of(context).primaryColor,
          child: new Text(
            "Text with background color",
            style: Theme.of(context).textTheme.title,
          ),
        ),
      ),
    floatingActionButton: new Theme(
      data: Theme.of(context).copyWith(accentColor: Colors.blue),
      child: new FloatingActionButton(
        onPressed: null,
        child: new Icon(Icons.add),
      ),
    )
    );
  }

}

我看了一个教程(在iOS中是颤抖的),那里的一切都像个魅力。 Tut是从2019年4月开始的。也许从那时起有些变化?

1 个答案:

答案 0 :(得分:0)

您可以使用

Theme.of(context).copyWith(
        colorScheme:
        Theme.of(context).colorScheme.copyWith(secondary: Colors.blue),
      )

详细信息参考https://flutter.dev/docs/cookbook/design/themes#complete-example

完整代码

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    final appName = "Custom Themes";

    return  MaterialApp(
      title: appName,
      theme:  ThemeData(
        brightness: Brightness.dark,
        primaryColor: Colors.red[300],
        accentColor: Colors.black,
      ),
      home:  MyHomePage(
          title: appName
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  final String title;

  MyHomePage({Key key, @required this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return  Scaffold(
        appBar: AppBar(
          title:  Text(title),
        ),
        body:  Center(
          child:  Container(
            color: Theme.of(context).primaryColor,
            child:  Text(
              "Text with background color",
              style: Theme.of(context).textTheme.title,
            ),
          ),
        ),
        floatingActionButton:  Theme(
          data: Theme.of(context).copyWith(
            colorScheme:
            Theme.of(context).colorScheme.copyWith(secondary: Colors.blue),
          ),
          child:  FloatingActionButton(
            onPressed: null,
            child:  Icon(Icons.add),
          ),
        )
    );
  }

}

enter image description here